Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (options.all) {
log("Stopping all servers...");
await pm2.promise.connect();
await pm2.promise.kill();
log("Stopped.");
// TODO: figure out why it's not exiting on it's own
process.exit();
} else {
await pm2serve.setupServeAsync(projectDir);
await pm2.promise.connect();
try {
log("Stopping the server...");
let name = await pm2serve.pm2NameAsync();
await pm2.promise.stop(name);
await pm2.promise.delete(name);
} catch (e) {
log.error("Failed to stop the server\n" + e.message);
process.exit(1);
return;
}
await pm2.promise.disconnect();
await config.projectExpJsonFile(projectDir).mergeAsync({state: 'STOPPED'});
log("Stopped.");
}
}
async function action(projectDir, options) {
if (options.all) {
log("Stopping all servers...");
await pm2.promise.connect();
await pm2.promise.kill();
log("Stopped.");
// TODO: figure out why it's not exiting on it's own
process.exit();
} else {
await pm2serve.setupServeAsync(projectDir);
await pm2.promise.connect();
try {
log("Stopping the server...");
let name = await pm2serve.pm2NameAsync();
await pm2.promise.stop(name);
await pm2.promise.delete(name);
} catch (e) {
log.error("Failed to stop the server\n" + e.message);
if (!argv.nodaemon) {
//log("Using pm2");
var _ref = yield _Promise.all([pm2NameAsync(), config.expInfoFile.getAsync('pm2Id', null)]);
var _ref2 = _slicedToArray(_ref, 2);
var pm2Name = _ref2[0];
var pm2Id = _ref2[1];
var bin = process.argv[0];
var script = process.argv[1];
var args_ = process.argv.slice(2);
args_.push('--nodaemon');
yield pm2.promise.connect();
yield config.expInfoFile.writeAsync({ pm2Id: pm2Id, pm2Name: pm2Name, state: 'STARTING' });
// There is a race condition here, but let's just not worry about it for now...
var needToStart = true;
if (pm2Id) {
// If this is already being managed by pm2, then restart it
//var app = await getPm2AppByIdAsync(pm2Id);
var app = yield getPm2AppByNameAsync(pm2Name);
if (app) {
log('pm2 managed process exists; restarting it');
yield pm2.promise.restart(app.pm_id);
//var app_ = await getPm2AppByIdAsync(pm2Id);
needToStart = false;
} else {
async function getPm2AppByIdAsync(id) {
// N.B. You need to be connected to PM2 for this to work
let apps = await pm2.promise.list();
for (let app of apps) {
if (app.pm_id === id) {
return app;
}
}
}
runAsync: _asyncToGenerator(function* (env) {
var argv = env.argv;
var args = argv._;
yield pm2serve.setupServeAsync(env);
var pm2Id = yield config.expInfoFile.getAsync('pm2Id', null);
if (!pm2Id) {
throw CommandError('NO_PM2_ID', env, "I can't find a server; try running `exp start` first.");
}
var lines = argv.lines || 50;
log("Use Ctrl-C to stop streaming logs");
yield pm2.promise.connect();
pm2.streamLogs(pm2Id, lines, !!argv.raw);
})
};
runAsync: _asyncToGenerator(function* (env) {
yield pm2.promise.connect();
try {
log('Stopping the server...');
yield pm2.promise.stop((yield pm2NameAsync()));
} catch (e) {
log.error('Failed to stop the server\n' + e.message);
}
yield pm2.promise.disconnect();
yield config.expInfoFile.updateAsync({ state: 'STOPPED' });
log('Stopped.');
}) },
pm2NameAsync: pm2NameAsync,
async function action(projectDir, options) {
await pm2serve.setupServeAsync(projectDir);
var pm2Id = await config.projectExpJsonFile(projectDir).getAsync('pm2Id', null);
if (pm2Id == null) {
throw CommandError('NO_PM2_ID', "I can't find a server; try running `exp start` first.");
}
var lines = options.lines || 50;
log("Use Ctrl-C to stop streaming logs");
await pm2.promise.connect();
pm2.streamLogs(pm2Id, lines, !!options.raw);
}
var getPm2AppByNameAsync = _asyncToGenerator(function* (name) {
var a = null;
var apps = yield pm2.promise.list();
for (var app of apps) {
if (app.name === name) {
a = app;
}
}
return a;
});
runAsync: _asyncToGenerator(function* (env) {
yield pm2.promise.connect();
try {
log('Stopping the server...');
yield pm2.promise.stop((yield pm2NameAsync()));
} catch (e) {
log.error('Failed to stop the server\n' + e.message);
}
yield pm2.promise.disconnect();
yield config.expInfoFile.updateAsync({ state: 'STOPPED' });
log('Stopped.');
}) },
pm2NameAsync: pm2NameAsync,