vulpes
Version:
Job management framework
293 lines (235 loc) • 13.5 kB
JavaScript
"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var ms = require("ms");
var Helper = require("./Helper.js");
var _require = require("delayable-setinterval"),
clearDelayedInterval = _require.clearDelayedInterval,
setDelayedInterval = _require.setDelayedInterval;
var _require2 = require("../symbols.js"),
JOB_RESULT_TYPE_FAILURE = _require2.JOB_RESULT_TYPE_FAILURE,
JOB_RESULT_TYPE_SUCCESS = _require2.JOB_RESULT_TYPE_SUCCESS,
JOB_RESULT_TYPE_TIMEOUT = _require2.JOB_RESULT_TYPE_TIMEOUT,
JOB_STATUS_STOPPED = _require2.JOB_STATUS_STOPPED;
var _require3 = require("../time.js"),
getTimestamp = _require3.getTimestamp;
/**
* Auto archive helper
* @augments Helper
* @memberof module:Vulpes
*/
var AutoArchiveHelper = function (_Helper) {
_inherits(AutoArchiveHelper, _Helper);
/**
* @typedef {Object} AutoArchiveHelperOptions
* @property {Number=} checkInterval - Milliseconds between archive checks
* @property {Number=} archivePeriod - Milliseconds that a job has been stopped before
* it can be archived
* @property {Number=} queryLimit - Max job results from queries for archving
*/
/**
* Constructor for the auto archive helper
* @param {AutoArchiveHelperOptions=} options Config options
* @memberof AutoArchiveHelper
*/
function AutoArchiveHelper() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$checkInterval = _ref.checkInterval,
checkInterval = _ref$checkInterval === undefined ? ms("10m") : _ref$checkInterval,
_ref$archivePeriod = _ref.archivePeriod,
archivePeriod = _ref$archivePeriod === undefined ? ms("2w") : _ref$archivePeriod,
_ref$deletePeriod = _ref.deletePeriod,
deletePeriod = _ref$deletePeriod === undefined ? ms("1w") : _ref$deletePeriod,
_ref$queryLimit = _ref.queryLimit,
queryLimit = _ref$queryLimit === undefined ? 50 : _ref$queryLimit;
_classCallCheck(this, AutoArchiveHelper);
var _this = _possibleConstructorReturn(this, (AutoArchiveHelper.__proto__ || Object.getPrototypeOf(AutoArchiveHelper)).call(this));
_this._checkInterval = checkInterval;
_this._archivePeriod = archivePeriod;
_this._queryLimit = queryLimit;
_this._deletePeriod = deletePeriod;
return _this;
}
/**
* Perform the archival process
* @returns {Promise}
* @memberof AutoArchiveHelper
*/
_createClass(AutoArchiveHelper, [{
key: "archive",
value: function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return this.archiveJobs();
case 2:
if (!this._deletePeriod) {
_context.next = 5;
break;
}
_context.next = 5;
return this.archiveJobs("delete");
case 5:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function archive() {
return _ref2.apply(this, arguments);
}
return archive;
}()
/**
* Archive or delete some jobs
* @param {String=} action The action with which to perform on the selected jobs
* (defaults to "archive", but can also be "delete")
* @returns {Promise}
* @memberof AutoArchiveHelper
*/
}, {
key: "archiveJobs",
value: function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
var action = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "archive";
var checkPeriod, now, query, oldJobs, processedIDs, i, jobTree, readyToArchive, j;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (!(action !== "archive" && action !== "delete")) {
_context2.next = 2;
break;
}
throw new Error("Invalid archive action: " + action);
case 2:
checkPeriod = action === "archive" ? this._archivePeriod : this._deletePeriod;
now = getTimestamp();
query = action === "archive" ? {
"result.type": function resultType(type) {
return type === JOB_RESULT_TYPE_FAILURE || type === JOB_RESULT_TYPE_TIMEOUT || type === JOB_RESULT_TYPE_SUCCESS;
},
status: JOB_STATUS_STOPPED,
"times.stopped": function timesStopped(timeProp) {
return timeProp && now - timeProp >= checkPeriod;
}
} : {
archived: true,
"times.archived": function timesArchived(timeProp) {
return timeProp && now - timeProp >= checkPeriod;
}
};
_context2.next = 7;
return this.service.queryJobs(query, {
limit: this._queryLimit,
sort: "created",
order: "asc"
});
case 7:
oldJobs = _context2.sent;
processedIDs = [];
i = 0;
case 10:
if (!(i < oldJobs.length)) {
_context2.next = 35;
break;
}
if (!processedIDs.includes(oldJobs[i].id)) {
_context2.next = 13;
break;
}
return _context2.abrupt("continue", 32);
case 13:
_context2.next = 15;
return this.service.getJobTree(oldJobs[i].id);
case 15:
jobTree = _context2.sent;
processedIDs.push.apply(processedIDs, _toConsumableArray(jobTree.map(function (job) {
return job.id;
})));
readyToArchive = jobTree.every(function (job) {
return job.status === JOB_STATUS_STOPPED && (job.result.type === JOB_RESULT_TYPE_FAILURE || job.result.type === JOB_RESULT_TYPE_SUCCESS || job.result.type === JOB_RESULT_TYPE_TIMEOUT);
});
if (!readyToArchive) {
_context2.next = 32;
break;
}
j = 0;
case 20:
if (!(j < jobTree.length)) {
_context2.next = 32;
break;
}
if (!(action === "archive")) {
_context2.next = 26;
break;
}
_context2.next = 24;
return this.service.archiveJob(jobTree[j].id);
case 24:
_context2.next = 29;
break;
case 26:
if (!(action === "delete")) {
_context2.next = 29;
break;
}
_context2.next = 29;
return this.service.removeJob(jobTree[j].id);
case 29:
j += 1;
_context2.next = 20;
break;
case 32:
i += 1;
_context2.next = 10;
break;
case 35:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
function archiveJobs() {
return _ref3.apply(this, arguments);
}
return archiveJobs;
}()
/**
* Attach to a service instance
* @param {Service} service The service to attach to
* @memberof AutoArchiveHelper
*/
}, {
key: "attach",
value: function attach(service) {
var _this2 = this;
_get(AutoArchiveHelper.prototype.__proto__ || Object.getPrototypeOf(AutoArchiveHelper.prototype), "attach", this).call(this, service);
this._timer = setDelayedInterval(function () {
return _this2.archive();
}, this._checkInterval);
}
/**
* Shutdown the helper
* @memberof AutoArchiveHelper
*/
}, {
key: "shutdown",
value: function shutdown() {
clearDelayedInterval(this._timer);
_get(AutoArchiveHelper.prototype.__proto__ || Object.getPrototypeOf(AutoArchiveHelper.prototype), "shutdown", this).call(this);
}
}]);
return AutoArchiveHelper;
}(Helper);
module.exports = AutoArchiveHelper;