2016-06-01 16:39:01 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var util = require('util');
|
2019-10-04 00:24:39 +08:00
|
|
|
var QueryBase = require('./query-base');
|
|
|
|
var jobStatus = require('../../job-status');
|
2016-06-01 16:39:01 +08:00
|
|
|
|
|
|
|
function Fallback(index) {
|
|
|
|
QueryBase.call(this, index);
|
|
|
|
}
|
|
|
|
util.inherits(Fallback, QueryBase);
|
|
|
|
|
|
|
|
module.exports = Fallback;
|
|
|
|
|
|
|
|
Fallback.is = function (query) {
|
|
|
|
if (query.onsuccess || query.onerror) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
Fallback.prototype.getNextQuery = function (job) {
|
|
|
|
if (this.hasOnSuccess(job)) {
|
|
|
|
return this.getOnSuccess(job);
|
|
|
|
}
|
|
|
|
if (this.hasOnError(job)) {
|
|
|
|
return this.getOnError(job);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Fallback.prototype.getOnSuccess = function (job) {
|
|
|
|
if (job.query.query[this.index].status === jobStatus.DONE &&
|
|
|
|
job.query.query[this.index].fallback_status === jobStatus.PENDING) {
|
2016-06-30 23:41:02 +08:00
|
|
|
var onsuccessQuery = job.query.query[this.index].onsuccess;
|
|
|
|
if (onsuccessQuery) {
|
|
|
|
onsuccessQuery = onsuccessQuery.replace(/<%=\s*job_id\s*%>/g, job.job_id);
|
|
|
|
}
|
|
|
|
return onsuccessQuery;
|
2016-06-01 16:39:01 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Fallback.prototype.hasOnSuccess = function (job) {
|
|
|
|
return !!this.getOnSuccess(job);
|
|
|
|
};
|
|
|
|
|
|
|
|
Fallback.prototype.getOnError = function (job) {
|
|
|
|
if (job.query.query[this.index].status === jobStatus.FAILED &&
|
|
|
|
job.query.query[this.index].fallback_status === jobStatus.PENDING) {
|
2016-06-29 20:22:23 +08:00
|
|
|
var onerrorQuery = job.query.query[this.index].onerror;
|
|
|
|
if (onerrorQuery) {
|
2016-06-30 23:41:02 +08:00
|
|
|
onerrorQuery = onerrorQuery.replace(/<%=\s*job_id\s*%>/g, job.job_id);
|
2016-06-29 20:22:23 +08:00
|
|
|
onerrorQuery = onerrorQuery.replace(/<%=\s*error_message\s*%>/g, job.query.query[this.index].failed_reason);
|
|
|
|
}
|
|
|
|
return onerrorQuery;
|
2016-06-01 16:39:01 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Fallback.prototype.hasOnError = function (job) {
|
|
|
|
return !!this.getOnError(job);
|
|
|
|
};
|
|
|
|
|
2016-06-02 19:47:45 +08:00
|
|
|
Fallback.prototype.setStatus = function (status, job, errorMessage) {
|
2016-06-01 16:39:01 +08:00
|
|
|
var isValid = false;
|
|
|
|
|
|
|
|
isValid = this.isValidTransition(job.query.query[this.index].fallback_status, status);
|
|
|
|
|
|
|
|
if (isValid) {
|
|
|
|
job.query.query[this.index].fallback_status = status;
|
2016-06-02 19:47:45 +08:00
|
|
|
if (status === jobStatus.FAILED && errorMessage) {
|
|
|
|
job.query.query[this.index].failed_reason = errorMessage;
|
2016-06-01 16:39:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return isValid;
|
|
|
|
};
|
2016-06-03 00:02:25 +08:00
|
|
|
|
|
|
|
Fallback.prototype.getStatus = function (job) {
|
|
|
|
return job.query.query[this.index].fallback_status;
|
|
|
|
};
|