-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_runner.js
68 lines (50 loc) · 1.54 KB
/
job_runner.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { exec, mkdir, test } from 'shelljs';
const runScript = async (job, command, socket) => {
}
const runExecutable = (job, command, socket) => {
return new Promise((resolve, reject) => {
socket.emit('job_' + job.id, {
type: 'log',
message: "Start Working on '" + command.name + "'"
});
const child = exec(command.params.command, { silent: true, async: true });
child.stdout.on('data', (data) => {
socket.emit('job_' + job.id, {
type: 'log',
message: data.toString()
})
});
child.stderr.on('data', (data) => {
socket.emit('job_' + job.id, {
type: 'error',
message: data.toString()
})
});
child.on('close', (code) => {
if (code === 0) {
//TODO check code and launch reject
return resolve();
}
return reject();
})
});
}
export default async (job, socket) => {
socket.emit('job_' + job.id, {
type: 'log',
message: "Start Working on " + job.name
});
if (!test('-e', './work/' + job.id)) {
mkdir('-p', './work/' + job.id);
}
for (const command of job.commands_info) {
switch (command.type) {
case 'script':
await runScript(job, command, socket);
break;
case 'executable':
await runExecutable(job, command, socket);
break;
}
}
}