Skip to content

Commit

Permalink
feat: uregister when user logs out
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Sep 28, 2023
1 parent 302d133 commit 12e1966
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
69 changes: 59 additions & 10 deletions src/main/background-processes/sync-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,29 @@ 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;
}

const stopPromise = new Promise<void>((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);
});

Expand All @@ -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 {
Expand All @@ -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<void>((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);
18 changes: 18 additions & 0 deletions src/workers/sync-engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}'
Expand Down

0 comments on commit 12e1966

Please sign in to comment.