2016-05-14 00:50:55 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
var JobFactory = require('./models/job-factory');
|
|
|
|
var jobStatus = require('./job-status');
|
2016-05-14 00:50:55 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function JobService (jobBackend, jobCanceller, logger) {
|
2016-05-14 00:50:55 +08:00
|
|
|
this.jobBackend = jobBackend;
|
|
|
|
this.jobCanceller = jobCanceller;
|
2019-04-04 20:31:41 +08:00
|
|
|
this.logger = logger;
|
2016-05-14 00:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = JobService;
|
|
|
|
|
2019-12-27 01:28:01 +08:00
|
|
|
JobService.prototype.get = function (jobId, callback) {
|
|
|
|
this.jobBackend.get(jobId, function (err, data) {
|
2016-05-14 00:50:55 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-12-27 01:28:01 +08:00
|
|
|
JobService.prototype.cancel = function (jobId, callback) {
|
2016-05-14 00:50:55 +08:00
|
|
|
var self = this;
|
|
|
|
|
2019-12-27 01:28:01 +08:00
|
|
|
self.get(jobId, function (err, job) {
|
2016-05-14 00:50:55 +08:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-12-27 01:28:01 +08:00
|
|
|
JobService.prototype.drain = function (jobId, callback) {
|
2016-05-14 00:50:55 +08:00
|
|
|
var self = this;
|
|
|
|
|
2019-12-27 01:28:01 +08:00
|
|
|
self.get(jobId, function (err, job) {
|
2016-05-14 00:50:55 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.jobCanceller.cancel(job, function (err) {
|
|
|
|
if (err) {
|
2019-12-27 01:28:01 +08:00
|
|
|
self.logger.debug('There was an error while draining job %s, %s ', jobId, 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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2016-10-27 23:40:13 +08:00
|
|
|
|
2016-10-27 23:46:43 +08:00
|
|
|
JobService.prototype.addWorkInProgressJob = function (user, jobId, callback) {
|
|
|
|
this.jobBackend.addWorkInProgressJob(user, jobId, callback);
|
2016-10-27 23:40:13 +08:00
|
|
|
};
|
2016-10-28 18:49:59 +08:00
|
|
|
|
2016-10-28 21:19:49 +08:00
|
|
|
JobService.prototype.clearWorkInProgressJob = function (user, jobId, callback) {
|
|
|
|
this.jobBackend.clearWorkInProgressJob(user, jobId, callback);
|
|
|
|
};
|
|
|
|
|
2016-10-28 18:50:33 +08:00
|
|
|
JobService.prototype.listWorkInProgressJobs = function (callback) {
|
|
|
|
this.jobBackend.listWorkInProgressJobs(callback);
|
2016-10-28 18:49:59 +08:00
|
|
|
};
|