Skip to content

Commit

Permalink
Add CLI test for starting/stopping from a directory with space (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Mar 1, 2020
1 parent 437ca4a commit 58eb131
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 85 deletions.
159 changes: 78 additions & 81 deletions lib/forever.js
Expand Up @@ -6,21 +6,21 @@
*
*/

var fs = require('fs'),
path = require('path'),
events = require('events'),
exec = require('child_process').exec,
spawn = require('child_process').spawn,
cliff = require('cliff'),
nconf = require('nconf'),
nssocket = require('nssocket'),
utile = require('utile'),
winston = require('winston'),
mkdirp = require('mkdirp'),
async = require('async'),
configUtils = require('./util/config-utils');

var forever = exports;
const fs = require("fs"),
path = require("path"),
events = require("events"),
exec = require("child_process").exec,
spawn = require("child_process").spawn,
cliff = require("cliff"),
nconf = require("nconf"),
nssocket = require("nssocket"),
utile = require("utile"),
winston = require("winston"),
mkdirp = require("mkdirp"),
async = require("async"),
configUtils = require("./util/config-utils");

const forever = exports;

//
// Setup `forever.log` to be a custom `winston` logger.
Expand Down Expand Up @@ -68,7 +68,7 @@ exports.version = require('../package').version;
// then it is created using `mkdirp`.
//
function getSockets(sockPath, callback) {
var sockets;
let sockets;

try {
sockets = fs.readdirSync(sockPath);
Expand All @@ -92,11 +92,11 @@ function getSockets(sockPath, callback) {
// Returns all data for processes managed by forever.
//
function getAllProcesses(callback) {
var sockPath = forever.config.get('sockPath');
const sockPath = forever.config.get("sockPath");

function getProcess(name, next) {
var fullPath = path.join(sockPath, name),
socket = new nssocket.NsSocket();
let fullPath = path.join(sockPath, name);
const socket = new nssocket.NsSocket();

if (process.platform === 'win32') {
// it needs the prefix
Expand Down Expand Up @@ -164,10 +164,10 @@ function getAllPids(processes) {
// Returns emitter that you can use to handle events on failure or success (i.e 'error' or <event>)
//
function stopOrRestart(action, event, format, target) {
var emitter = new events.EventEmitter();
const emitter = new events.EventEmitter();

function sendAction(proc, next) {
var socket = new nssocket.NsSocket();
const socket = new nssocket.NsSocket();

function onMessage(data) {
//
Expand All @@ -181,8 +181,8 @@ function stopOrRestart(action, event, format, target) {
// Messages are only sent back from error cases. The event
// calling context is available from `nssocket`.
//
var message = data && data.message,
type = this.event.slice().pop();
const message = data && data.message,
type = this.event.slice().pop();

//
// Remark (Tjatse): This message comes from `forever-monitor`, the process is marked
Expand Down Expand Up @@ -223,7 +223,7 @@ function stopOrRestart(action, event, format, target) {
});
}

var procs;
let procs;
if (target !== undefined && target !== null) {
if (isNaN(target)) {
procs = forever.findByScript(target, processes);
Expand Down Expand Up @@ -355,7 +355,7 @@ forever.load = function (options) {
// Sets up debugging for this forever process
//
forever._debug = function () {
var debug = forever.config.get('debug');
const debug = forever.config.get("debug");

if (!debug) {
forever.config.set('debug', true);
Expand All @@ -381,7 +381,7 @@ forever.load();
// the target script does exist before executing callback.
//
forever.stat = function (logFile, script, callback) {
var logAppend;
let logAppend;

if (arguments.length === 4) {
logAppend = callback;
Expand Down Expand Up @@ -419,7 +419,7 @@ forever.start = function (script, options) {
//
// Create the monitor, log events, and start.
//
var monitor = new forever.Monitor(script, options);
const monitor = new forever.Monitor(script, options);
forever.logEvents(monitor);
return monitor.start();
};
Expand All @@ -436,19 +436,17 @@ forever.startDaemon = function (script, options) {
options.logFile = forever.logFilePath(options.logFile || forever.config.get('logFile') || options.uid + '.log');
options.pidFile = forever.pidFilePath(options.pidFile || forever.config.get('pidFile') || options.uid + '.pid');

var monitor, outFD, errFD, monitorPath;

//
// This log file is forever's log file - the user's outFile and errFile
// options are not taken into account here. This will be an aggregate of all
// the app's output, as well as messages from the monitor process, where
// applicable.
//
outFD = fs.openSync(options.logFile, 'a');
errFD = fs.openSync(options.logFile, 'a');
monitorPath = path.resolve(__dirname, '..', 'bin', 'monitor');
const outFD = fs.openSync(options.logFile, 'a');
const errFD = fs.openSync(options.logFile, 'a');
const monitorPath = path.resolve(__dirname, '..', 'bin', 'monitor');

monitor = spawn(process.execPath, [monitorPath, script], {
const monitor = spawn(process.execPath, [monitorPath, script], {
stdio: ['ipc', outFD, errFD],
detached: true
});
Expand Down Expand Up @@ -481,9 +479,9 @@ forever.startDaemon = function (script, options) {
// **NOTE:** This will change your `process.title`.
//
forever.startServer = function () {
var args = Array.prototype.slice.call(arguments),
monitors = [],
callback;
const args = Array.prototype.slice.call(arguments);
let monitors = [],
callback;

args.forEach(function (a) {
if (Array.isArray(a)) {
Expand All @@ -500,9 +498,9 @@ forever.startServer = function () {
});

async.map(monitors, function (monitor, next) {
var worker = new forever.Worker({
const worker = new forever.Worker({
monitor: monitor,
sockPath: forever.config.get('sockPath'),
sockPath: forever.config.get("sockPath"),
exitOnStop: true
});

Expand Down Expand Up @@ -592,47 +590,48 @@ forever.tail = function (target, options, callback) {
options.stream = false;
}

var that = this,
length = options.length || forever.config.get('loglength'),
stream = options.stream || forever.config.get('logstream'),
blanks = function (e, i, a) { return e !== ''; },
title = function (e, i, a) { return e.match(/^==>/); },
args = ['-n', length],
logs;
const that = this,
length = options.length || forever.config.get("loglength"),
stream = options.stream || forever.config.get("logstream"),
blanks = function(e, i, a) {
return e !== "";
},
title = function(e, i, a) {
return e.match(/^==>/);
},
args = ["-n", length];
let logs;

if (stream) { args.unshift('-f'); }

function tailProcess(procs, next) {
var count = 0,
map = {},
tail;
let count = 0;
const map = {};

procs.forEach(function (proc) {
args.push(proc.logFile);
map[proc.logFile] = { pid: proc.pid, file: proc.file };
count++;
});

tail = spawn('tail', args, {
const tail = spawn('tail', args, {
stdio: [null, 'pipe', 'pipe'],
});

tail.stdio[1].setEncoding('utf8');
tail.stdio[2].setEncoding('utf8');

tail.stdio[1].on('data', function (data) {
var chunk = data.split('\n\n');
const chunk = data.split("\n\n");
chunk.forEach(function (logs) {
var filteredLogs = logs.split('\n').filter(blanks),
file = filteredLogs.filter(title),
lines,
proc;
const filteredLogs = logs.split("\n").filter(blanks),
file = filteredLogs.filter(title);

proc = file.length
const proc = file.length
? map[file[0].split(' ')[1]]
: map[procs[0].logFile];

lines = count !== 1
const lines = count !== 1
? filteredLogs.slice(1)
: filteredLogs;

Expand All @@ -655,7 +654,7 @@ forever.tail = function (target, options, callback) {
return callback(new Error('Cannot find forever process: ' + target));
}

var procs = forever.findByIndex(target, processes)
const procs = forever.findByIndex(target, processes)
|| forever.findByScript(target, processes);

if (!procs) {
Expand All @@ -675,7 +674,7 @@ forever.tail = function (target, options, callback) {
forever.findById = function (id, processes) {
if (!processes) { return null; }

var procs = processes.filter(function (p) {
let procs = processes.filter(function(p) {
return p.id === id;
});

Expand All @@ -690,8 +689,8 @@ forever.findById = function (id, processes) {
// Finds the process with the specified index.
//
forever.findByIndex = function (index, processes) {
var indexAsNum = parseInt(index, 10),
proc;
const indexAsNum = parseInt(index, 10);
let proc;

if (indexAsNum == index) {
proc = processes && processes[indexAsNum];
Expand All @@ -713,7 +712,7 @@ forever.findByScript = function (script, processes) {
script = path.resolve(process.cwd(), script);
}

var procs = processes.filter(function (p) {
let procs = processes.filter(function(p) {
return p.file === script || path.join(p.spawnWith.cwd, p.file) === script;
});

Expand All @@ -728,9 +727,9 @@ forever.findByScript = function (script, processes) {
// Finds the process with the specified uid.
//
forever.findByUid = function (script, processes) {
var procs = !processes
let procs = !processes
? null
: processes.filter(function (p) {
: processes.filter(function(p) {
return p.uid === script;
});

Expand All @@ -749,7 +748,7 @@ forever.findByPid = function (pid, processes) {
? parseInt(pid, 10)
: pid;

var procs = processes && processes.filter(function (p) {
let procs = processes && processes.filter(function(p) {
return p.pid === pid;
});

Expand All @@ -769,10 +768,10 @@ forever.format = function (format, procs) {
return null;
}

var index = 0,
columns = forever.config.get('columns'),
rows = [[' '].concat(columns)],
formatted;
let index = 0;
const columns = forever.config.get("columns"),
rows = [[" "].concat(columns)];
let formatted;

function mapColumns(prefix, mapFn) {
return [prefix].concat(columns.map(mapFn));
Expand Down Expand Up @@ -809,8 +808,8 @@ forever.format = function (format, procs) {
// config, and log files used by forever.
//
forever.cleanUp = function (cleanLogs, allowManager) {
var emitter = new events.EventEmitter(),
pidPath = forever.config.get('pidPath');
const emitter = new events.EventEmitter(),
pidPath = forever.config.get("pidPath");

getAllProcesses(function (err, processes) {
if (err) {
Expand Down Expand Up @@ -899,16 +898,14 @@ forever.cleanUp = function (cleanLogs, allowManager) {
// that do not belong to current running forever processes.
//
forever.cleanLogsSync = function (processes) {
var root = forever.config.get('root'),
files = fs.readdirSync(root),
running,
runningLogs;
const root = forever.config.get("root"),
files = fs.readdirSync(root);

running = processes && processes.filter(function (p) {
const running = processes && processes.filter(function (p) {
return p && p.logFile;
});

runningLogs = running && running.map(function (p) {
const runningLogs = running && running.map(function (p) {
return p.logFile.split('/').pop();
});

Expand Down Expand Up @@ -1028,14 +1025,14 @@ forever.columns = {
return "STOPPED".red;
}

var delta = (new Date().getTime() - proc.ctime) / 1000;
var days = Math.floor(delta / 86400);
let delta = (new Date().getTime() - proc.ctime) / 1000;
const days = Math.floor(delta / 86400);
delta -= days * 86400;
var hours = Math.floor(delta / 3600) % 24;
const hours = Math.floor(delta / 3600) % 24;
delta -= hours * 3600;
var minutes = Math.floor(delta / 60) % 60;
const minutes = Math.floor(delta / 60) % 60;
delta -= minutes * 60;
var seconds = delta % 60;
const seconds = delta % 60;

return (days+':'+hours+':'+minutes+':'+seconds).yellow;
}
Expand Down

0 comments on commit 58eb131

Please sign in to comment.