total5 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/release.js ADDED
@@ -0,0 +1,167 @@
1
+ require('./index');
2
+
3
+ // Variables
4
+ var WATCHER = process.connected === true;
5
+ var options;
6
+ var app;
7
+ var firstinit = true;
8
+ var pidname;
9
+ var unexpectedexit = false;
10
+ var restarting;
11
+
12
+ module.exports = function(opt) {
13
+
14
+ options = opt || {};
15
+
16
+ // options.ip = '127.0.0.1';
17
+ // options.port = parseInt(process.argv[2]);
18
+ // options.unixsocket = require('node:path').join(require('node:os').tmpdir(), 'app_name');
19
+ // options.config = { name: 'Total.js' };
20
+ // options.https = { key: Fs.readFileSync('keys/agent2-key.pem'), cert: Fs.readFileSync('keys/agent2-cert.pem')};
21
+ // options.sleep = 3000;
22
+ // options.inspector = 9229;
23
+ // options.debugger = 40894;
24
+ // options.watch = ['adminer'];
25
+ // options.livereload = true;
26
+ // options.watcher = false;
27
+ // options.cluster = 'auto' || or NUMBER
28
+ // options.limit = 10;
29
+ // options.timeout = 5000;
30
+ // options.edit = 'wss://.....com/?id=myprojectname'
31
+
32
+ if (!WATCHER)
33
+ WATCHER = process.argv.indexOf('--watcher') === -1 && !options.watcher;
34
+
35
+ };
36
+
37
+ function makestamp() {
38
+ return '--- # --- [ ' + new Date().format('yyyy-MM-dd HH:mm:ss') + ' ] ';
39
+ }
40
+
41
+ function restart() {
42
+ restarting && clearTimeout(restarting);
43
+ restarting = setTimeout(run, 100);
44
+ }
45
+
46
+ function run() {
47
+
48
+ restarting = null;
49
+
50
+ var arr = F.TUtils.clone(process.argv).slice(2);
51
+ var port = arr.pop();
52
+ var directory = process.cwd();
53
+
54
+ if (!firstinit)
55
+ arr.push('--restart');
56
+
57
+ port && arr.push(port);
58
+ var filename = F.TUtils.getName(process.argv[1] || 'index.js');
59
+
60
+ pidname = F.Path.join(directory, filename.replace(/\.js$/, '.pid'));
61
+ app = F.Fork(F.Path.join(process.cwd(), filename), arr);
62
+ app.on('message', function(msg) {
63
+ switch (msg) {
64
+ case 'total:eaddrinuse':
65
+ process.exit(1);
66
+ break;
67
+ case 'total:ready':
68
+ if (firstinit) {
69
+ app.send('total:debug');
70
+ firstinit = false;
71
+ }
72
+ break;
73
+ case 'total:restart':
74
+ console.log(makestamp().replace('#', 'RES'));
75
+ unexpectedexit = true;
76
+ setTimeout(restart, 1000);
77
+ process.kill(app.pid);
78
+ app = null;
79
+ break;
80
+ }
81
+ });
82
+
83
+ F.Fs.writeFileSync(pidname, process.pid + '');
84
+
85
+ app.on('exit', function() {
86
+
87
+ // checks unexpected exit
88
+ if (unexpectedexit === true)
89
+ app = null;
90
+ else
91
+ restart();
92
+
93
+ unexpectedexit = false;
94
+ });
95
+
96
+ F.emit('watcher', app);
97
+ }
98
+
99
+ function init() {
100
+
101
+ if (options.cluster) {
102
+ options.release = true;
103
+ options.count = options.cluster;
104
+ F.TCluster.http(options);
105
+ return;
106
+ }
107
+
108
+ if (WATCHER) {
109
+
110
+ delete options.watcher;
111
+
112
+ if (options.servicemode) {
113
+ var types = options.servicemode === true || options.servicemode === 1 ? '' : options.servicemode.split(',').trim();
114
+ global.DEBUG = false;
115
+ F.load(types);
116
+ if (!process.connected)
117
+ F.console();
118
+ } else {
119
+ global.DEBUG = false;
120
+ F.http(options);
121
+ }
122
+ return;
123
+ }
124
+
125
+ var end = function() {
126
+
127
+ if (process.isending)
128
+ return;
129
+
130
+ process.isending = true;
131
+ F.Fs.unlink(pidname, NOOP);
132
+
133
+ if (app) {
134
+ unexpectedexit = true;
135
+ process.kill(app.pid);
136
+ app = null;
137
+ }
138
+
139
+ process.exit(0);
140
+ };
141
+
142
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1';
143
+ console.log('-------- RELEASE PID: ' + process.pid + ' (v' + F.version_header + ')');
144
+ process.on('uncaughtException', e => e.toString().indexOf('ESRCH') == -1 && console.log(e));
145
+ process.title = 'total: release';
146
+ process.on('SIGTERM', end);
147
+ process.on('SIGINT', end);
148
+ process.on('exit', end);
149
+
150
+ setInterval(function() {
151
+ F.Fs.stat(pidname, function(err) {
152
+ if (err) {
153
+ unexpectedexit = true;
154
+ process.kill(app.pid);
155
+ setTimeout(() => process.exit(0), 100);
156
+ }
157
+ });
158
+ }, 4000);
159
+
160
+ if (!process.connected && options.edit) {
161
+ require('./edit').init(options.edit.replace(/^http/, 'ws'));
162
+ setTimeout(run, 1000);
163
+ } else
164
+ setImmediate(run);
165
+ }
166
+
167
+ setTimeout(init, 50);