-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyno.js
101 lines (79 loc) · 2.6 KB
/
dyno.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var path = require('path'),
spawn = require('child_process').spawn,
net = require('net'),
httpd = require('./httpd');
exports.dynos = [];
var Dyno = function (app, type) {
this.app = app;
this.type = type;
this.id = app.nextDynoId(type);
this.name = app.name + '[' + type + '.' + this.id + ']';
app.dynos.push(this);
exports.dynos.push(this);
this.log('Created');
};
Dyno.prototype.log = function (line) {
var stamp = new Date();
var hour = stamp.getHours();
var min = stamp.getMinutes();
var sec = stamp.getSeconds();
if (min < 10) min = '0' + min;
if (sec < 10) sec = '0' + sec;
console.log([hour, min, sec].join(':') + ' ' + this.name + ': ' + line);
};
Dyno.prototype.run = function (argv, callback) {
var env = JSON.parse(JSON.stringify(this.app.config.env));
env.APP_NAME = this.app.name;
env.PORT = this.port;
//env.COMMIT_HASH = 1234567;
//env.DATABASE_URL = "type://user:pass@host/database";
//env.LAST_GIT_BY =
env.STACK = "app-manager-0.0.1";
//env.URL = "http://localhost:7200/" + slug;
this.command = this.app.config.procs[this.type];
this.command = this.command.replace("$PORT", env.PORT);
this.command = this.command.replace("$RAILS_ENV", env.RAILS_ENV);
this.command = this.command.replace("$RACK_ENV", env.RAILS_ENV);
if (argv) this.command += " " + argv;
this.command = this.command.split(" ");
var opts = {cwd: this.app.slug, env: env};
this.proc = spawn(this.command[0], this.command.slice(1), opts);
this.log('Starting');
var self = this;
this.proc.on("exit", function () {
self.log('Crashed; removing from list of running dynos');
self.app.dynos.splice(self.app.dynos.indexOf(self), 1);
});
this.proc.stdout.on("data", function (data) {
data.trimRight().split('\n').forEach(function (line) {
self.log(line);
});
}).setEncoding("utf8");
this.proc.stderr.on("data", function (data) {
data.trimRight().split('\n').forEach(function (line) {
self.log(line);
});
}).setEncoding("utf8");
// wait for the dyno to listen
var check = function () {
var sock = net.connect(env.PORT, 'localhost', function () {
sock.end();
self.log('Bound to port ' + env.PORT);
callback && callback();
}).on('error',function () {
setTimeout(check, 250);
});
};
check();
}
exports.start = function (app, proc, params, callback) {
var dyno = new Dyno(app, proc);
if (proc == 'web')
httpd.reservePort(dyno);
dyno.run(params, callback);
};
exports.killAll = function () {
exports.dynos.forEach(function (dyno) {
dyno.proc.kill('SIGTERM');
});
};