UNPKG

3.27 kBJavaScriptView Raw
1var prog = require('child_process')
2
3var cons = require('./console').Console
4
5var _colors = require('./colors')
6var colors_max = _colors.colors_max
7var colors = _colors.colors
8
9var os = require('os')
10var platform = os.platform()
11
12// Run a Specific Process
13// - Key is a Process Name and Number
14// - Proc is an object with the launch properties
15//
16// i.e. web=2 becomes the web.2 key
17function 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// Run a Specific Process Once using the ENV variables
55// from the .env file
56function 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// Figure Out What to Start Based on Procfile Processes
79// And Requirements Passed as Command Line Arguments
80//
81// e.g. web=2,api=3 are requirements
82function 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// Merge object b into object a
124function 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
133module.exports.start = start
134module.exports.run = run
135module.exports.once = once