Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Commit 2951894

Browse files
author
Ilya Radchenko
committed
Implement prune jobs, rename command files
1 parent 9685ae4 commit 2951894

File tree

9 files changed

+143
-33
lines changed

9 files changed

+143
-33
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ command
2020
upgrade Replace a plugin with the the latest version
2121
init Initialize a new plugin for development
2222
runTest Run a test and optionally deploy
23-
cleanupJobs Cleanup all jobs except for the latest 20 or custom
23+
pruneJobs Cleanup all jobs except for the latest 20 or custom
2424
2525
Options:
2626
-v, --version Print version and exit
@@ -96,12 +96,13 @@ Options:
9696
-d Deploy on green (optional) (flag)
9797
```
9898

99-
### cleanupJobs
99+
### pruneJobs
100100

101101
Cleanup all jobs except for the latest 20 or custom
102102

103103
```
104104
Options:
105105
-k Number of latest jobs to keep, defaults to 20
106106
-p Project to targer, defaults to all projects
107+
-d Just print stats about what will be removed, but do not remove any jobs
107108
```

commands/cleanupJobs.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

commands/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
'use strict';
22

33
var order = [
4-
require('./addUser'),
4+
require('./add-user'),
55
require('./restart'),
66
require('./list'),
77
require('./install'),
88
require('./uninstall'),
99
require('./upgrade'),
1010
require('./init'),
11-
require('./runTest')
11+
require('./run-test'),
12+
require('./prune-jobs')
1213
];
1314

1415
module.exports.setup = function (deps, parser) {

commands/prune-jobs.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
module.exports = function (deps, parser) {
4+
var cleanupJobs = require('../lib/cleanup-jobs')(deps);
5+
6+
parser.command('pruneJobs')
7+
.option('project', {
8+
abbr: 'p',
9+
help: 'Project to targer, defaults to all projects if not specified',
10+
})
11+
.option('keepJobs', {
12+
abbr: 'k',
13+
help: 'Number of latest jobs to keep',
14+
default: 20
15+
})
16+
.option('dryRun', {
17+
abbr: 'd',
18+
help: 'Just print stats about what will be removed, but do not remove any jobs',
19+
flag: true,
20+
default: false
21+
})
22+
.callback(function (opts) {
23+
deps.connect(function (err) {
24+
if (err) {
25+
throw err;
26+
}
27+
28+
cleanupJobs(opts.keepJobs, opts.project, opts.dryRun);
29+
});
30+
})
31+
.help('Cleanup all jobs except for the latest 20 or custom');
32+
};

lib/cleanup-jobs.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/prune-jobs.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
'use strict';
2+
3+
var Bluebird = require('bluebird');
4+
var colors = require('colors');
5+
6+
module.exports = function(deps) {
7+
var models = deps.models();
8+
var Project = models.Project;
9+
var Job = models.Job;
10+
11+
return function pruneJobs(keepNumJobs, projectName, dryRun) {
12+
if (dryRun) {
13+
console.log('** Dry Run **\n');
14+
}
15+
16+
findProjects(projectName, Project).then(function (projects) {
17+
return Bluebird.map(projects, function (project) {
18+
return removeJobsAfterLatest(keepNumJobs, project.name, dryRun, Job);
19+
})
20+
}).catch(function (err) {
21+
console.log(colors.red(err.message || err));
22+
}).finally(function () {
23+
process.exit(1);
24+
});
25+
};
26+
};
27+
28+
/**
29+
* Find all projects, or just the one specified
30+
*
31+
* @private
32+
* @param {String} name name identified or project, user/name format
33+
* @param {Model<Project>} Project mongoose model
34+
* @returns {Promise} resolves to an array of projects
35+
*/
36+
function findProjects(name, Project) {
37+
var query;
38+
39+
if (name) {
40+
query = Project.findOne({ name: name }).exec();
41+
} else {
42+
query = Project.find().exec();
43+
}
44+
45+
return query.then(function (res) {
46+
return res.length ? res : [res];
47+
});
48+
}
49+
50+
/**
51+
* Finds all jobs for project, and removes if not a dry run;
52+
*
53+
* @private
54+
* @param {Number} keepJobs number of jobs to keep, defaults to 20
55+
* @param {String} projectName user/name format
56+
* @param {Boolean} dryRun just print the stats, no actual removal done
57+
* @returns {Promise} resolves once stats printed, or jobs removed
58+
*/
59+
function removeJobsAfterLatest(keepJobs, projectName, dryRun, Job) {
60+
return Job.find({ project: projectName })
61+
.select('created')
62+
.sort('-created')
63+
.exec().then(function (jobs) {
64+
var toRemove = jobs.slice(keepJobs);
65+
66+
logStats(projectName, jobs, toRemove, dryRun);
67+
68+
if (!dryRun && toRemove.length) {
69+
return Bluebird.map(toRemove, function (job) {
70+
return job.remove();
71+
}).then(function () {
72+
console.log(colors.yellow(' ' + toRemove.length + ' jobs removed'));
73+
});
74+
}
75+
});
76+
}
77+
78+
/**
79+
* Log stats for job removal of a project
80+
*
81+
* @private
82+
* @param {String} projectName
83+
* @param {Array<Job>} allJobs
84+
* @param {Array<Job>} toRemove
85+
*/
86+
function logStats(projectName, allJobs, toRemove) {
87+
if (!toRemove.length) {
88+
return console.log(colors.green('No jobs to remove'));
89+
}
90+
91+
var log = [
92+
colors.green(colors.bold('Removing jobs for "' + projectName + '":')),
93+
' Keeping Latest ' + (allJobs.length - toRemove.length) + ' jobs.',
94+
' Total Jobs: ' + allJobs.length,
95+
' Latest job created on: ' + allJobs[0].created,
96+
' Oldest job created on: ' + allJobs[allJobs.length - 1].created,
97+
' Jobs To Remove: ' + toRemove.length,
98+
' Latest job created on: ' + toRemove[0].created,
99+
' Oldest job created on: ' + toRemove[toRemove.length - 1].created
100+
];
101+
102+
console.log(log.join('\n'));
103+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
},
1818
"homepage": "https://github.com/Strider-CD/strider-cli",
1919
"dependencies": {
20+
"bluebird": "^3.4.7",
2021
"chokidar": "^0.9.0",
2122
"cli-table": "^0.3.0",
23+
"colors": "^1.1.2",
2224
"debug": "^2.1.1",
2325
"glob": "^4.0.6",
2426
"lodash": "^2.4.1",

0 commit comments

Comments
 (0)