-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
175 lines (138 loc) · 4.61 KB
/
index.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
const util = require('util');
const helper = require('./lib/helper');
const logger = require('./lib/logger');
module.exports = class Command {
constructor() {
this.helper = require('./lib/helper');
const env = process.env;
const { PORT, INSTANCES } = env;
this.poc_argvs = {
command: process.execPath || 'node',
baseDir: process.cwd(), // 运行目录
instances: +INSTANCES || require('os').cpus().length, // worker数量
// port: +PORT || 7001, // 启动端口
framework: 'egg',
title: '',
isDaemon: false, // 守护进程模式
logDir: null,
ignoreStdErr: false
};
this.commander = null;
}
async start() {
this.commander = require('./lib/commander');
this._stop();
this._reload();
this._startOrReload();
this._start();
this.commander.parse(process.argv);
}
setInstances(command) {
command.option('-i, --instances <n>', 'max workers num of cluster mode', (num = 1) => {
const instances = +num;
if(isNaN(instances) || instances <= 0) {
instances = 1;
}
this.poc_argvs.instances = instances;
});
return this;
}
setPort(command) {
command.option('-p, --port <n>', 'set egg app listening port', (port = 7001) => {
this.poc_argvs.port = +port;
});
return this;
}
setTitle(command) {
command.option('-t, --title <t>', 'set application title', title => {
this.poc_argvs.title = title;
});
return this;
}
setIsDaemon(command) {
command.option('-d, --daemon', 'open daemon', () => {
this.poc_argvs.isDaemon = true;
});
return this;
}
setLogDir(command) {
command.option('-l, --logdir <d>', 'log dir', dir => {
this.poc_argvs.logDir = dir;
});
return this;
}
setBaseDir(command) {
command.option('-b, --basedir <d>', 'set egg application base dir', dir => {
this.poc_argvs.baseDir = dir;
});
return this;
}
setIgnoreStdErr(command) {
command.option('-ig, --ignore-stderr', 'ignore std err', () => {
this.poc_argvs.ignoreStdErr = true;
});
return this;
}
_start() {
const command = this.commander.command('start');
this.setInstances(command)
.setPort(command)
.setTitle(command)
.setIsDaemon(command)
.setLogDir(command)
.setIgnoreStdErr(command)
.setBaseDir(command);
command.action(async () => {
await require('./lib/stop').stop(this.poc_argvs);
await require('./lib/start').start(this.poc_argvs);
})
}
_stop() {
const command = this.commander.command('stop');
this.setTitle(command);
command.action(()=>{
require('./lib/stop').stop(this.poc_argvs);
});
}
_reload() {
const command = this.commander.command('reload');
this.setTitle(command);
command.action(()=>{
require('./lib/reload').reload(this.poc_argvs);
});
}
_startOrReload() {
const command = this.commander.command('startOrReload');
this.setInstances(command)
.setPort(command)
.setTitle(command)
.setIsDaemon(command)
.setLogDir(command)
.setIgnoreStdErr(command)
.setBaseDir(command);
command.action(async () => {
const argvs = this.poc_argvs;
const { title } = argvs;
if(!title) {
logger.error('title option is required');
process.exit(1);
}
const processList = await helper.findNodeProcess(item => {
const cmd = item.cmd;
return title ?
cmd.includes('start-cluster') && cmd.includes(util.format(helper.osRelated.titleTemplate, title)) :
cmd.includes('start-cluster');
});
let pids = processList.map(x => x.pid);
if(pids.length) {
logger.info('reload egg app: %s', title);
await require('./lib/reload').reload(argvs);
}
else {
logger.info('start egg app: %s', title);
await require('./lib/start').start(argvs);
logger.info(`[${title}] success started`);
}
});
}
}