Skip to content

Commit 1c11fe8

Browse files
committed
bug fix: server restart not working
1 parent 0080d68 commit 1c11fe8

4 files changed

Lines changed: 62 additions & 29 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ $ node src/main.js server.json
4141
```
4242

4343
**Note:** To configure your Discord bot, follow these two guides: [Setting up a bot application](https://discordjs.guide/preparations/setting-up-a-bot-application.html) and [Adding your bot to servers](https://discordjs.guide/preparations/adding-your-bot-to-servers.html).
44-
**Note2:** To run multiple servers with the same base and FXAdmin installation, just duplicate your config.json and change the ports. Two instances of FXAdmin cannot be running in the same web server port.
44+
**Note2:** To run multiple servers with the same base and FXAdmin installation, just duplicate your `server.json` and change the ports. Two instances of FXAdmin cannot be running in the same web server port.
4545

4646

4747
## Troubleshooting
4848
- If you are getting `Wrong password!` when executing an action, make sure you have your admins file configured correctly. If there is anything wrong with the file you should get an error when starting FXAdmin.
49-
- If you are getting `Server error: timeout of 1000ms exceeded` it means the fxserver is offline, start it in the web panel.
49+
- If you are getting `[FXAdmin:Monitor] Server error: ` of timeout or connection refused, it means the fxserver is offline, start it in the web panel.
5050
- If you are having trouble starting the fxserver via fxadmin, run `node src/config-tester.js server.json` and see which test is failing.
5151
- If you run into problems when executing `npm install`, try `npm i node-gyp` or `npm i --global --production windows-build-tools` if you are on windows.
5252

src/components/fxRunner.js

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,94 @@ module.exports = class FXRunner {
1111
this.config = config;
1212
this.fxChild = null;
1313
this.fxChildStatus = null;
14+
this.spawnVariables = null;
1415
this.outData = '';
1516
this.enableBuffer = false;
16-
if(config.autostart) this.spawnServer();
17+
this.setupVariables();
18+
19+
//The setTimeout is not strictly necessary, but it's nice to have other errors in the top before fxserver starts.
20+
if(config.autostart){
21+
setTimeout(() => {
22+
this.spawnServer();
23+
}, 1000);
24+
}
1725
}
1826

19-
27+
2028
//================================================================
2129
/**
22-
* Spawns the FXServer and sets up all the event handlers
30+
* Setup the spawn variables
2331
*/
24-
async spawnServer(){
25-
if(this.fxChild !== null) return false;
26-
cleanTerminal();
32+
setupVariables(){
2733
let onesyncFlag = (this.config.onesync)? '+set onesync_enabled 1' : '';
28-
let spawnShell = null;
29-
let spawnCmdArgs = null;
3034
if(this.config.isLinux){
31-
spawnShell = '/bin/bash';
32-
spawnCmdArgs = [`${this.config.buildPath}/run.sh`, `${onesyncFlag} +exec ${this.config.cfgPath}`];
35+
this.spawnVariables = {
36+
shell: '/bin/bash',
37+
cmdArgs: [`${this.config.buildPath}/run.sh`, `${onesyncFlag} +exec ${this.config.cfgPath}`]
38+
};
3339
}else{
34-
spawnShell = 'cmd.exe';
35-
spawnCmdArgs = ['/c', `${this.config.buildPath}/run.cmd ${onesyncFlag} +exec ${this.config.cfgPath}`];
40+
this.spawnVariables = {
41+
shell: 'cmd.exe',
42+
cmdArgs: ['/c', `${this.config.buildPath}/run.cmd ${onesyncFlag} +exec ${this.config.cfgPath}`]
43+
};
44+
}
45+
46+
}//Final xxxx()
47+
48+
49+
//================================================================
50+
/**
51+
* Spawns the FXServer and sets up all the event handlers
52+
*/
53+
async spawnServer(){
54+
//Sanity Check
55+
if(
56+
this.spawnVariables == null ||
57+
typeof this.spawnVariables.shell == 'undefined' ||
58+
typeof this.spawnVariables.cmdArgs == 'undefined'
59+
){
60+
logError('this.spawnVariables is not set.', context);
61+
return false;
62+
}
63+
if(this.fxChild !== null){
64+
logError('this.fxChild is not null.', context);
65+
return false;
3666
}
3767

68+
//Starting server
3869
try {
3970
this.fxChild = spawn(
40-
spawnShell,
41-
spawnCmdArgs,
71+
this.spawnVariables.shell,
72+
this.spawnVariables.cmdArgs,
4273
{cwd: this.config.basePath}
4374
);
75+
logOk(`::Server started with PID ${this.fxChild.pid}!`, context);
4476
} catch (error) {
4577
logError('Failed to start FXServer with the following error:');
4678
dir(error);
4779
process.exit(0);
4880
}
4981

50-
logOk(`::Server started with PID ${this.fxChild.pid}!`, context);
82+
//Pipping stdin and stdout
5183
this.fxChild.stdout.pipe(process.stdout);
5284
process.stdin.pipe(this.fxChild.stdin);
5385

86+
//Setting up event handlers
5487
this.fxChild.on('close', function (code, signal) {
55-
console.log('close: ' + `code ${code} and signal ${signal}`);
88+
logWarn('close: ' + `code ${code} and signal ${signal}`, context);
5689
});
5790
this.fxChild.on('disconnect', function () {
58-
console.log('disconnect');
91+
logWarn('fxChild disconnect event', context);
5992
});
6093
this.fxChild.on('error', function (err) {
61-
console.log('error ', err);
94+
logWarn('fxChild error event:', context);
95+
dir(err)
6296
});
6397
this.fxChild.on('exit', function (code, signal) {
64-
logError('this.fxChild process exited with ' + `code ${code} and signal ${signal}`, context);
65-
// console.log("==========================");
66-
// console.log(JSON.stringify(this.fxChild));
67-
// console.log("==========================");
98+
logWarn('fxChild process exited with ' + `code ${code} and signal ${signal}`, context);
6899
});
69100
this.fxChild.stderr.on('data', (data) => {
70-
console.error(`==============================================:\n${data}\n==============================================`);
101+
logWarn(`========:\n${data}\n========`, context);
71102
});
72103
this.fxChild.stdout.on('data', (data) => {
73104
if(this.enableBuffer) this.outData += data;
@@ -93,9 +124,11 @@ module.exports = class FXRunner {
93124
killServer(){
94125
try {
95126
this.fxChild.kill();
127+
this.fxChild = null;
96128
return true;
97129
} catch (error) {
98-
logWarn("Couldn't kill the server. Perhaps What Is Dead May Never Die.")
130+
logWarn("Couldn't kill the server. Perhaps What Is Dead May Never Die.");
131+
this.fxChild = null;
99132
return false;
100133
}
101134
}

src/components/monitor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module.exports = class Monitor {
6868
players = res.data;
6969
if(!Array.isArray(players)) throw new Error("not array")
7070
} catch (error) {
71-
logError(`Server error: ${error.message}`, context);
71+
logWarn(`Server error: ${error.message}`, context);
7272
this.statusServer = {
7373
online: false,
7474
ping: false,

version.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"version": "0.5.0",
3-
"changelog": "Linux support, Config tester."
2+
"version": "0.5.1",
3+
"changelog": "Linux support, Config tester. Bug fix: server restart"
44
}

0 commit comments

Comments
 (0)