Advertisement
Superloup10

Untitled

May 25th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs");
  2. const childProcess = require("child_process");
  3. const fork = require("./debugFork");
  4. const paths = require("./paths");
  5.  
  6. const dirname = paths.baseDir;
  7.  
  8. function getRunningPid(callback) {
  9.     fs.readFile(paths.pidfile, {encoding: 'utf-8'}, (err, pid) => {
  10.         if (err)
  11.             return callback(err);
  12.         pid = parseInt(pid, 10);
  13.         try {
  14.            process.kill(pid, 0);
  15.            callback(null, pid);
  16.         }
  17.         catch (e) {
  18.             callback(e);
  19.         }
  20.     });
  21. }
  22.  
  23. function start(options) {
  24.     if (options.dev) {
  25.         process.env.NODE_ENV = 'development';
  26.         fork(paths.loader, ['--no-daemon', '--no-silent'], {
  27.             env: process.env,
  28.             cwd: dirname,
  29.             stdio: 'inherit'
  30.         });
  31.         return;
  32.     }
  33.     if (options.log) {
  34.         console.log('\n' + [
  35.             'Starting MFFBot with logging output'.bold,
  36.             'Hit '.red + 'CTRL-C'.bold + 'to exit'.red,
  37.             'The MFFBot will continue to run in the background',
  38.             'Use "' + './mffbot stop'.yellow + '" to stop the MFFBot'
  39.         ].join('\n'));
  40.     }
  41.     else if (!options.silent) {
  42.         console.log('\n' + [
  43.             'Starting MFFBot'.bold,
  44.             '\t"' + './mffbot stop'.yellow + '" to stop the MFFBot',
  45.             '\t"' + './mffbot log'.yellow + '" to view log output',
  46.             '\t"' + './mffbot help'.yellow + '" for more commands\n'.reset
  47.         ].join('\n'));
  48.     }
  49.  
  50.     // Spawn a new MFFBot process
  51.     const child = fork(paths.loader, process.argv.slice(3), {
  52.         env: process.env,
  53.         cwd: dirname
  54.     });
  55.     if (options.log) {
  56.         childProcess.spawn('tail', ['-F', './logs/output.log'], {
  57.             cwd: dirname,
  58.             stdio: 'inherit'
  59.         });
  60.     }
  61.  
  62.     return child;
  63. }
  64.  
  65. function stop() {
  66.     getRunningPid((err, pid) => {
  67.        if(!err) {
  68.            process.kill(pid, 'SIGTERM');
  69.            console.log('Stopping MFFBot. GoodBye!');
  70.        }
  71.        else {
  72.            console.log('MFFBot is already stopped.');
  73.        }
  74.     });
  75. }
  76.  
  77. function restart(options) {
  78.     getRunningPid((err, pid) => {
  79.         if(!err) {
  80.             console.log('\nRestarting MFFBot'.bold);
  81.             process.kill(pid, 'SIGTERM');
  82.             options.silent = true;
  83.             start(options);
  84.         }
  85.         else {
  86.             console.warn("MFFBot couldn't be restarted, as a running instance couldn't be found");
  87.         }
  88.     });
  89. }
  90.  
  91. function status() {
  92.     getRunningPid((err, pid) => {
  93.        if(!err) {
  94.            console.log('\n' + [
  95.                'MFFBot Running '.bold + ('(pid ' + pid.toString() + ')').cyan,
  96.                '\t"' + './mffbot stop'.yellow + '" to stop the MFFBot',
  97.                '\t"' + './mffbot log'.yellow + '" to view log output',
  98.                '\t"' + './mffbot restart'.yellow + '" to restart MFFBot'
  99.            ].join('\n'));
  100.        }
  101.        else {
  102.            console.log('\nMFFBot is not running'.bold);
  103.            console.log('\t"' + './mffbot start'.yellow + '" to launch the MFFBot\n'.reset)
  104.        }
  105.     });
  106. }
  107.  
  108. function log() {
  109.     console.log('\nHit '.red + 'CTRL-C'.bold + ' to exit\n'.red + '\n'.reset);
  110.     childProcess.spawn('tail', ['-F', './logs/output.log'], {
  111.         cwd: dirname,
  112.         stdio: 'inherit'
  113.     });
  114. }
  115.  
  116. exports.start = start;
  117. exports.stop = stop;
  118. exports.restart = restart;
  119. exports.status = status;
  120. exports.log = log;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement