diff --git a/src/main/background-processes/sync-engine.ts b/src/main/background-processes/sync-engine.ts index 847d4ff34..efb984d00 100644 --- a/src/main/background-processes/sync-engine.ts +++ b/src/main/background-processes/sync-engine.ts @@ -33,21 +33,21 @@ function spawnSyncEngineWorker() { }); ipcMain.on('SYNC_ENGINE_PROCESS_SETUP_SUCCESSFUL', () => { - Logger.debug('[MAIN] SYNC ENGINE RUNNIG'); + Logger.debug('[MAIN] SYNC ENGINE RUNNING'); workerIsRunning = true; }); ipcMain.on('SYNC_ENGINE_PROCESS_SETUP_FAILED', () => { - Logger.debug('[MAIN] SYNC ENGINE NOT RUNNIG'); + Logger.debug('[MAIN] SYNC ENGINE NOT RUNNING'); workerIsRunning = false; }); } export async function stopSyncEngineWatcher() { - Logger.info('[MAIN] STOPING SYNC ENGINE WORKER...'); + Logger.info('[MAIN] STOPPING SYNC ENGINE WORKER...'); if (!workerIsRunning) { - Logger.info('[MAIN] WORKER WAS NOT RUNNIG'); + Logger.info('[MAIN] WORKER WAS NOT RUNNING'); worker?.destroy(); worker = null; return; @@ -55,7 +55,7 @@ export async function stopSyncEngineWatcher() { const stopPromise = new Promise((resolve, reject) => { ipcMain.once('SYNC_ENGINE_STOP_ERROR', (_, error: Error) => { - Logger.error('[MAIN] Error stoping sync engine worker', error); + Logger.error('[MAIN] Error stopping sync engine worker', error); reject(error); }); @@ -64,15 +64,15 @@ export async function stopSyncEngineWatcher() { Logger.info('[MAIN] Sync engine stopped'); }); - const millisecndsToWait = 10_000; + const millisecondsToWait = 10_000; setTimeout(() => { reject( new Error( - `Timeout waiting for sync engien to stop after ${millisecndsToWait} milliseconds` + `Timeout waiting for sync engine to stop after ${millisecondsToWait} milliseconds` ) ); - }, millisecndsToWait); + }, millisecondsToWait); }); try { @@ -89,6 +89,55 @@ export async function stopSyncEngineWatcher() { } } -eventBus.on('USER_LOGGED_OUT', stopSyncEngineWatcher); -eventBus.on('USER_WAS_UNAUTHORIZED', stopSyncEngineWatcher); +async function stopAndClearSyncEngineWatcher() { + Logger.info('[MAIN] STOPPING AND CLEAR SYNC ENGINE WORKER...'); + + if (!workerIsRunning) { + Logger.info('[MAIN] WORKER WAS NOT RUNNING'); + worker?.destroy(); + worker = null; + return; + } + + const response = new Promise((resolve, reject) => { + ipcMain.once( + 'ERROR_ON_STOP_AND_CLEAR_SYNC_ENGINE_PROCESS', + (_, error: Error) => { + Logger.error('[MAIN] Error stopping sync engine worker', error); + reject(error); + } + ); + + ipcMain.once('SYNC_ENGINE_STOP_AND_CLEAR_SUCCESS', () => { + resolve(); + Logger.info('[MAIN] Sync engine stopped and cleared'); + }); + + const millisecondsToWait = 10_000; + + setTimeout(() => { + reject( + new Error( + `Timeout waiting for sync engine to stop after ${millisecondsToWait} milliseconds` + ) + ); + }, millisecondsToWait); + }); + + try { + worker?.webContents.send('STOP_AND_CLEAR_SYNC_ENGINE_PROCESS'); + + await response; + } catch (err) { + // TODO: handle error + Logger.error(err); + } finally { + worker?.destroy(); + workerIsRunning = false; + worker = null; + } +} + +eventBus.on('USER_LOGGED_OUT', stopAndClearSyncEngineWatcher); +eventBus.on('USER_WAS_UNAUTHORIZED', stopAndClearSyncEngineWatcher); eventBus.on('INITIAL_SYNC_READY', spawnSyncEngineWorker); diff --git a/src/workers/sync-engine/index.ts b/src/workers/sync-engine/index.ts index 4e22de93e..244ca8348 100644 --- a/src/workers/sync-engine/index.ts +++ b/src/workers/sync-engine/index.ts @@ -5,6 +5,7 @@ import packageJson from '../../../package.json'; import { BindingsManager } from './BindingManager'; import fs from 'fs/promises'; import { iconPath } from 'workers/utils/icon'; +import { VirtualDrive } from 'virtual-drive/dist'; async function ensureTheFolderExist(path: string) { try { @@ -42,6 +43,23 @@ async function setUp() { event.sender.send('SYNC_ENGINE_STOP_SUCCESS'); }); + ipcRenderer.on('STOP_AND_CLEAR_SYNC_ENGINE_PROCESS', async (event) => { + Logger.info('[SYNC ENGINE] Stopping and clearing sync engine'); + + try { + await bindings.stop(); + + VirtualDrive.unregisterSyncRoot(virtualDrivePath); + + Logger.info('[SYNC ENGINE] sync engine stopped successfully'); + + event.sender.send('SYNC_ENGINE_STOP_AND_CLEAR_SUCCESS'); + } catch (error: unknown) { + Logger.error('[SYNC ENGINE] Error stopping and cleaning: ', error); + event.sender.send('ERROR_ON_STOP_AND_CLEAR_SYNC_ENGINE_PROCESS'); + } + }); + await bindings.start( packageJson.version, '{E9D7EB38-B229-5DC5-9396-017C449D59CD}'