-
Notifications
You must be signed in to change notification settings - Fork 1
/
serverCommands.js
134 lines (121 loc) · 3.78 KB
/
serverCommands.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
const pm2 = require('pm2'),
path = require('path'),
dotProp = require('dot-prop'),
serverName = 'Pianoserver',
launcherName = 'Pianoserver_Launcher',
defaultPort = 8081,
defaultSubdomain = 'pianoserver',
config = require('./config')
function startServer(subdomain, port) {
return new Promise((resolve, reject) => {
pm2.connect(function (err) {
if (err) {
reject('An error occured attempting to start the server, please try again...')
return
}
pm2.start({
name: serverName,
script: path.resolve(__dirname, 'index.js'), // Script to be run
args: [port || defaultPort, subdomain || defaultSubdomain],
}, (err, apps) => {
pm2.disconnect()
if (err) {
reject(false)
} else {
resolve(true)
}
})
})
})
}
function restartServer() {
return new Promise((resolve, reject) => {
pm2.connect(function (err) {
if (err) {
reject("An error occured attempting to restart the server, please try again...")
return
}
config.set('willRestart', true)
pm2.gracefulReload(serverName, (err, desc) => {
if (err) {
reject(false)
}
pm2.disconnect()
resolve(true)
})
})
})
}
function quitServer() {
return new Promise((resolve, reject) => {
pm2.connect(function (err) {
if (err) {
reject("An error occured attempting to quit the server, please try again...")
return
}
config.set('willRestart', false)
pm2.stop(serverName, (err, desc) => {
if (err) {
reject(false)
}
pm2.disconnect()
resolve(true)
})
})
})
}
function checkIfRunning(cb, which) {
const { notRunning = () => false, running = () => true } = (arguments.length==1?{}:cb || {})
return new Promise((resolve, reject) => {
pm2.connect(function (err) {
if (err) {
notRunning()
reject(err)
return
}
pm2.describe(which || serverName, (err, descriptions) => {
pm2.disconnect()
const stoppedWhen = ['stopped', 'errored', 'stopping']
if (err || descriptions.length === 0 || stoppedWhen.includes(dotProp.get(descriptions, '0.pm2_env.status'))) {
notRunning()
reject(false)
return
}
running()
resolve(true)
})
})
})
}
function startLauncher(port) {
return new Promise((resolve, reject) => {
pm2.connect(function (err) {
if (err) {
reject('An error occured attempting to start the server, please try again...')
return
}
pm2.start({
name: launcherName,
script: path.resolve(__dirname, 'launcher.js'),
args: [port || 8082],
}, function (err, apps) {
pm2.disconnect()
if (err) {
reject(err)
} else {
resolve(true)
}
})
})
})
}
module.exports = {
serverName: serverName,
quitServer: quitServer,
restartServer: restartServer,
startServer: startServer,
checkIfRunning: checkIfRunning,
startLauncher: startLauncher,
launcherName: launcherName,
defaultPort: defaultPort
}