2022-10-07 16:43:04 -04:00
|
|
|
const { exec } = require("child_process");
|
2022-09-25 01:31:59 -04:00
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
class Server {
|
|
|
|
constructor(dir) {
|
|
|
|
this.rootDir = dir;
|
|
|
|
this.name = dir.split('/').at(-1);
|
|
|
|
this.pidFilePath = this.rootDir + '/pid.txt';
|
|
|
|
|
|
|
|
// read version file
|
|
|
|
var versionFilePath = this.rootDir + '/current_version.txt';
|
2022-10-05 00:12:50 -04:00
|
|
|
if (fs.existsSync(versionFilePath)) {
|
|
|
|
this.version = fs.readFileSync(versionFilePath, {encoding:'utf8', flag:'r'});
|
|
|
|
}
|
2022-09-25 01:31:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2022-10-07 16:43:04 -04:00
|
|
|
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.`);
|
|
|
|
});
|
2022-09-25 01:31:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
2022-10-07 16:43:04 -04:00
|
|
|
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
|
2022-09-25 01:31:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getPid() {
|
|
|
|
if (fs.existsSync(this.pidFilePath)) {
|
|
|
|
return fs.readFileSync(this.pidFilePath, {encoding:'utf8', flag:'r'})
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.Server = Server;
|