fix(core): update

This commit is contained in:
2020-05-25 21:45:43 +00:00
parent c968e156ae
commit dd722146f4
29 changed files with 19349 additions and 824 deletions

View 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);
}
}

View File

@ -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();
}
}

View File

@ -1,5 +1,3 @@
import * as process from 'process';
/**
* easy high resolution time measurement
*/

View 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();
}
}
}

View File

@ -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 };