2015-07-12 08:39:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('underscore'),
|
|
|
|
logger = require('./lib/logger')('scheduler'),
|
|
|
|
CronJob = require('cron').CronJob;
|
|
|
|
|
|
|
|
exports.init = function(app, callback) {
|
|
|
|
|
|
|
|
var distributor = app.distributor,
|
|
|
|
projectJobs = {};
|
|
|
|
|
|
|
|
app.on('projectLoaded', function(project) {
|
|
|
|
var time = project.buildEvery && project.buildEvery.time;
|
|
|
|
if (time) {
|
|
|
|
logger.log('Start job for loaded project "%s"', project.name);
|
|
|
|
projectJobs[project.name] = {};
|
|
|
|
projectJobs[project.name].job = new CronJob({
|
|
|
|
cronTime: time,
|
|
|
|
onTick: function() {
|
|
|
|
logger.log('Run project "%s"', project.name);
|
|
|
|
distributor.run({
|
|
|
|
projectName: project.name,
|
2015-11-23 18:35:52 +00:00
|
|
|
withScmChangesOnly: project.buildEvery.withScmChangesOnly,
|
|
|
|
initiator: {type: 'scheduler'}
|
2015-07-12 08:39:22 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
start: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.on('projectUnloaded', function(project) {
|
|
|
|
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();
|
|
|
|
};
|