-
Notifications
You must be signed in to change notification settings - Fork 9
/
pm2.js
44 lines (40 loc) · 946 Bytes
/
pm2.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
const pm2 = require('pm2')
const count = parseInt(process.argv[2])
const config = require('./dist/src/config.js')
const startingPort = config.port ?? 8080
console.log(`count: ${count} starting port:${startingPort}`)
pm2.connect(function (err) {
if (err) {
console.error(err)
process.exit(2)
}
for (let i = 0; i < count; i++) {
startRPC(startingPort + i)
}
setTimeout(() => {
console.log(`Run "pm2 list" to see started processes.`)
process.exit(0)
}, 3000)
return
})
function startRPC(port) {
const processName = 'rpc_' + port
pm2.start(
{
script: 'dist/src/server.js',
name: processName,
args: String(port),
},
function (err, apps) {
if (err) {
console.error(err)
pm2.restart(processName, (err, proc) => {
pm2.disconnect()
})
return
} else {
console.log(`Started RPC server at port ${port}`)
}
}
)
}