restart server with condition and timeout

This commit is contained in:
Eva Ho 2023-07-21 12:47:54 -04:00
parent f5f0da06d9
commit 0768b1b907

View file

@ -102,30 +102,34 @@ if (require('electron-squirrel-startup')) {
function server() { function server() {
const binary = app.isPackaged const binary = app.isPackaged
? path.join(process.resourcesPath, 'ollama') ? path.join(process.resourcesPath, 'ollama')
: path.resolve(process.cwd(), '..', 'ollama') : path.resolve(process.cwd(), '..', 'ollama');
const proc = spawn(binary, ['serve']) const proc = spawn(binary, ['serve']);
proc.stdout.on('data', data => { proc.stdout.on('data', data => {
logger.info(data.toString().trim()) logger.info(data.toString().trim());
}) });
proc.stderr.on('data', data => { proc.stderr.on('data', data => {
logger.error(data.toString().trim()) logger.error(data.toString().trim());
}) });
function restart() { proc.on('exit', (code, signal) => {
logger.info('Restarting the server...') if (code === 0 || code === null) {
server() logger.info('Server has stopped.');
} setTimeout(server, 5000);
} else {
proc.on('exit', restart) logger.error(`Server exited with code: ${code}, signal: ${signal}`);
setTimeout(server, 3000);
}
});
app.on('before-quit', () => { app.on('before-quit', () => {
proc.off('exit', restart) proc.off('exit', server);
proc.kill() proc.kill();
}) });
} }
if (process.platform === 'darwin') { if (process.platform === 'darwin') {