-
Notifications
You must be signed in to change notification settings - Fork 1
/
pm2_start_dev_server.js
132 lines (113 loc) · 3.44 KB
/
pm2_start_dev_server.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
/**
* pm2 启动服务器
* Created by lixiaodong on 16/12/12.
*/
var async = require('async');
var pm2 = require('pm2');
var game_env = process.argv[2];
var gameConfig,
isConnected,
pm2Apps={};
/**
* 根据配置env & port 动态生成app配置
* @param appName
* @param envName
* @param port
* @returns {*}
*/
function getAppConfig(appName,envName,port){
var appConfig ;
var apps = gameConfig.apps;
for(var i = 0 ; i < apps.length; i++){
if(apps[i].name == appName){
appConfig = apps[i];
break;
}
}
if(appConfig){
appConfig = JSON.stringify(appConfig);
appConfig = appConfig.replace(/%GAME_ENV/g,envName);
appConfig = appConfig.replace(/%PORT/g,port);
appConfig = JSON.parse(appConfig);
}
return appConfig;
}
async.waterfall([
function (cb) {
if (!game_env) {
return cb('missing env');
}
gameConfig = require('./game_dev.json');
var appConfig = gameConfig && gameConfig.targets && gameConfig.targets[game_env];
var serverPort = appConfig.server;
if(!serverPort || !serverPort.length){
return cb('server port missing');
}
//multi server port
for(var i = 0 ; i < serverPort.length; i++){
var port = serverPort[i];
var app = getAppConfig('server-%GAME_ENV-%PORT',game_env,port);
pm2Apps[app.name] = app;
}
console.log('pm2Apps>>>>>',pm2Apps);
cb();
},
function (cb) {
pm2.connect(function (err) {
if(!err){
isConnected = true;
}
cb(err);
});
},
function (cb) {
pm2.list(function (err, processDescriptionList) {
//正在运行的pm2 app
var appNamesRunning = {};
if (!!processDescriptionList) {
for (var i = 0; i < processDescriptionList.length; i++) {
var runningAppName = processDescriptionList[i].name;
appNamesRunning[runningAppName] = 1;
}
}
var appNames = Object.keys(appNamesRunning);
console.log('appNames running:', appNames);
var appNamesToStart = Object.keys(pm2Apps);
var appNamesWillStop = [];
for(var i = 0 ; i < appNames.length; i++){
if(appNamesToStart.indexOf(appNames[i]) != -1){
appNamesWillStop.push(appNames[i]);
}
}
console.log('appNamesWillStop>>>>>',appNamesWillStop);
async.mapSeries(appNamesWillStop,function (app,call) {
pm2.stop(app,function (err) {
console.log('pm2 stop app:' + app + 'err:'+err);
call();
});
},function (err) {
cb(err);
});
});
},
function (cb) {
var appNamesToStart = Object.keys(pm2Apps);
async.mapSeries(appNamesToStart,function (app,call) {
pm2.start(pm2Apps[app],function (err) {
console.log('pm2 start app:' + app + 'err:'+err);
call();
});
},function (err) {
cb(err);
})
}
],function (err) {
if(isConnected){
console.log('pm2 disconnect');
pm2.disconnect();
}
if(!!err){
process.exit(2);
}
console.log('pm2 start server complete!');
});