-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
release.js
184 lines (145 loc) · 3.99 KB
/
release.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
require('./index');
// Constants
const Fs = require('fs');
// Variables
var WATCHER = process.connected === true;
var options;
var app;
var firstinit = true;
var pidname;
var unexpectedexit = false;
var restarting;
module.exports = function(opt) {
options = opt;
// options.ip = '127.0.0.1';
// options.port = parseInt(process.argv[2]);
// options.unixsocket = require('path').join(require('os').tmpdir(), 'app_name');
// options.config = { name: 'Total.js' };
// options.https = { key: Fs.readFileSync('keys/agent2-key.pem'), cert: Fs.readFileSync('keys/agent2-cert.pem')};
// options.sleep = 3000;
// options.inspector = 9229;
// options.debugger = 40894;
// options.watch = ['adminer'];
// options.livereload = true;
// options.watcher = false;
// options.cluster = 'auto' || or NUMBER
// options.cluster_limit = 10;
// options.timeout = 5000;
// options.threads = '/api/' || or true or false;
// options.thread = 'thread_name';
// options.logs = 'isolated';
// options.edit = 'wss://.....com/?id=myprojectname'
if (!WATCHER)
WATCHER = process.argv.indexOf('--watcher') === -1 && !options.watcher;
};
function makestamp() {
return '--- # --- [ ' + new Date().format('yyyy-MM-dd HH:mm:ss') + ' ] ';
}
function restart() {
restarting && clearTimeout(restarting);
restarting = setTimeout(runapp, 100);
}
function runapp() {
restarting = null;
var Fork = require('child_process').fork;
var Fs = require('fs');
var Path = require('path');
var arr = CLONE(process.argv).slice(2);
var port = arr.pop();
var directory = process.cwd();
if (!firstinit)
arr.push('--restart');
port && arr.push(port);
var filename = U.getName(process.argv[1] || 'index.js');
pidname = Path.join(directory, filename.replace(/\.js$/, '.pid'));
app = Fork(Path.join(process.cwd(), filename), arr);
app.on('message', function(msg) {
switch (msg) {
case 'total:eaddrinuse':
process.exit(1);
break;
case 'total:ready':
if (firstinit) {
app.send('total:debug');
firstinit = false;
}
break;
case 'total:restart':
console.log(makestamp().replace('#', 'RES'));
unexpectedexit = true;
setTimeout(restart, 1000);
process.kill(app.pid);
app = null;
break;
}
});
Fs.writeFileSync(pidname, process.pid + '');
app.on('exit', function() {
// checks unexpected exit
if (unexpectedexit === true)
app = null;
else
restart();
unexpectedexit = false;
});
EMIT('watcher', app);
}
function init() {
if (options.cluster && !options.threads) {
var cluster = options.cluster;
delete options.cluster;
require('total4').cluster.http(cluster, 'release', options);
return;
}
if (WATCHER) {
delete options.watcher;
if (options.servicemode) {
LOAD(options.servicemode === true || options.servicemode === 1 ? '' : options.servicemode);
ON('ready', function() {
F.cache.init_timer(); // internal hack
F.$snapshot();
});
if (!process.connected)
F.console();
} else if (options.https)
HTTPS('release', options);
else
HTTP('release', options);
return;
}
var end = function() {
if (process.isending)
return;
process.isending = true;
Fs.unlink(pidname, NOOP);
if (app) {
unexpectedexit = true;
process.kill(app.pid);
app = null;
}
process.exit(0);
};
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1';
console.log('-------- RELEASE PID: ' + process.pid + ' (v' + F.version_header + ')');
process.on('uncaughtException', e => e.toString().indexOf('ESRCH') == -1 && console.log(e));
process.title = 'total: release';
process.on('SIGTERM', end);
process.on('SIGINT', end);
process.on('exit', end);
setInterval(function() {
Fs.stat(pidname, function(err) {
if (err) {
unexpectedexit = true;
process.kill(app.pid);
setTimeout(() => process.exit(0), 100);
}
});
}, 4000);
if (!process.connected && options.edit) {
require('./index');
require('./edit').init(options.edit.replace(/^http/, 'ws'));
setTimeout(runapp, 1000);
} else
setImmediate(runapp);
}
setTimeout(init, 50);