Added ability to start a server
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
const { exec } = require("child_process");
|
||||
const fs = require('fs');
|
||||
|
||||
class Server {
|
||||
@ -11,21 +12,44 @@ class Server {
|
||||
if (fs.existsSync(versionFilePath)) {
|
||||
this.version = fs.readFileSync(versionFilePath, {encoding:'utf8', flag:'r'});
|
||||
}
|
||||
|
||||
// set server state
|
||||
if (fs.existsSync(this.pidFilePath)) {
|
||||
this.state = true;
|
||||
} else {
|
||||
this.state = false;
|
||||
}
|
||||
}
|
||||
|
||||
start() {
|
||||
console.log('Starting server...');
|
||||
console.log(`Starting server ${this.name}...`);
|
||||
|
||||
// create shell command and execute it
|
||||
let cmd = `${this.rootDir}/start.sh`;
|
||||
exec(cmd, (err, stdout, stderr) => {
|
||||
if (err) {
|
||||
console.log(`Error while starting server: ${error.message}`);
|
||||
}
|
||||
|
||||
console.log(`Server ${this.name} has been started.`);
|
||||
});
|
||||
}
|
||||
|
||||
stop() {
|
||||
console.log('Stopping server...');
|
||||
console.log(`Stopping server ${this.name}...`);
|
||||
}
|
||||
|
||||
getStatus() {
|
||||
if (fs.existsSync(this.pidFilePath)) {
|
||||
let pid = fs.readFileSync(this.pidFilePath, {encoding: 'utf8', flag: 'r'});
|
||||
try {
|
||||
return process.kill(pid, 0);
|
||||
} catch (err) {
|
||||
// ESRCH error means the process doesn't exist, so should return false
|
||||
if (err.code !== 'ESRCH') {
|
||||
console.error(err);
|
||||
return err.code === 'EPERM';
|
||||
} else {
|
||||
// delete pid.txt so we don't have to worry about checking it again
|
||||
fs.unlinkSync(this.pidFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
getPid() {
|
||||
|
Reference in New Issue
Block a user