Skip to content

Commit 2f01ad0

Browse files
committed
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)
1 parent 4fb1a5c commit 2f01ad0

File tree

6 files changed

+65
-5
lines changed

6 files changed

+65
-5
lines changed

extensions/systemd/systemd.js

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ class SystemdProcessManager extends cli.ProcessManager {
1818
.catch((error) => Promise.reject(new cli.errors.ProcessError(error)));
1919
}
2020

21+
isRunning() {
22+
try {
23+
execa.shellSync(`systemctl is-active ${this.systemdName}`);
24+
return true;
25+
} catch (e) {
26+
// systemctl is-active returns exit code 3 when a service isn't active,
27+
// so throw if we don't have that.
28+
if (e.code !== 3) {
29+
throw e;
30+
}
31+
32+
return false;
33+
}
34+
}
35+
2136
static willRun() {
2237
try {
2338
execa.shellSync('which systemctl', {stdio: 'ignore'});

lib/instance.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,23 @@ class Instance {
2525
return true;
2626
}
2727

28-
// TODO: extend to support process manager run check
2928
get running() {
30-
return this.cliConfig.has('running');
29+
if (!this.cliConfig.has('running')) {
30+
return false;
31+
}
32+
33+
// Ordinarily we'd use this.loadRunningConfig(), but stack overflow
34+
// (the error not the programming bible) happens if we do that.
35+
let env = this.cliConfig.get('running');
36+
this.config = new Config(path.join(this.dir, `config.${env}.json`));
37+
this.loadProcess();
38+
39+
if (!this.process.isRunning(this.dir)) {
40+
this.cliConfig.set('running', null);
41+
return false;
42+
}
43+
44+
return true;
3145
}
3246
set running(environment) {
3347
this.cliConfig.set('running', environment).save();

lib/process-manager.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22
const requiredMethods = [
33
'start',
4-
'stop'
4+
'stop',
5+
'isRunning'
56
];
67

78
class ProcessManager {
@@ -36,6 +37,10 @@ class ProcessManager {
3637
throw error;
3738
}
3839

40+
isRunning() {
41+
// Base Implementation
42+
}
43+
3944
/**
4045
* This function checks if this process manager can be used on this system
4146
*

lib/utils/local-process.js

+21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path');
44
const fkill = require('fkill');
55
const spawn = require('child_process').spawn;
66
const assign = require('lodash/assign');
7+
const isRunning = require('is-running');
78

89
const errors = require('../errors');
910
const ProcessManager = require('../process-manager');
@@ -77,6 +78,26 @@ class LocalProcess extends ProcessManager {
7778
if (process.send) {process.send({error: true, message: error.message});}
7879
}
7980

81+
isRunning(cwd) {
82+
let pidfile = path.join(cwd, PID_FILE);
83+
84+
if (!fs.existsSync(pidfile)) {
85+
// Even if the process exists, if the file has been deleted we really can't
86+
// determine if it's still running, so just assume it's not.
87+
return false;
88+
}
89+
90+
let pid = parseInt(fs.readFileSync(pidfile));
91+
let running = isRunning(pid);
92+
93+
if (!running) {
94+
// If not running, cleanup the pid file
95+
fs.removeSync(pidfile);
96+
}
97+
98+
return running;
99+
}
100+
80101
static willRun() {
81102
return true;
82103
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"ghost-ignition": "2.8.11",
5252
"greenlock-cli": "2.2.9",
5353
"inquirer": "3.1.1",
54+
"is-running": "2.1.0",
5455
"knex-migrator": "2.0.16",
5556
"listr": "0.12.0",
5657
"lodash": "4.17.4",

yarn.lock

+6-2
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,10 @@ is-retry-allowed@^1.0.0:
17911791
version "1.1.0"
17921792
resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-1.1.0.tgz#11a060568b67339444033d0125a61a20d564fb34"
17931793

1794+
1795+
version "2.1.0"
1796+
resolved "https://registry.yarnpkg.com/is-running/-/is-running-2.1.0.tgz#30a73ff5cc3854e4fc25490809e9f5abf8de09e0"
1797+
17941798
is-stream@^1.0.0, is-stream@^1.1.0:
17951799
version "1.1.0"
17961800
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"
@@ -3088,11 +3092,11 @@ resolve-from@^1.0.0:
30883092
version "1.0.1"
30893093
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226"
30903094

3091-
[email protected], [email protected], resolve@^1.1.6, resolve@^1.1.7, resolve@~1.1.7:
3095+
30923096
version "1.1.7"
30933097
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b"
30943098

3095-
resolve@^1.2.0:
3099+
resolve@^1.1.6, resolve@^1.1.7, resolve@^1.2.0:
30963100
version "1.3.3"
30973101
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.3.3.tgz#655907c3469a8680dc2de3a275a8fdd69691f0e5"
30983102
dependencies:

0 commit comments

Comments
 (0)