2016-05-14 00:50:55 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-18 17:06:49 +08:00
|
|
|
var debug = require('./util/debug')('job-service');
|
2016-05-23 21:20:14 +08:00
|
|
|
var JobFactory = require('./models/job_factory');
|
2016-05-14 00:50:55 +08:00
|
|
|
var jobStatus = require('./job_status');
|
|
|
|
|
|
|
|
function JobService(jobBackend, jobCanceller) {
|
|
|
|
this.jobBackend = jobBackend;
|
|
|
|
this.jobCanceller = jobCanceller;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = JobService;
|
|
|
|
|
|
|
|
JobService.prototype.get = function (job_id, callback) {
|
|
|
|
this.jobBackend.get(job_id, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var job;
|
|
|
|
|
|
|
|
try {
|
|
|
|
job = JobFactory.create(data);
|
|
|
|
} catch (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, job);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
JobService.prototype.create = function (data, callback) {
|
|
|
|
try {
|
|
|
|
var job = JobFactory.create(data);
|
|
|
|
job.validate();
|
2016-05-16 07:22:47 +08:00
|
|
|
this.jobBackend.create(job.data, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
callback(null, job);
|
|
|
|
});
|
2016-05-14 00:50:55 +08:00
|
|
|
} catch (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JobService.prototype.save = function (job, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
try {
|
|
|
|
job.validate();
|
|
|
|
} catch (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-05-16 07:22:47 +08:00
|
|
|
|
|
|
|
self.jobBackend.update(job.data, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
job = JobFactory.create(data);
|
|
|
|
} catch (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, job);
|
|
|
|
});
|
2016-05-14 00:50:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
JobService.prototype.cancel = function (job_id, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.get(job_id, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-05-16 07:22:47 +08:00
|
|
|
var isPending = job.isPending();
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
try {
|
|
|
|
job.setStatus(jobStatus.CANCELLED);
|
|
|
|
} catch (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-05-16 07:22:47 +08:00
|
|
|
if (isPending) {
|
|
|
|
return self.save(job, callback);
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
self.jobCanceller.cancel(job, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-05-16 07:22:47 +08:00
|
|
|
self.save(job, callback);
|
2016-05-14 00:50:55 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
JobService.prototype.drain = function (job_id, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.get(job_id, function (err, job) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jobCanceller.cancel(job, function (err) {
|
|
|
|
if (err) {
|
2016-05-18 17:06:49 +08:00
|
|
|
debug('There was an error while draining job %s, %s ', job_id, err);
|
2016-05-14 00:50:55 +08:00
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
job.setStatus(jobStatus.PENDING);
|
|
|
|
} catch (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jobBackend.update(job.data, callback);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|