-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
45 lines (38 loc) · 1.34 KB
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import io from 'socket.io-client';
import querystring from 'querystring';
import JobRunner from './job_runner';
module.exports = (args) => {
//TODO validate args --url
//TODO validate args --secret
let secret = args['secret'];
let socket = io.connect(args['url'], { query: querystring.stringify({ secret: secret, platform: process.platform }) });
socket.on('messages', (data) => {
console.log('Message:', data);
});
socket.on('run_job', (data) => {
try {
JobRunner(data, socket);
} catch (e) {
socket.emit('job_' + data.id, {
type: 'error',
message: e
});
}
})
//TODO decidir si esto es util. Se podría usar pm2 para forar el reinicio despues del disconnect.
//Quizas el propio socket reconecta mejor solo que reiniciando.
socket.on('disconnect', () => {
console.log('Server gone');
// console.log("Restarting");
// setTimeout(function () {
// process.on("exit", function () {
// require("child_process").spawn(process.argv.shift(), process.argv, {
// cwd: process.cwd(),
// detached: true,
// stdio: "inherit"
// });
// });
// process.exit();
// }, 5000);
})
}