add robustness when we run the electron process and move the exit
controller inside the main js file
This commit is contained in:
parent
3a443402a6
commit
0ae2e7f73c
4 changed files with 33 additions and 9 deletions
12
server.js
12
server.js
|
@ -150,6 +150,18 @@
|
||||||
if (err) throw err
|
if (err) throw err
|
||||||
// Create/activate the webtorrent module
|
// Create/activate the webtorrent module
|
||||||
webtorrent.create(function () {
|
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 -----------
|
// ----------- Make the server listening -----------
|
||||||
server.listen(port, function () {
|
server.listen(port, function () {
|
||||||
videos.seedAll(function () {
|
videos.seedAll(function () {
|
||||||
|
|
|
@ -186,5 +186,10 @@
|
||||||
return dec
|
return dec
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utils.cleanForExit = function (webtorrent_process) {
|
||||||
|
logger.info('Gracefully exiting')
|
||||||
|
process.kill(-webtorrent_process.pid)
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = utils
|
module.exports = utils
|
||||||
})()
|
})()
|
||||||
|
|
|
@ -43,11 +43,23 @@
|
||||||
ipc.serve(function () {
|
ipc.serve(function () {
|
||||||
if (!webtorrentnode.silent) logger.info('IPC server ready.')
|
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 () {
|
ipc.server.on(processKey + '.ready', function () {
|
||||||
if (!webtorrentnode.silent) logger.info('Webtorrent process ready.')
|
if (!webtorrentnode.silent) logger.info('Webtorrent process ready.')
|
||||||
|
clearTimeout(timeout_webtorrent_process)
|
||||||
callback()
|
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 })
|
var webtorrent_process = spawn(__dirname + '/webtorrent.js', host, port, { detached: true })
|
||||||
webtorrent_process.stderr.on('data', function (data) {
|
webtorrent_process.stderr.on('data', function (data) {
|
||||||
// logger.debug('Webtorrent process stderr: ', data.toString())
|
// logger.debug('Webtorrent process stderr: ', data.toString())
|
||||||
|
@ -57,15 +69,6 @@
|
||||||
// logger.debug('Webtorrent process:', data.toString())
|
// 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
|
webtorrentnode.app = webtorrent_process
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -83,5 +83,9 @@
|
||||||
ipc.of[nodeKey].emit(processKey + '.ready')
|
ipc.of[nodeKey].emit(processKey + '.ready')
|
||||||
console.log('Ready.')
|
console.log('Ready.')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
process.on('uncaughtException', function (e) {
|
||||||
|
ipc.of[nodeKey].emit(processKey + '.exception', { exception: e })
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
|
Loading…
Reference in a new issue