Skip to content

Commit d125276

Browse files
fix: Added reconnect logic for sidecar in the electron app (#990)
Closes #956 --------- Co-authored-by: Miguel Betegón <[email protected]>
1 parent fd42752 commit d125276

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

.changeset/busy-shoes-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@spotlightjs/electron": minor
3+
---
4+
5+
Added restart logic for the sidecar

packages/electron/src/electron/main/index.ts

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "node:path";
22
import * as Sentry from "@sentry/electron/main";
33
import { clearBuffer, setupSidecar } from "@spotlightjs/sidecar";
4+
import { DEFAULT_PORT } from "@spotlightjs/sidecar/constants";
45
import { BrowserWindow, Menu, Tray, app, dialog, ipcMain, nativeImage, shell } from "electron";
56
import Store from "electron-store";
67
import { autoUpdater } from "electron-updater";
@@ -543,7 +544,7 @@ function createTray() {
543544
{
544545
label: "Sidecar Status",
545546
enabled: false,
546-
sublabel: "Running on port 8969",
547+
sublabel: `Running on port ${DEFAULT_PORT}`,
547548
},
548549
{ type: "separator" },
549550
{
@@ -580,14 +581,7 @@ function createTray() {
580581
});
581582
}
582583

583-
Promise.all([
584-
setupSidecar({
585-
port: 8969,
586-
incomingPayload: storeIncomingPayload,
587-
isStandalone: true,
588-
}),
589-
app.whenReady(),
590-
]).then(() => {
584+
app.whenReady().then(() => {
591585
if (!isLinux) {
592586
createTray();
593587
}
@@ -599,3 +593,51 @@ Promise.all([
599593

600594
ipcMain.on("set-badge-count", handleBadgeCount);
601595
});
596+
597+
const MAX_RETRIES = 3;
598+
const RECHECK_DELAY = 5000;
599+
const RETRY_DELAY_INCREMENT = 2000;
600+
601+
async function makeSureSidecarIsRunning() {
602+
let retries = 0;
603+
let subscriber: NodeJS.Timeout | null = null;
604+
605+
async function handler() {
606+
try {
607+
await setupSidecar({
608+
port: DEFAULT_PORT,
609+
incomingPayload: storeIncomingPayload,
610+
isStandalone: true,
611+
});
612+
613+
retries = 0;
614+
} catch (error) {
615+
console.error(error);
616+
retries++;
617+
618+
if (retries >= MAX_RETRIES) {
619+
Sentry.captureException(error);
620+
621+
dialog.showErrorBox(
622+
"Spotlight",
623+
`Unable to start Spotlight server. This could happen due to a port (${DEFAULT_PORT}) conflict or the server being blocked by a firewall. Try checking your firewall settings and restart the app.`,
624+
);
625+
}
626+
}
627+
628+
if (subscriber) {
629+
clearTimeout(subscriber);
630+
subscriber = null;
631+
}
632+
633+
if (retries >= MAX_RETRIES) {
634+
return;
635+
}
636+
637+
subscriber = setTimeout(handler, RECHECK_DELAY + retries * RETRY_DELAY_INCREMENT);
638+
}
639+
640+
handler();
641+
}
642+
643+
makeSureSidecarIsRunning();

0 commit comments

Comments
 (0)