Skip to content

Commit

Permalink
feat: health check
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Oct 17, 2023
1 parent 2fbca82 commit bd5f34e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/main/background-processes/sync-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,56 @@ import { BrowserWindow, ipcMain } from 'electron';
import path from 'path';
import Logger from 'electron-log';
import eventBus from '../event-bus';
import nodeSchedule from 'node-schedule';

let worker: BrowserWindow | null = null;
let workerIsRunning = false;
let startingWorker = false;
let healthCheckSchedule: nodeSchedule.Job | null = null;

async function healthCheck() {
const responsePromise = new Promise<void>((resolve, reject) => {
ipcMain.once('SYNC_ENGINE:PONG', () => {
resolve();
});

const millisecondsToWait = 2_000;

setTimeout(() => {
reject(
new Error(
`Health check failed after ${millisecondsToWait} milliseconds`
)
);
}, millisecondsToWait);
});

worker?.webContents.send('SYNC_ENGINE:PING');

await responsePromise;
}

function scheduleHeathCheck() {
if (healthCheckSchedule) {
healthCheckSchedule.cancel(false);
}

const relaunchOnFail = () =>
healthCheck()
.then(() => {
// Logger.debug('Health check succeeded');
})
.catch(() => {
Logger.warn('Health check failed, relaunching the worker');
workerIsRunning = false;
worker?.destroy();
spawnSyncEngineWorker();
});

healthCheckSchedule = nodeSchedule.scheduleJob('*/30 * * * * *', async () => {
await relaunchOnFail();
});
}

function spawnSyncEngineWorker() {
if (startingWorker) {
Expand Down Expand Up @@ -35,6 +81,7 @@ function spawnSyncEngineWorker() {
)
.then(() => {
Logger.info('[MAIN] Sync engine worker loaded');
scheduleHeathCheck();
})
.catch((err) => {
Logger.error('[MAIN] Error loading sync engine worker', err);
Expand Down Expand Up @@ -66,6 +113,8 @@ function spawnSyncEngineWorker() {
export async function stopSyncEngineWatcher() {
Logger.info('[MAIN] STOPPING SYNC ENGINE WORKER...');

healthCheckSchedule?.cancel(false);

if (!workerIsRunning) {
Logger.info('[MAIN] WORKER WAS NOT RUNNING');
worker?.destroy();
Expand Down Expand Up @@ -112,6 +161,8 @@ export async function stopSyncEngineWatcher() {
async function stopAndClearSyncEngineWatcher() {
Logger.info('[MAIN] STOPPING AND CLEAR SYNC ENGINE WORKER...');

healthCheckSchedule?.cancel(false);

if (!workerIsRunning) {
Logger.info('[MAIN] WORKER WAS NOT RUNNING');
worker?.destroy();
Expand Down
4 changes: 4 additions & 0 deletions src/workers/sync-engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ async function setUp() {
}
});

ipcRenderer.on('SYNC_ENGINE:PING', (event) => {
event.sender.send('SYNC_ENGINE:PONG');
});

await bindings.start(
packageJson.version,
'{E9D7EB38-B229-5DC5-9396-017C449D59CD}'
Expand Down

0 comments on commit bd5f34e

Please sign in to comment.