@angstone/monostone
Version:
monolitic event-sourced framework
102 lines • 3.27 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const ast = require("@angstone/node-util");
const _1 = require("./");
const cron_1 = require("cron");
const state = {
cronjobs: [],
runningJobs: [],
};
const RUNNING_VERIFIER_REST_TIME = 50;
/*const DEFAULT_TIMEZONE: string|undefined =
process.env.CRONJOB_TIMEZONE ||
process.env.TIMEZONE ||
undefined;*/
const config = () => {
ast.log("creating cronjob controller");
state.cronjobs = [];
state.runningJobs = [];
};
/**
* stop all cronjobs
*/
const stop = () => __awaiter(this, void 0, void 0, function* () {
ast.log("stoping cronjobs");
state.cronjobs.forEach((cronjob) => {
cronjob.stop();
});
let alldone = false;
while (!alldone) {
alldone = true;
state.runningJobs.forEach((isRunning) => {
if (isRunning) {
alldone = false;
}
});
if (!alldone) {
yield ast.delay(RUNNING_VERIFIER_REST_TIME);
}
}
ast.log("cronjobs stoped");
});
/**
* start all cronjobs
*/
const start = () => __awaiter(this, void 0, void 0, function* () {
ast.log("starting cronjob controller");
state.cronjobs.forEach((cronjob) => {
cronjob.start();
});
ast.log("cronjobs started");
});
/**
* load cronjobs
* @param features list of the features loaded by server
*/
const loadFeatures = (features) => {
ast.log("loading cronjobs");
const cronjobs = [];
features.forEach((feature) => {
if (feature.cronjobs) {
feature.cronjobs.forEach((cronjob) => {
cronjobs.push(cronjob);
});
}
});
cronjobs.forEach((jobSheet, jobIndex) => {
state.runningJobs.push(false);
const cronjob = new cron_1.CronJob({
cronTime: jobSheet.cron,
onTick: () => {
if (!state.runningJobs[jobIndex]) {
state.runningJobs[jobIndex] = true;
try {
jobSheet.job(() => {
state.runningJobs[jobIndex] = false;
});
}
catch (error) {
if (jobSheet.onError) {
jobSheet.onError(error);
}
state.runningJobs[jobIndex] = false;
}
}
},
});
state.cronjobs.push(cronjob);
});
};
exports.CronjobModule = Object.assign({}, _1.BasicModule, { config,
loadFeatures,
start,
stop });
//# sourceMappingURL=cronjob.module.js.map