1 | var prog = require('child_process')
|
2 |
|
3 | var cons = require('./console').Console
|
4 |
|
5 | var _colors = require('./colors')
|
6 | var colors_max = _colors.colors_max
|
7 | var colors = _colors.colors
|
8 |
|
9 | var os = require('os')
|
10 | var platform = os.platform()
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function run(key,proc,emitter) {
|
18 | var file, args;
|
19 | if (platform === 'win32') {
|
20 | file = process.env.comspec || 'cmd.exe';
|
21 | args = ['/s', '/c', proc.command];
|
22 | } else {
|
23 | file = '/bin/sh';
|
24 | args = ['-c', proc.command];
|
25 | }
|
26 | var child = prog.spawn(file, args, { env: proc.env });
|
27 |
|
28 | child.stdout.on('data',function(data){
|
29 | cons.log(key,proc,data.toString());
|
30 | });
|
31 |
|
32 | child.stderr.on('data',function(data){
|
33 | cons.log(key,proc,data.toString());
|
34 | });
|
35 |
|
36 | child.on('close',function(code){
|
37 | if(code==0){
|
38 | cons.info(key,proc,"Exited Successfully");
|
39 | }else{
|
40 | cons.Info(key,proc,"Exited Abnormally");
|
41 | }
|
42 | });
|
43 |
|
44 | child.on('exit',function(code, signal){
|
45 | emitter.emit('killall', signal);
|
46 | });
|
47 |
|
48 | emitter.on('killall',function(signal){
|
49 | child.kill(signal);
|
50 | });
|
51 |
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 | function once(input,envs,callback) {
|
57 | var file, args;
|
58 | var proc = {
|
59 | command : input,
|
60 | env : merge(merge({}, process.env), envs)
|
61 | };
|
62 |
|
63 | if (platform === 'win32') {
|
64 | file = process.env.comspec || 'cmd.exe';
|
65 | args = ['/s', '/c', proc.command];
|
66 | } else {
|
67 | file = '/bin/sh';
|
68 | args = ['-c', proc.command];
|
69 | }
|
70 |
|
71 | var child = prog.spawn(file, args, { env: proc.env, stdio: 'inherit' });
|
72 |
|
73 | child.on('close', function(code) {
|
74 | callback(code);
|
75 | });
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | function start(procs,requirements,envs,portarg,emitter){
|
83 |
|
84 | var j = 0;
|
85 | var k = 0;
|
86 | var port = parseInt(portarg);
|
87 |
|
88 | if(port<1024)
|
89 | return cons.Error('Only Proxies Can Bind to Privileged Ports - '+
|
90 | 'Try \'sudo nf start -x %s\'',port);
|
91 |
|
92 | for(key in requirements){
|
93 | var n = parseInt(requirements[key]);
|
94 |
|
95 | for(i=0;i<n;i++){
|
96 |
|
97 | var color_val = (j+k) % colors_max;
|
98 |
|
99 | if (!procs[key]){
|
100 | cons.Warn("Required Key '%s' Does Not Exist in Procfile Definition",key);
|
101 | continue;
|
102 | }
|
103 |
|
104 | var p = {
|
105 | command : procs[key],
|
106 | color : colors[color_val],
|
107 | env : merge(merge({}, process.env), envs)
|
108 | }
|
109 |
|
110 | p.env.PORT = port + j + k*100;
|
111 | p.env.FOREMAN_WORKER_NAME = p.env.FOREMAN_WORKER_NAME || key+"."+(i+1)
|
112 |
|
113 | run(key+"."+(i+1),p,emitter);
|
114 |
|
115 | j++;
|
116 |
|
117 | }
|
118 | j=0;
|
119 | k++;
|
120 | }
|
121 | }
|
122 |
|
123 |
|
124 | function merge(a, b) {
|
125 | if (a && b) {
|
126 | for (var key in b) {
|
127 | a[key] = b[key];
|
128 | }
|
129 | }
|
130 | return a;
|
131 | }
|
132 |
|
133 | module.exports.start = start
|
134 | module.exports.run = run
|
135 | module.exports.once = once
|