diff --git a/server.js b/server.js index fe0cb237a..96c493f29 100644 --- a/server.js +++ b/server.js @@ -150,6 +150,18 @@ if (err) throw err // Create/activate the webtorrent module webtorrent.create(function () { + function cleanForExit () { + utils.cleanForExit(webtorrent.app) + } + + function exitGracefullyOnSignal () { + process.exit() + } + + process.on('exit', cleanForExit) + process.on('SIGINT', exitGracefullyOnSignal) + process.on('SIGTERM', exitGracefullyOnSignal) + // ----------- Make the server listening ----------- server.listen(port, function () { videos.seedAll(function () { diff --git a/src/utils.js b/src/utils.js index 7a5c7b7ea..8ce1789f9 100644 --- a/src/utils.js +++ b/src/utils.js @@ -186,5 +186,10 @@ return dec } + utils.cleanForExit = function (webtorrent_process) { + logger.info('Gracefully exiting') + process.kill(-webtorrent_process.pid) + } + module.exports = utils })() diff --git a/src/webTorrentNode.js b/src/webTorrentNode.js index ebe8d5d81..507cf31fc 100644 --- a/src/webTorrentNode.js +++ b/src/webTorrentNode.js @@ -43,11 +43,23 @@ ipc.serve(function () { if (!webtorrentnode.silent) logger.info('IPC server ready.') + // Run a timeout of 30s after which we exit the process + var timeout_webtorrent_process = setTimeout(function () { + logger.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.') + process.exit() + }, 30000) + ipc.server.on(processKey + '.ready', function () { if (!webtorrentnode.silent) logger.info('Webtorrent process ready.') + clearTimeout(timeout_webtorrent_process) callback() }) + ipc.server.on(processKey + '.exception', function (data) { + logger.error('Received exception error from webtorrent process.', { exception: data.exception }) + process.exit() + }) + var webtorrent_process = spawn(__dirname + '/webtorrent.js', host, port, { detached: true }) webtorrent_process.stderr.on('data', function (data) { // logger.debug('Webtorrent process stderr: ', data.toString()) @@ -57,15 +69,6 @@ // logger.debug('Webtorrent process:', data.toString()) }) - function exitChildProcess () { - if (!webtorrentnode.silent) logger.info('Gracefully exit child') - process.kill(-webtorrent_process.pid) - process.exit(0) - } - - process.on('SIGINT', exitChildProcess) - process.on('SIGTERM', exitChildProcess) - webtorrentnode.app = webtorrent_process }) diff --git a/src/webtorrent.js b/src/webtorrent.js index 840f97671..18ae6b6ba 100644 --- a/src/webtorrent.js +++ b/src/webtorrent.js @@ -83,5 +83,9 @@ ipc.of[nodeKey].emit(processKey + '.ready') console.log('Ready.') }) + + process.on('uncaughtException', function (e) { + ipc.of[nodeKey].emit(processKey + '.exception', { exception: e }) + }) } })()