11import path from "node:path" ;
22import * as Sentry from "@sentry/electron/main" ;
33import { clearBuffer , setupSidecar } from "@spotlightjs/sidecar" ;
4+ import { DEFAULT_PORT } from "@spotlightjs/sidecar/constants" ;
45import { BrowserWindow , Menu , Tray , app , dialog , ipcMain , nativeImage , shell } from "electron" ;
56import Store from "electron-store" ;
67import { 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