From 2f01ad0ae7402c998497bbb74ea8e7c81ee2950f Mon Sep 17 00:00:00 2001 From: Austin Burdine Date: Thu, 29 Jun 2017 19:04:06 -0400 Subject: [PATCH] feat(process): update running check to call process manager closes #207 - updates the running check to make sure the process is actually running - adds a method to the process manager class (required to implement) --- extensions/systemd/systemd.js | 15 +++++++++++++++ lib/instance.js | 18 ++++++++++++++++-- lib/process-manager.js | 7 ++++++- lib/utils/local-process.js | 21 +++++++++++++++++++++ package.json | 1 + yarn.lock | 8 ++++++-- 6 files changed, 65 insertions(+), 5 deletions(-) diff --git a/extensions/systemd/systemd.js b/extensions/systemd/systemd.js index 6f8019fbd..f1a50de49 100644 --- a/extensions/systemd/systemd.js +++ b/extensions/systemd/systemd.js @@ -18,6 +18,21 @@ class SystemdProcessManager extends cli.ProcessManager { .catch((error) => Promise.reject(new cli.errors.ProcessError(error))); } + isRunning() { + try { + execa.shellSync(`systemctl is-active ${this.systemdName}`); + return true; + } catch (e) { + // systemctl is-active returns exit code 3 when a service isn't active, + // so throw if we don't have that. + if (e.code !== 3) { + throw e; + } + + return false; + } + } + static willRun() { try { execa.shellSync('which systemctl', {stdio: 'ignore'}); diff --git a/lib/instance.js b/lib/instance.js index 7feffa6d9..e9cc72480 100644 --- a/lib/instance.js +++ b/lib/instance.js @@ -25,9 +25,23 @@ class Instance { return true; } - // TODO: extend to support process manager run check get running() { - return this.cliConfig.has('running'); + if (!this.cliConfig.has('running')) { + return false; + } + + // Ordinarily we'd use this.loadRunningConfig(), but stack overflow + // (the error not the programming bible) happens if we do that. + let env = this.cliConfig.get('running'); + this.config = new Config(path.join(this.dir, `config.${env}.json`)); + this.loadProcess(); + + if (!this.process.isRunning(this.dir)) { + this.cliConfig.set('running', null); + return false; + } + + return true; } set running(environment) { this.cliConfig.set('running', environment).save(); diff --git a/lib/process-manager.js b/lib/process-manager.js index b01a39f05..884cb1188 100644 --- a/lib/process-manager.js +++ b/lib/process-manager.js @@ -1,7 +1,8 @@ 'use strict'; const requiredMethods = [ 'start', - 'stop' + 'stop', + 'isRunning' ]; class ProcessManager { @@ -36,6 +37,10 @@ class ProcessManager { throw error; } + isRunning() { + // Base Implementation + } + /** * This function checks if this process manager can be used on this system * diff --git a/lib/utils/local-process.js b/lib/utils/local-process.js index a0684de0b..efdfcbd46 100644 --- a/lib/utils/local-process.js +++ b/lib/utils/local-process.js @@ -4,6 +4,7 @@ const path = require('path'); const fkill = require('fkill'); const spawn = require('child_process').spawn; const assign = require('lodash/assign'); +const isRunning = require('is-running'); const errors = require('../errors'); const ProcessManager = require('../process-manager'); @@ -77,6 +78,26 @@ class LocalProcess extends ProcessManager { if (process.send) {process.send({error: true, message: error.message});} } + isRunning(cwd) { + let pidfile = path.join(cwd, PID_FILE); + + if (!fs.existsSync(pidfile)) { + // Even if the process exists, if the file has been deleted we really can't + // determine if it's still running, so just assume it's not. + return false; + } + + let pid = parseInt(fs.readFileSync(pidfile)); + let running = isRunning(pid); + + if (!running) { + // If not running, cleanup the pid file + fs.removeSync(pidfile); + } + + return running; + } + static willRun() { return true; } diff --git a/package.json b/package.json index 56d648e66..8176f6e2d 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "ghost-ignition": "2.8.11", "greenlock-cli": "2.2.9", "inquirer": "3.1.1", + "is-running": "2.1.0", "knex-migrator": "2.0.16", "listr": "0.12.0", "lodash": "4.17.4", diff --git a/yarn.lock b/yarn.lock index c5a67c4c1..771a81498 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1791,6 +1791,10 @@ is-retry-allowed@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-1.1.0.tgz#11a060568b67339444033d0125a61a20d564fb34" +is-running@2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/is-running/-/is-running-2.1.0.tgz#30a73ff5cc3854e4fc25490809e9f5abf8de09e0" + is-stream@^1.0.0, is-stream@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44" @@ -3088,11 +3092,11 @@ resolve-from@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226" -resolve@1.1.7, resolve@1.1.x, resolve@^1.1.6, resolve@^1.1.7, resolve@~1.1.7: +resolve@1.1.7, resolve@1.1.x, resolve@~1.1.7: version "1.1.7" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b" -resolve@^1.2.0: +resolve@^1.1.6, resolve@^1.1.7, resolve@^1.2.0: version "1.3.3" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.3.3.tgz#655907c3469a8680dc2de3a275a8fdd69691f0e5" dependencies: