-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathstop.js
69 lines (55 loc) · 1.63 KB
/
stop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var _ = require('lodash');
var async = require('async');
var fs = require('fs');
var http = require('http');
var pm2 = require('pm2');
module.exports = {
name:'stop',
description:'Stops all of the microservices (or subset based on regex pattern) using pm2',
example:'bosco stop <pattern>',
cmd:cmd
}
function cmd(bosco, args) {
var repoPattern = args.shift() || '.*';
var repoRegex = new RegExp(repoPattern);
var repos = bosco.config.get('github:repos');
var runningServices = {};
// Connect or launch PM2
pm2.connect(function(err) {
var stopRunningServices = function(running) {
async.map(repos, function(repo, next) {
var pkg, basePath, repoPath = bosco.getRepoPath(repo), packageJson = [repoPath,"package.json"].join("/");
if(repo.match(repoRegex) && bosco.exists(packageJson)) {
pkg = require(packageJson);
if(_.contains(running, repo)) {
stopService(repo, pkg.scripts.start, repoPath, next);
} else {
bosco.error("Not running: " + repo);
next();
}
} else {
next();
}
}, function(err) {
process.exit(0);
});
}
var getRunningServices = function(next) {
pm2.list(function(err, list) {
next(err, _.pluck(list,'name'));
});
}
var stopService = function(repo, script, repoPath, next) {
bosco.log("Stopping " + repo + " @ " + repoPath + " via " + script.blue);
pm2.stop(repo, function(err, proc) {
pm2.delete(repo, function(err, proc) {
next(err);
});
});
}
bosco.log("Stop each mircoservice " + args);
getRunningServices(function(err, running) {
stopRunningServices(running);
});
});
}