Skip to content

Commit

Permalink
feat(process): update running check to call process manager
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
acburdine committed Jun 29, 2017
1 parent 4fb1a5c commit 2f01ad0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
15 changes: 15 additions & 0 deletions extensions/systemd/systemd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
Expand Down
18 changes: 16 additions & 2 deletions lib/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion lib/process-manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
const requiredMethods = [
'start',
'stop'
'stop',
'isRunning'
];

class ProcessManager {
Expand Down Expand Up @@ -36,6 +37,10 @@ class ProcessManager {
throw error;
}

isRunning() {
// Base Implementation
}

/**
* This function checks if this process manager can be used on this system
*
Expand Down
21 changes: 21 additions & 0 deletions lib/utils/local-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2f01ad0

Please sign in to comment.