Typescript migration:

* Refactoring of scheduler and jobs to use promises.
* Add "failed" state for tasks.
This commit is contained in:
baldo 2020-04-08 23:41:04 +02:00
parent 5c0b5abf73
commit 8b8835e4ac
15 changed files with 251 additions and 192 deletions

View file

@ -1,18 +0,0 @@
'use strict';
const Logger = require('../logger')
const NodeService = require('../services/nodeService')
module.exports = {
description: 'Makes sure node files (holding fastd key, name, etc.) are correctly named.',
run: function (callback) {
NodeService.fixNodeFilenames(function (err) {
if (err) {
Logger.tag('nodes', 'fix-filenames').error('Error fixing filenames:', err);
}
callback();
});
}
}

View file

@ -0,0 +1,22 @@
import Logger from "../logger";
import NodeService from "../services/nodeService";
export default {
name: 'FixNodeFilenamesJob',
description: 'Makes sure node files (holding fastd key, name, etc.) are correctly named.',
run: (): Promise<void> => {
return new Promise<void>(
(resolve, reject) => {
NodeService.fixNodeFilenames((err: any): void => {
if (err) {
Logger.tag('nodes', 'fix-filenames').error('Error fixing filenames:', err);
return reject(err);
}
resolve();
});
}
);
}
}

View file

@ -1,18 +0,0 @@
'use strict';
const Logger = require('../logger')
const MailService = require('../services/mailService')
module.exports = {
description: 'Send pending emails (up to 5 attempts in case of failures).',
run: function (callback) {
MailService.sendPendingMails(function (err) {
if (err) {
Logger.tag('mail', 'queue').error('Error sending pending mails:', err);
}
callback();
});
}
}

View file

@ -0,0 +1,22 @@
import Logger from "../logger"
import MailService from "../services/mailService"
export default {
name: 'MailQueueJob',
description: 'Send pending emails (up to 5 attempts in case of failures).',
run: (): Promise<void> => {
return new Promise<void>(
(resolve, reject) => {
MailService.sendPendingMails((err: any): void => {
if (err) {
Logger.tag('mail', 'queue').error('Error sending pending mails:', err);
return reject(err);
}
resolve();
});
}
)
}
}

View file

@ -1,18 +0,0 @@
'use strict';
const Logger = require('../logger')
const MonitoringService = require('../services/monitoringService')
module.exports = {
description: 'Sends monitoring emails depending on the monitoring state of nodes retrieved by the NodeInformationRetrievalJob.',
run: function (callback) {
MonitoringService.sendMonitoringMails(function (err) {
if (err) {
Logger.tag('monitoring', 'mail-sending').error('Error sending monitoring mails:', err);
}
callback();
});
}
}

View file

@ -0,0 +1,20 @@
import Logger from "../logger";
import MonitoringService from "../services/monitoringService";
export default {
name: 'MonitoringMailsSendingJob',
description: 'Sends monitoring emails depending on the monitoring state of nodes retrieved by the NodeInformationRetrievalJob.',
run: (): Promise<void> => {
return new Promise<void>((resolve, reject) => {
MonitoringService.sendMonitoringMails((err: any): void => {
if (err) {
Logger.tag('monitoring', 'mail-sending').error('Error sending monitoring mails:', err);
return reject(err);
}
resolve();
});
});
}
};

View file

@ -1,18 +0,0 @@
'use strict';
const Logger = require('../logger')
const MonitoringService = require('../services/monitoringService')
module.exports = {
description: 'Fetches the nodes.json and calculates and stores the monitoring / online status for registered nodes.',
run: function (callback) {
MonitoringService.retrieveNodeInformation(function (err) {
if (err) {
Logger.tag('monitoring', 'information-retrieval').error('Error retrieving node data:', err);
}
callback();
});
}
}

View file

@ -0,0 +1,22 @@
import Logger from "../logger";
import MonitoringService from "../services/monitoringService";
export default {
name: 'NodeInformationRetrievalJob',
description: 'Fetches the nodes.json and calculates and stores the monitoring / online status for registered nodes.',
run: (): Promise<void> => {
return new Promise<void>(
(resolve, reject) => {
MonitoringService.retrieveNodeInformation((err: any): void => {
if (err) {
Logger.tag('monitoring', 'information-retrieval').error('Error retrieving node data:', err);
return reject(err);
}
resolve();
});
}
);
}
};

View file

@ -1,18 +0,0 @@
'use strict';
const Logger = require('../logger')
const MonitoringService = require('../services/monitoringService')
module.exports = {
description: 'Delete nodes that are offline for more than 100 days.',
run: function (callback) {
MonitoringService.deleteOfflineNodes(function (err) {
if (err) {
Logger.tag('nodes', 'delete-offline').error('Error deleting offline nodes:', err);
}
callback();
});
}
}

View file

@ -0,0 +1,20 @@
import MonitoringService from "../services/monitoringService";
import Logger from "../logger";
export default {
name: 'OfflineNodesDeletionJob',
description: 'Delete nodes that are offline for more than 100 days.',
run: (): Promise<void> => {
return new Promise<void>((resolve, reject) => {
MonitoringService.deleteOfflineNodes((err: any): void => {
if (err) {
Logger.tag('nodes', 'delete-offline').error('Error deleting offline nodes:', err);
return reject(err);
}
resolve();
});
});
}
};

View file

@ -1,99 +0,0 @@
'use strict';
const _ = require('lodash');
const cron = require('node-cron');
const glob = require('glob');
const moment = require('moment');
const config = require('../config').config
const Logger = require('../logger')
const jobFiles = glob.sync(__dirname + '/*Job.js');
_.each(jobFiles, function (jobFile) {
require(jobFile);
});
const tasks = {};
let taskId = 1;
function nextTaskId() {
const id = taskId;
taskId += 1;
return id;
}
function schedule(expr, jobName) {
Logger.tag('jobs').info('Scheduling job: %s %s', expr, jobName);
var job = require(`../jobs/${jobName}`);
if (!_.isFunction(job.run)) {
throw new Error('The job ' + jobName + ' does not provide a "run" function.');
}
var id = nextTaskId();
var task = {
id: id,
name: jobName,
description: job.description,
schedule: expr,
job: job,
runningSince: false,
lastRunStarted: false,
lastRunDuration: null,
state: 'idle',
enabled: true
};
task.run = function () {
if (task.runningSince || !task.enabled) {
// job is still running, skip execution
return;
}
task.runningSince = moment();
task.lastRunStarted = task.runningSince;
task.state = 'running';
job.run(function () {
var now = moment();
var duration = now.diff(task.runningSince);
Logger.tag('jobs').profile('[%sms]\t%s', duration, task.name);
task.runningSince = false;
task.lastRunDuration = duration;
task.state = 'idle';
});
};
cron.schedule(expr, task.run);
tasks['' + id] = task;
}
module.exports = {
init: function () {
Logger.tag('jobs').info('Scheduling background jobs...');
try {
schedule('0 */1 * * * *', 'MailQueueJob');
schedule('15 */1 * * * *', 'FixNodeFilenamesJob');
if (config.client.monitoring.enabled) {
schedule('30 */15 * * * *', 'NodeInformationRetrievalJob');
schedule('45 */5 * * * *', 'MonitoringMailsSendingJob');
schedule('0 0 3 * * *', 'OfflineNodesDeletionJob'); // every night at 3:00
}
}
catch (error) {
Logger.tag('jobs').error('Error during scheduling of background jobs:', error);
throw error;
}
Logger.tag('jobs').info('Scheduling of background jobs done.');
},
getTasks: function () {
return tasks;
}
}

126
server/jobs/scheduler.ts Normal file
View file

@ -0,0 +1,126 @@
import cron from "node-cron";
import moment from "moment";
import {config} from "../config";
import Logger from "../logger";
import MailQueueJob from "./MailQueueJob";
import FixNodeFilenamesJob from "./FixNodeFilenamesJob";
import NodeInformationRetrievalJob from "./NodeInformationRetrievalJob";
import MonitoringMailsSendingJob from "./MonitoringMailsSendingJob";
import OfflineNodesDeletionJob from "./OfflineNodesDeletionJob";
export interface Job {
name: string,
description: string,
run(): Promise<void>,
}
export enum TaskState {
IDLE = "idle",
RUNNING = "running",
FAILED = "failed",
}
export class Task {
constructor(
public id: number,
public name: string,
public description: string,
public schedule: string,
public job: Job,
public runningSince: moment.Moment | null,
public lastRunStarted: moment.Moment | null,
public lastRunDuration: number | null,
public state: TaskState,
public enabled: true,
) {}
run(): void {
if (this.runningSince || !this.enabled) {
// job is still running, skip execution
return;
}
this.runningSince = moment();
this.lastRunStarted = this.runningSince;
this.state = TaskState.RUNNING;
const done = (state: TaskState):void => {
const now = moment();
const duration = now.diff(this.runningSince || now);
Logger.tag('jobs').profile('[%sms]\t%s', duration, this.name);
this.runningSince = null;
this.lastRunDuration = duration;
this.state = state;
};
this.job.run().then(() => {
done(TaskState.IDLE);
}).catch((err: any) => {
Logger.tag('jobs').error("Job %s failed: %s", this.name, err);
done(TaskState.FAILED);
});
}
}
type Tasks = {[key: string]: Task};
const tasks: Tasks = {};
let taskId = 1;
function nextTaskId(): number {
const id = taskId;
taskId += 1;
return id;
}
function schedule(expr: string, job: Job): void {
Logger.tag('jobs').info('Scheduling job: %s %s', expr, job.name);
const id = nextTaskId();
const task = new Task(
id,
job.name,
job.description,
expr,
job,
null,
null,
null,
TaskState.IDLE,
true
);
cron.schedule(expr, task.run);
tasks['' + id] = task;
}
export function init() {
Logger.tag('jobs').info('Scheduling background jobs...');
try {
schedule('0 */1 * * * *', MailQueueJob);
schedule('15 */1 * * * *', FixNodeFilenamesJob);
if (config.client.monitoring.enabled) {
schedule('30 */15 * * * *', NodeInformationRetrievalJob);
schedule('45 */5 * * * *', MonitoringMailsSendingJob);
schedule('0 0 3 * * *', OfflineNodesDeletionJob); // every night at 3:00
}
}
catch (error) {
Logger.tag('jobs').error('Error during scheduling of background jobs:', error);
throw error;
}
Logger.tag('jobs').info('Scheduling of background jobs done.');
}
export function getTasks(): Tasks {
return tasks;
}