nci/scheduler.js

45 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2015-07-12 08:39:22 +00:00
'use strict';
var _ = require('underscore'),
CronJob = require('cron').CronJob;
exports.init = function(app, callback) {
2016-01-05 11:18:20 +00:00
var logger = app.lib.logger('scheduler'),
2015-07-12 08:39:22 +00:00
projectJobs = {};
2016-01-05 11:18:20 +00:00
app.projects.on('projectLoaded', function(project) {
2015-07-12 08:39:22 +00:00
var time = project.buildEvery && project.buildEvery.time;
if (time) {
2016-01-05 11:18:20 +00:00
logger.log(
'Start job for loaded project "%s" by schedule "%s"',
project.name,
time
);
2015-07-12 08:39:22 +00:00
projectJobs[project.name] = {};
projectJobs[project.name].job = new CronJob({
cronTime: time,
onTick: function() {
logger.log('Run project "%s"', project.name);
2016-01-06 20:24:41 +00:00
app.builds.create({
2015-07-12 08:39:22 +00:00
projectName: project.name,
withScmChangesOnly: project.buildEvery.withScmChangesOnly,
initiator: {type: 'scheduler'}
2015-07-12 08:39:22 +00:00
});
},
start: true
});
}
});
2016-01-05 11:18:20 +00:00
app.projects.on('projectUnloaded', function(project) {
2015-07-12 08:39:22 +00:00
if (project.name in projectJobs) {
logger.log('Stop job for unloaded project "%s"', project.name);
projectJobs[project.name].job.stop();
delete projectJobs[project.name];
}
});
callback();
};