fix(core): update
This commit is contained in:
45
ts/smarttime.classes.cronjob.ts
Normal file
45
ts/smarttime.classes.cronjob.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import * as plugins from './smarttime.plugins';
|
||||
import { CronManager } from './smarttime.classes.cronmanager';
|
||||
|
||||
export type TJobFunction = (() => void) | (() => Promise<any>);
|
||||
|
||||
export class CronJob {
|
||||
public status: 'started' | 'stopped' | 'initial' = 'initial';
|
||||
public cronExpression: string;
|
||||
public jobFunction: TJobFunction;
|
||||
|
||||
private cronInterval= plugins.cronParser.parseExpression('* * * * * *');
|
||||
private nextExecutionUnix: number = 0;
|
||||
|
||||
constructor(cronManager: CronManager, cronExpressionArg: string, jobFunction: TJobFunction ) {
|
||||
this.cronExpression = cronExpressionArg;
|
||||
this.jobFunction = jobFunction;
|
||||
}
|
||||
|
||||
/**
|
||||
* checks wether the cronjob needs to be executed
|
||||
*/
|
||||
public checkExecution() {
|
||||
if (this.nextExecutionUnix === 0) {
|
||||
this.nextExecutionUnix = this.cronInterval.next().toDate().getTime();
|
||||
return;
|
||||
}
|
||||
if (Date.now() > this.nextExecutionUnix) {
|
||||
this.jobFunction();
|
||||
this.nextExecutionUnix = this.cronInterval.next().toDate().getTime();
|
||||
}
|
||||
}
|
||||
|
||||
public start() {
|
||||
this.cronInterval = this.getCronInterval();
|
||||
this.status = 'started';
|
||||
}
|
||||
|
||||
public stop() {
|
||||
this.status = 'stopped';
|
||||
}
|
||||
|
||||
private getCronInterval () {
|
||||
return plugins.cronParser.parseExpression(this.cronExpression);
|
||||
}
|
||||
}
|
@ -1,16 +1,28 @@
|
||||
import * as plugins from './smarttime.plugins';
|
||||
import { CronJob } from './smarttime.classes.cronjob';
|
||||
import { Timer } from './smarttime.classes.timer';
|
||||
import { Interval } from './smarttime.classes.interval';
|
||||
|
||||
export class CronManager {
|
||||
public cronInterval = new Interval(1000);
|
||||
|
||||
public status: 'started' | 'stopped' = 'stopped';
|
||||
public cronjobs: CronJob[] = [];
|
||||
|
||||
public cronjobs: plugins.cron.CronJob[] = [];
|
||||
constructor() {
|
||||
this.cronInterval.addIntervalJob(() => {
|
||||
for (const cronJob of this.cronjobs) {
|
||||
cronJob.checkExecution();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public addCronjob(cronIdentifierArg: string, cronFunctionArg: plugins.cron.CronCommand) {
|
||||
const newCronJob = new plugins.cron.CronJob(cronIdentifierArg, cronFunctionArg);
|
||||
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => any) {
|
||||
const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg);
|
||||
this.cronjobs.push(newCronJob);
|
||||
if (this.status === 'started') {
|
||||
newCronJob.start();
|
||||
}
|
||||
this.cronjobs.push(newCronJob);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -21,6 +33,7 @@ export class CronManager {
|
||||
for (const cron of this.cronjobs) {
|
||||
cron.start();
|
||||
}
|
||||
this.cronInterval.start();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -31,5 +44,6 @@ export class CronManager {
|
||||
for (const cron of this.cronjobs) {
|
||||
cron.stop();
|
||||
}
|
||||
this.cronInterval.stop();
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,3 @@
|
||||
import * as process from 'process';
|
||||
|
||||
/**
|
||||
* easy high resolution time measurement
|
||||
*/
|
||||
|
40
ts/smarttime.classes.interval.ts
Normal file
40
ts/smarttime.classes.interval.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import * as plugins from './smarttime.plugins';
|
||||
|
||||
export class Interval {
|
||||
public status: 'started' | 'stopped' | 'initial' = 'initial';
|
||||
private statusAuthorization: any = null;
|
||||
|
||||
public intervalMilliseconds: number;
|
||||
public intervalJobs: Array<() => any> = [];
|
||||
constructor(intervalMillisencondsArg: number) {
|
||||
this.intervalMilliseconds = intervalMillisencondsArg;
|
||||
}
|
||||
|
||||
public start() {
|
||||
this.status = 'started';
|
||||
const statusAuth = new Date();
|
||||
this.statusAuthorization = statusAuth;
|
||||
const runInterval = async () => {
|
||||
while (this.status === 'started' && this.statusAuthorization === statusAuth) {
|
||||
await plugins.smartdelay.delayFor(this.intervalMilliseconds);
|
||||
this.executeIntervalJobs();
|
||||
}
|
||||
};
|
||||
runInterval();
|
||||
}
|
||||
|
||||
public stop () {
|
||||
this.status = 'stopped';
|
||||
this.statusAuthorization = null;
|
||||
}
|
||||
|
||||
public addIntervalJob(funcArg: () => any) {
|
||||
this.intervalJobs.push(funcArg);
|
||||
}
|
||||
|
||||
private executeIntervalJobs () {
|
||||
for (const funcArg of this.intervalJobs) {
|
||||
funcArg();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,11 @@
|
||||
// @pushrocks scope
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
|
||||
export { smartpromise };
|
||||
export { smartdelay, smartpromise };
|
||||
|
||||
// third parties
|
||||
import cronParser from 'cron-parser';
|
||||
import * as luxon from 'luxon';
|
||||
import * as cron from 'cron';
|
||||
|
||||
export { luxon, cron };
|
||||
export { cronParser, luxon };
|
||||
|
Reference in New Issue
Block a user