Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require("fs");
- const childProcess = require("child_process");
- const fork = require("./debugFork");
- const paths = require("./paths");
- const dirname = paths.baseDir;
- function getRunningPid(callback) {
- fs.readFile(paths.pidfile, {encoding: 'utf-8'}, (err, pid) => {
- if (err)
- return callback(err);
- pid = parseInt(pid, 10);
- try {
- process.kill(pid, 0);
- callback(null, pid);
- }
- catch (e) {
- callback(e);
- }
- });
- }
- function start(options) {
- if (options.dev) {
- process.env.NODE_ENV = 'development';
- fork(paths.loader, ['--no-daemon', '--no-silent'], {
- env: process.env,
- cwd: dirname,
- stdio: 'inherit'
- });
- return;
- }
- if (options.log) {
- console.log('\n' + [
- 'Starting MFFBot with logging output'.bold,
- 'Hit '.red + 'CTRL-C'.bold + 'to exit'.red,
- 'The MFFBot will continue to run in the background',
- 'Use "' + './mffbot stop'.yellow + '" to stop the MFFBot'
- ].join('\n'));
- }
- else if (!options.silent) {
- console.log('\n' + [
- 'Starting MFFBot'.bold,
- '\t"' + './mffbot stop'.yellow + '" to stop the MFFBot',
- '\t"' + './mffbot log'.yellow + '" to view log output',
- '\t"' + './mffbot help'.yellow + '" for more commands\n'.reset
- ].join('\n'));
- }
- // Spawn a new MFFBot process
- const child = fork(paths.loader, process.argv.slice(3), {
- env: process.env,
- cwd: dirname
- });
- if (options.log) {
- childProcess.spawn('tail', ['-F', './logs/output.log'], {
- cwd: dirname,
- stdio: 'inherit'
- });
- }
- return child;
- }
- function stop() {
- getRunningPid((err, pid) => {
- if(!err) {
- process.kill(pid, 'SIGTERM');
- console.log('Stopping MFFBot. GoodBye!');
- }
- else {
- console.log('MFFBot is already stopped.');
- }
- });
- }
- function restart(options) {
- getRunningPid((err, pid) => {
- if(!err) {
- console.log('\nRestarting MFFBot'.bold);
- process.kill(pid, 'SIGTERM');
- options.silent = true;
- start(options);
- }
- else {
- console.warn("MFFBot couldn't be restarted, as a running instance couldn't be found");
- }
- });
- }
- function status() {
- getRunningPid((err, pid) => {
- if(!err) {
- console.log('\n' + [
- 'MFFBot Running '.bold + ('(pid ' + pid.toString() + ')').cyan,
- '\t"' + './mffbot stop'.yellow + '" to stop the MFFBot',
- '\t"' + './mffbot log'.yellow + '" to view log output',
- '\t"' + './mffbot restart'.yellow + '" to restart MFFBot'
- ].join('\n'));
- }
- else {
- console.log('\nMFFBot is not running'.bold);
- console.log('\t"' + './mffbot start'.yellow + '" to launch the MFFBot\n'.reset)
- }
- });
- }
- function log() {
- console.log('\nHit '.red + 'CTRL-C'.bold + ' to exit\n'.red + '\n'.reset);
- childProcess.spawn('tail', ['-F', './logs/output.log'], {
- cwd: dirname,
- stdio: 'inherit'
- });
- }
- exports.start = start;
- exports.stop = stop;
- exports.restart = restart;
- exports.status = status;
- exports.log = log;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement