-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.demo.js
74 lines (65 loc) · 1.83 KB
/
basic.demo.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
69
70
71
72
73
74
// Require the main Pidman class
const Pidman = require("./dist").Pidman;
// Optionally create groups manually
const PidmanGroup = require("./dist").PidmanGroup;
// Instantiate the Pidman's root class. Everything starts here.
const pm = new Pidman();
/*
This is a shared container working as a Pidman's events handler.
We could monitor a PidmanProcess individually or choose to include this
in the group options and monitor all of them from a single place.
*/
const monitor = {
// Whenever a process outputs some data, it's injected here.
onData: function (data) {
console.log(data.output);
},
/*
When the process closes/exits by whatever reason, being error or success.
The argument provided to this function has all the required information
as well as the corresponding PidmanProcess.
*/
onClose: function (data) {
console.log(data);
},
};
/*
Optionally choose to create a new group by class.
We could just provide the options object to Pidman's addProcessGroup method,
which also accepts a PidmanGroup object.
*/
const group = new PidmanGroup({
user: "nico",
/*
Initialize the processes in this group.
We could use PidmanGroup's addProcess method which can accept an options object
or a PidmanProcess object.
*/
processes: [
{
command: "websockify",
arguments: "-D 127.0.0.1:8080 0.0.0.0:80".split(" "),
monitor,
},
],
});
// Add one more process using PidmanGroup's addProcess method.
group.addProcess({
// A forced typo. This will produce an error.
command: "ls",
arguments: ['foo'],
monitor,
});
// attach group
pm.addProcessGroup(group);
// Let's run all these processes.
pm.run();
// And kill them at once after a few seconds.
setTimeout(async () => {
try {
const killed = await group.kill();
console.info(killed);
} catch (err) {
console.error(err);
}
}, 5000);