CartoDB-SQL-API/batch/models/job_base.js

123 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-05-14 00:50:55 +08:00
'use strict';
var util = require('util');
2016-05-14 00:50:55 +08:00
var uuid = require('node-uuid');
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',
'host',
2016-05-14 00:50:55 +08:00
'user'
];
function JobBase(data) {
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;
}
}
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
};
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;
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;
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));
2016-05-14 00:50:55 +08:00
delete data.host;
delete data.dbuser;
delete data.port;
delete data.dbname;
delete data.pass;
2016-05-14 00:50:55 +08:00
return data;
};
2016-09-30 22:44:39 +08:00
JobBase.prototype.log = function(/*logger*/) {
return false;
};