aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2015-11-02 22:19:39 +0100
committerChocobozzz <florian.bigard@gmail.com>2015-11-03 08:10:30 +0100
commit0ae2e7f73c24779f8c99453fb02491382cc0520e (patch)
treef7bd68672746107c5bcc91686fef48c0f5c11bf2
parent3a443402a6f22e4ac1fd80af62abfef7efd6d7a4 (diff)
downloadPeerTube-0ae2e7f73c24779f8c99453fb02491382cc0520e.tar.gz
PeerTube-0ae2e7f73c24779f8c99453fb02491382cc0520e.tar.zst
PeerTube-0ae2e7f73c24779f8c99453fb02491382cc0520e.zip
add robustness when we run the electron process and move the exit
controller inside the main js file
-rw-r--r--server.js12
-rw-r--r--src/utils.js5
-rw-r--r--src/webTorrentNode.js21
-rw-r--r--src/webtorrent.js4
4 files changed, 33 insertions, 9 deletions
diff --git a/server.js b/server.js
index fe0cb237a..96c493f29 100644
--- a/server.js
+++ b/server.js
@@ -150,6 +150,18 @@
150 if (err) throw err 150 if (err) throw err
151 // Create/activate the webtorrent module 151 // Create/activate the webtorrent module
152 webtorrent.create(function () { 152 webtorrent.create(function () {
153 function cleanForExit () {
154 utils.cleanForExit(webtorrent.app)
155 }
156
157 function exitGracefullyOnSignal () {
158 process.exit()
159 }
160
161 process.on('exit', cleanForExit)
162 process.on('SIGINT', exitGracefullyOnSignal)
163 process.on('SIGTERM', exitGracefullyOnSignal)
164
153 // ----------- Make the server listening ----------- 165 // ----------- Make the server listening -----------
154 server.listen(port, function () { 166 server.listen(port, function () {
155 videos.seedAll(function () { 167 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 @@
186 return dec 186 return dec
187 } 187 }
188 188
189 utils.cleanForExit = function (webtorrent_process) {
190 logger.info('Gracefully exiting')
191 process.kill(-webtorrent_process.pid)
192 }
193
189 module.exports = utils 194 module.exports = utils
190})() 195})()
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 @@
43 ipc.serve(function () { 43 ipc.serve(function () {
44 if (!webtorrentnode.silent) logger.info('IPC server ready.') 44 if (!webtorrentnode.silent) logger.info('IPC server ready.')
45 45
46 // Run a timeout of 30s after which we exit the process
47 var timeout_webtorrent_process = setTimeout(function () {
48 logger.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
49 process.exit()
50 }, 30000)
51
46 ipc.server.on(processKey + '.ready', function () { 52 ipc.server.on(processKey + '.ready', function () {
47 if (!webtorrentnode.silent) logger.info('Webtorrent process ready.') 53 if (!webtorrentnode.silent) logger.info('Webtorrent process ready.')
54 clearTimeout(timeout_webtorrent_process)
48 callback() 55 callback()
49 }) 56 })
50 57
58 ipc.server.on(processKey + '.exception', function (data) {
59 logger.error('Received exception error from webtorrent process.', { exception: data.exception })
60 process.exit()
61 })
62
51 var webtorrent_process = spawn(__dirname + '/webtorrent.js', host, port, { detached: true }) 63 var webtorrent_process = spawn(__dirname + '/webtorrent.js', host, port, { detached: true })
52 webtorrent_process.stderr.on('data', function (data) { 64 webtorrent_process.stderr.on('data', function (data) {
53 // logger.debug('Webtorrent process stderr: ', data.toString()) 65 // logger.debug('Webtorrent process stderr: ', data.toString())
@@ -57,15 +69,6 @@
57 // logger.debug('Webtorrent process:', data.toString()) 69 // logger.debug('Webtorrent process:', data.toString())
58 }) 70 })
59 71
60 function exitChildProcess () {
61 if (!webtorrentnode.silent) logger.info('Gracefully exit child')
62 process.kill(-webtorrent_process.pid)
63 process.exit(0)
64 }
65
66 process.on('SIGINT', exitChildProcess)
67 process.on('SIGTERM', exitChildProcess)
68
69 webtorrentnode.app = webtorrent_process 72 webtorrentnode.app = webtorrent_process
70 }) 73 })
71 74
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 @@
83 ipc.of[nodeKey].emit(processKey + '.ready') 83 ipc.of[nodeKey].emit(processKey + '.ready')
84 console.log('Ready.') 84 console.log('Ready.')
85 }) 85 })
86
87 process.on('uncaughtException', function (e) {
88 ipc.of[nodeKey].emit(processKey + '.exception', { exception: e })
89 })
86 } 90 }
87})() 91})()