fix(core): update
This commit is contained in:
		| @@ -1,6 +1,7 @@ | ||||
| export * from './smarttime.classes.cronmanager'; | ||||
| export * from './smarttime.classes.extendeddate'; | ||||
| export * from './smarttime.classes.hrtmeasurement'; | ||||
| export * from './smarttime.classes.interval'; | ||||
| export * from './smarttime.classes.timer'; | ||||
| export * from './smarttime.classes.timestamp'; | ||||
| export * from './smarttime.units'; | ||||
|   | ||||
| @@ -4,48 +4,37 @@ import { CronManager } from './smarttime.classes.cronmanager'; | ||||
| export type TJobFunction = (() => void) | (() => Promise<any>); | ||||
|  | ||||
| export class CronJob { | ||||
|   public croner;  | ||||
|   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; | ||||
|     this.croner = plugins.croner(this.cronExpression); | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * checks wether the cronjob needs to be executed | ||||
|    */ | ||||
|   public checkExecution() { | ||||
|   public checkExecution(): number { | ||||
|     if (this.nextExecutionUnix === 0) { | ||||
|       this.nextExecutionUnix = this.cronInterval | ||||
|         .next() | ||||
|         .toDate() | ||||
|         .getTime(); | ||||
|       return; | ||||
|       this.nextExecutionUnix = this.croner.msToNext(); | ||||
|     } | ||||
|     if (Date.now() > this.nextExecutionUnix) { | ||||
|       this.jobFunction(); | ||||
|       this.nextExecutionUnix = this.cronInterval | ||||
|         .next() | ||||
|         .toDate() | ||||
|         .getTime(); | ||||
|       this.nextExecutionUnix = this.croner.msToNext(); | ||||
|     } | ||||
|     return this.nextExecutionUnix; | ||||
|   } | ||||
|  | ||||
|   public start() { | ||||
|     this.cronInterval = this.getCronInterval(); | ||||
|     this.status = 'started'; | ||||
|   } | ||||
|  | ||||
|   public stop() { | ||||
|     this.status = 'stopped'; | ||||
|   } | ||||
|  | ||||
|   private getCronInterval() { | ||||
|     return plugins.cronParser.parseExpression(this.cronExpression); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -1,21 +1,13 @@ | ||||
| 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 executionTimeout: plugins.smartdelay.Timeout<void>; | ||||
|  | ||||
|   public status: 'started' | 'stopped' = 'stopped'; | ||||
|   public cronjobs: CronJob[] = []; | ||||
|  | ||||
|   constructor() { | ||||
|     this.cronInterval.addIntervalJob(() => { | ||||
|       for (const cronJob of this.cronjobs) { | ||||
|         cronJob.checkExecution(); | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
|   constructor() {} | ||||
|  | ||||
|   public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => any) { | ||||
|     const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg); | ||||
| @@ -30,10 +22,28 @@ export class CronManager { | ||||
|    */ | ||||
|   public start() { | ||||
|     this.status = 'started'; | ||||
|     for (const cron of this.cronjobs) { | ||||
|       cron.start(); | ||||
|     for (const cronJob of this.cronjobs) { | ||||
|       cronJob.start(); | ||||
|     } | ||||
|     this.cronInterval.start(); | ||||
|     this.executionTimeout = new plugins.smartdelay.Timeout(0); | ||||
|  | ||||
|     // recursion | ||||
|     const runCheckExecution = () => { | ||||
|       console.log(`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds`); | ||||
|       this.executionTimeout.promise.then(() => { | ||||
|         let timeToNextOverallExecution: number; | ||||
|         for (const cronJob of this.cronjobs) { | ||||
|           const timeToNextJobExecution = cronJob.checkExecution(); | ||||
|           if (timeToNextJobExecution < timeToNextOverallExecution || !timeToNextOverallExecution) { | ||||
|             timeToNextOverallExecution = timeToNextJobExecution; | ||||
|           } | ||||
|         } | ||||
|         this.executionTimeout = new plugins.smartdelay.Timeout(timeToNextOverallExecution); | ||||
|         runCheckExecution(); | ||||
|       }); | ||||
|     }; | ||||
|  | ||||
|     runCheckExecution(); | ||||
|   } | ||||
|  | ||||
|   /** | ||||
| @@ -41,9 +51,9 @@ export class CronManager { | ||||
|    */ | ||||
|   public stop() { | ||||
|     this.status = 'stopped'; | ||||
|     this.executionTimeout.cancel(); | ||||
|     for (const cron of this.cronjobs) { | ||||
|       cron.stop(); | ||||
|     } | ||||
|     this.cronInterval.stop(); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -26,12 +26,12 @@ export class ExtendedDate extends Date { | ||||
|  | ||||
|   public static fromEuropeanDate(europeanDate: string) { | ||||
|     const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDate); | ||||
|     const luxonDate = plugins.luxon.DateTime.utc( | ||||
|     const date = new Date( | ||||
|       parseFloat(dateArray[3]), // year | ||||
|       parseFloat(dateArray[2]), // month | ||||
|       parseFloat(dateArray[2]) - 1, // month | ||||
|       parseFloat(dateArray[1]) // day | ||||
|     ); | ||||
|     const unixMilli = luxonDate.toMillis(); | ||||
|     const unixMilli = date.getTime(); | ||||
|     return new ExtendedDate(unixMilli); | ||||
|   } | ||||
|  | ||||
| @@ -64,10 +64,8 @@ export class ExtendedDate extends Date { | ||||
|     const dateTimeString = `${dateArray[3]}-${sliceDate(dateArray[2])}-${sliceDate( | ||||
|       dateArray[1] | ||||
|     )}T${timeArg}`; | ||||
|     const luxonDate = plugins.luxon.DateTime.fromISO(dateTimeString, { | ||||
|       zone: zoneArg | ||||
|     }); | ||||
|     const unixMilli = luxonDate.toMillis(); | ||||
|     const date = plugins.dayjs(dateTimeString); | ||||
|     const unixMilli = date.toDate().getTime(); | ||||
|     return new ExtendedDate(unixMilli); | ||||
|   } | ||||
|  | ||||
|   | ||||
| @@ -4,7 +4,10 @@ export class Interval { | ||||
|   public status: 'started' | 'stopped' | 'initial' = 'initial'; | ||||
|   private statusAuthorization: any = null; | ||||
|  | ||||
|   // timings | ||||
|   public intervalMilliseconds: number; | ||||
|   public nextIntervalMillisenconds: number; | ||||
|  | ||||
|   public intervalJobs: Array<() => any> = []; | ||||
|   constructor(intervalMillisencondsArg: number) { | ||||
|     this.intervalMilliseconds = intervalMillisencondsArg; | ||||
|   | ||||
| @@ -5,7 +5,7 @@ import * as smartpromise from '@pushrocks/smartpromise'; | ||||
| export { smartdelay, smartpromise }; | ||||
|  | ||||
| // third parties | ||||
| import cronParser from 'cron-parser'; | ||||
| import * as luxon from 'luxon'; | ||||
| import croner from 'croner'; | ||||
| import dayjs from 'dayjs'; | ||||
|  | ||||
| export { cronParser, luxon }; | ||||
| export { croner, dayjs }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user