2016-05-14 00:50:55 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-02 18:26:20 +08:00
|
|
|
var util = require('util');
|
2016-05-14 00:50:55 +08:00
|
|
|
var uuid = require('node-uuid');
|
2016-06-02 18:26:20 +08:00
|
|
|
var JobStateMachine = require('./job_state_machine');
|
2016-05-18 00:59:39 +08:00
|
|
|
var jobStatus = require('../job_status');
|
2016-05-14 00:50:55 +08:00
|
|
|
var mandatoryProperties = [
|
|
|
|
'job_id',
|
|
|
|
'status',
|
|
|
|
'query',
|
|
|
|
'created_at',
|
|
|
|
'updated_at',
|
2016-05-23 23:51:56 +08:00
|
|
|
'host',
|
2016-05-14 00:50:55 +08:00
|
|
|
'user'
|
|
|
|
];
|
|
|
|
|
|
|
|
function JobBase(data) {
|
2016-06-02 18:26:20 +08:00
|
|
|
JobStateMachine.call(this);
|
|
|
|
|
2016-05-14 00:50:55 +08:00
|
|
|
var now = new Date().toISOString();
|
|
|
|
|
|
|
|
this.data = data;
|
|
|
|
|
|
|
|
if (!this.data.job_id) {
|
|
|
|
this.data.job_id = uuid.v4();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.data.created_at) {
|
|
|
|
this.data.created_at = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.data.updated_at) {
|
|
|
|
this.data.updated_at = now;
|
|
|
|
}
|
|
|
|
}
|
2016-06-02 18:26:20 +08:00
|
|
|
util.inherits(JobBase, JobStateMachine);
|
2016-05-14 00:50:55 +08:00
|
|
|
|
|
|
|
module.exports = JobBase;
|
|
|
|
|
|
|
|
// should be implemented by childs
|
|
|
|
JobBase.prototype.getNextQuery = function () {
|
|
|
|
throw new Error('Unimplemented method');
|
|
|
|
};
|
|
|
|
|
|
|
|
JobBase.prototype.hasNextQuery = function () {
|
2016-05-16 07:22:47 +08:00
|
|
|
return !!this.getNextQuery();
|
2016-05-14 00:50:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
JobBase.prototype.isPending = function () {
|
|
|
|
return this.data.status === jobStatus.PENDING;
|
|
|
|
};
|
|
|
|
|
|
|
|
JobBase.prototype.isRunning = function () {
|
|
|
|
return this.data.status === jobStatus.RUNNING;
|
|
|
|
};
|
|
|
|
|
|
|
|
JobBase.prototype.isDone = function () {
|
|
|
|
return this.data.status === jobStatus.DONE;
|
|
|
|
};
|
|
|
|
|
|
|
|
JobBase.prototype.isCancelled = function () {
|
|
|
|
return this.data.status === jobStatus.CANCELLED;
|
|
|
|
};
|
|
|
|
|
|
|
|
JobBase.prototype.isFailed = function () {
|
|
|
|
return this.data.status === jobStatus.FAILED;
|
|
|
|
};
|
|
|
|
|
|
|
|
JobBase.prototype.isUnknown = function () {
|
|
|
|
return this.data.status === jobStatus.UNKNOWN;
|
|
|
|
};
|
|
|
|
|
2016-05-16 07:22:47 +08:00
|
|
|
JobBase.prototype.setQuery = function (query) {
|
2016-05-14 00:50:55 +08:00
|
|
|
var now = new Date().toISOString();
|
|
|
|
|
2016-05-16 07:22:47 +08:00
|
|
|
if (!this.isPending()) {
|
|
|
|
throw new Error('Job is not pending, it cannot be updated');
|
2016-05-14 00:50:55 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 07:22:47 +08:00
|
|
|
this.data.updated_at = now;
|
|
|
|
this.data.query = query;
|
2016-05-14 00:50:55 +08:00
|
|
|
};
|
|
|
|
|
2016-05-19 22:58:14 +08:00
|
|
|
JobBase.prototype.setStatus = function (finalStatus, errorMesssage) {
|
2016-05-16 07:22:47 +08:00
|
|
|
var now = new Date().toISOString();
|
2016-05-14 00:50:55 +08:00
|
|
|
var initialStatus = this.data.status;
|
2016-06-02 18:26:20 +08:00
|
|
|
var isValid = this.isValidTransition(initialStatus, finalStatus);
|
2016-05-14 00:50:55 +08:00
|
|
|
|
|
|
|
if (!isValid) {
|
|
|
|
throw new Error('Cannot set status from ' + initialStatus + ' to ' + finalStatus);
|
|
|
|
}
|
|
|
|
|
2016-05-16 07:22:47 +08:00
|
|
|
this.data.updated_at = now;
|
2016-05-14 00:50:55 +08:00
|
|
|
this.data.status = finalStatus;
|
2016-05-19 22:58:14 +08:00
|
|
|
if (finalStatus === jobStatus.FAILED && errorMesssage) {
|
|
|
|
this.data.failed_reason = errorMesssage;
|
|
|
|
}
|
2016-05-14 00:50:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
JobBase.prototype.validate = function () {
|
|
|
|
for (var i = 0; i < mandatoryProperties.length; i++) {
|
|
|
|
if (!this.data[mandatoryProperties[i]]) {
|
|
|
|
throw new Error('property "' + mandatoryProperties[i] + '" is mandatory');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JobBase.prototype.serialize = function () {
|
|
|
|
var data = JSON.parse(JSON.stringify(this.data));
|
|
|
|
delete data.host;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
2016-09-30 22:44:39 +08:00
|
|
|
|
|
|
|
JobBase.prototype.log = function(/*logger*/) {
|
|
|
|
return false;
|
|
|
|
};
|