From 0768b1b9079ea75c22b453e1856e1f2841ff8571 Mon Sep 17 00:00:00 2001 From: Eva Ho Date: Fri, 21 Jul 2023 12:47:54 -0400 Subject: [PATCH] restart server with condition and timeout --- app/src/index.ts | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/app/src/index.ts b/app/src/index.ts index a88afdc2..5e3fa9fc 100644 --- a/app/src/index.ts +++ b/app/src/index.ts @@ -102,30 +102,34 @@ if (require('electron-squirrel-startup')) { function server() { const binary = app.isPackaged - ? path.join(process.resourcesPath, 'ollama') - : path.resolve(process.cwd(), '..', 'ollama') + ? path.join(process.resourcesPath, 'ollama') + : path.resolve(process.cwd(), '..', 'ollama'); - const proc = spawn(binary, ['serve']) + const proc = spawn(binary, ['serve']); proc.stdout.on('data', data => { - logger.info(data.toString().trim()) - }) + logger.info(data.toString().trim()); + }); proc.stderr.on('data', data => { - logger.error(data.toString().trim()) - }) + logger.error(data.toString().trim()); + }); + - function restart() { - logger.info('Restarting the server...') - server() - } - - proc.on('exit', restart) + proc.on('exit', (code, signal) => { + if (code === 0 || code === null) { + logger.info('Server has stopped.'); + setTimeout(server, 5000); + } else { + logger.error(`Server exited with code: ${code}, signal: ${signal}`); + setTimeout(server, 3000); + } + }); app.on('before-quit', () => { - proc.off('exit', restart) - proc.kill() - }) + proc.off('exit', server); + proc.kill(); + }); } if (process.platform === 'darwin') {