]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - src/webTorrentNode.js
add robustness when we run the electron process and move the exit
[github/Chocobozzz/PeerTube.git] / src / webTorrentNode.js
1 ;(function () {
2 'use strict'
3
4 var spawn = require('electron-spawn')
5 var config = require('config')
6 var ipc = require('node-ipc')
7 var pathUtils = require('path')
8
9 var logger = require('./logger')
10
11 var host = config.get('webserver.host')
12 var port = config.get('webserver.port')
13
14 var nodeKey = 'webtorrentnode' + port
15 var processKey = 'webtorrent' + port
16
17 ipc.config.silent = true
18 ipc.config.id = nodeKey
19
20 var webtorrentnode = {}
21
22 // Useful for beautiful tests
23 webtorrentnode.silent = false
24
25 // Useful to kill it
26 webtorrentnode.app = null
27
28 webtorrentnode.create = function (options, callback) {
29 if (typeof options === 'function') {
30 callback = options
31 options = {}
32 }
33
34 // Override options
35 if (options.host) host = options.host
36 if (options.port) {
37 port = options.port
38 nodeKey = 'webtorrentnode' + port
39 processKey = 'webtorrent' + port
40 ipc.config.id = nodeKey
41 }
42
43 ipc.serve(function () {
44 if (!webtorrentnode.silent) logger.info('IPC server ready.')
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
52 ipc.server.on(processKey + '.ready', function () {
53 if (!webtorrentnode.silent) logger.info('Webtorrent process ready.')
54 clearTimeout(timeout_webtorrent_process)
55 callback()
56 })
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
63 var webtorrent_process = spawn(__dirname + '/webtorrent.js', host, port, { detached: true })
64 webtorrent_process.stderr.on('data', function (data) {
65 // logger.debug('Webtorrent process stderr: ', data.toString())
66 })
67
68 webtorrent_process.stdout.on('data', function (data) {
69 // logger.debug('Webtorrent process:', data.toString())
70 })
71
72 webtorrentnode.app = webtorrent_process
73 })
74
75 ipc.server.start()
76 }
77
78 webtorrentnode.seed = function (path, callback) {
79 var extension = pathUtils.extname(path)
80 var basename = pathUtils.basename(path, extension)
81 var data = {
82 _id: basename,
83 args: {
84 path: path
85 }
86 }
87
88 if (!webtorrentnode.silent) logger.debug('Node wants to seed ' + data._id)
89
90 // Finish signal
91 ipc.server.on(nodeKey + '.seedDone.' + data._id, function (received) {
92 if (!webtorrentnode.silent) logger.debug('Process seeded torrent ' + received.magnetUri)
93
94 // This is a fake object, we just use the magnetUri in this project
95 var torrent = {
96 magnetURI: received.magnetUri
97 }
98
99 callback(torrent)
100 })
101
102 ipc.server.broadcast(processKey + '.seed', data)
103 }
104
105 webtorrentnode.add = function (magnetUri, callback) {
106 var data = {
107 _id: magnetUri,
108 args: {
109 magnetUri: magnetUri
110 }
111 }
112
113 if (!webtorrentnode.silent) logger.debug('Node wants to add ' + data._id)
114
115 // Finish signal
116 ipc.server.on(nodeKey + '.addDone.' + data._id, function (received) {
117 if (!webtorrentnode.silent) logger.debug('Process added torrent')
118
119 // This is a fake object, we just use the magnetUri in this project
120 var torrent = {
121 files: received.files
122 }
123
124 callback(torrent)
125 })
126
127 ipc.server.broadcast(processKey + '.add', data)
128 }
129
130 webtorrentnode.remove = function (magnetUri, callback) {
131 var data = {
132 _id: magnetUri,
133 args: {
134 magnetUri: magnetUri
135 }
136 }
137
138 if (!webtorrentnode.silent) logger.debug('Node wants to stop seeding ' + data._id)
139
140 // Finish signal
141 ipc.server.on(nodeKey + '.removeDone.' + data._id, function (received) {
142 if (!webtorrentnode.silent) logger.debug('Process removed torrent ' + data._id)
143
144 var err = null
145 if (received.err) err = received.err
146
147 callback(err)
148 })
149
150 ipc.server.broadcast(processKey + '.remove', data)
151 }
152
153 module.exports = webtorrentnode
154 })()