Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
36bff0a70a | |||
fa8fd9fa35 | |||
dd783b455f | |||
996685e7fb | |||
110b2c00cf | |||
c8b455b8e2 | |||
d16fd8b69b | |||
f7ed88cd57 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smarttime",
|
||||
"version": "3.0.29",
|
||||
"version": "3.0.33",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -1,14 +1,14 @@
|
||||
{
|
||||
"name": "@pushrocks/smarttime",
|
||||
"private": false,
|
||||
"version": "3.0.29",
|
||||
"version": "3.0.33",
|
||||
"description": "handle time in smart ways",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest ./test/)",
|
||||
"test": "(tstest ./test)",
|
||||
"build": "(tsbuild --web && tsbundle npm)"
|
||||
},
|
||||
"devDependencies": {
|
||||
@ -25,7 +25,6 @@
|
||||
"@pushrocks/lik": "^4.0.17",
|
||||
"@pushrocks/smartdelay": "^2.0.10",
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"croner": "^1.1.23",
|
||||
"dayjs": "^1.8.35",
|
||||
"is-nan": "^1.3.0"
|
||||
},
|
||||
|
@ -1,10 +1,12 @@
|
||||
import * as plugins from './smarttime.plugins';
|
||||
import { CronManager } from './smarttime.classes.cronmanager';
|
||||
|
||||
import { CronParser } from './smarttime.classes.cronparser';
|
||||
|
||||
export type TJobFunction = (() => void) | (() => Promise<any>);
|
||||
|
||||
export class CronJob {
|
||||
public croner;
|
||||
public cronParser: CronParser;
|
||||
public status: 'started' | 'stopped' | 'initial' = 'initial';
|
||||
public cronExpression: string;
|
||||
public jobFunction: TJobFunction;
|
||||
@ -13,7 +15,7 @@ export class CronJob {
|
||||
constructor(cronManager: CronManager, cronExpressionArg: string, jobFunction: TJobFunction) {
|
||||
this.cronExpression = cronExpressionArg;
|
||||
this.jobFunction = jobFunction;
|
||||
this.croner = plugins.croner(this.cronExpression);
|
||||
this.cronParser = new CronParser(cronExpressionArg);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -41,7 +43,7 @@ export class CronJob {
|
||||
* gets the time to next execution
|
||||
*/
|
||||
public getTimeToNextExecution() {
|
||||
return this.croner.msToNext();
|
||||
return this.cronParser.getMsToNextTimeMatch();
|
||||
}
|
||||
|
||||
public start() {
|
||||
|
@ -46,10 +46,17 @@ export class CronManager {
|
||||
nextRunningCronjob = cronJob;
|
||||
}
|
||||
}
|
||||
if (nextRunningCronjob) {
|
||||
this.executionTimeout = new plugins.smartdelay.Timeout(nextRunningCronjob.getTimeToNextExecution());
|
||||
console.log(
|
||||
`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds`
|
||||
);
|
||||
} else {
|
||||
this.executionTimeout = new plugins.smartdelay.Timeout(1000);
|
||||
console.log('no cronjobs specified! Checking again in 1 second');
|
||||
}
|
||||
|
||||
|
||||
await this.executionTimeout.promise;
|
||||
} while (this.status === 'started');
|
||||
};
|
||||
|
71
ts/smarttime.classes.cronparser.ts
Normal file
71
ts/smarttime.classes.cronparser.ts
Normal file
@ -0,0 +1,71 @@
|
||||
import * as plugins from './smarttime.plugins';
|
||||
|
||||
export class CronParser {
|
||||
public cronExpression: string;
|
||||
public get cronArray() {
|
||||
return this.cronExpression.split(' ');
|
||||
}
|
||||
constructor(cronExpressionArg: string) {
|
||||
this.cronExpression = cronExpressionArg;
|
||||
if (this.cronArray.length < 6) {
|
||||
throw new Error('CronParser needs second level accuracy');
|
||||
}
|
||||
}
|
||||
|
||||
private getNextPartMatch(cronPart: string, startValue: number, moduloArg: number) {
|
||||
if (cronPart === '*') {
|
||||
return startValue;
|
||||
}
|
||||
if (cronPart.includes('/')) {
|
||||
const every = parseInt(cronPart.split('/')[1], 10);
|
||||
const findEvenMatch = (recursionStartArg: number) => {
|
||||
if (recursionStartArg % every === 0) {
|
||||
return recursionStartArg;
|
||||
} else {
|
||||
return findEvenMatch(recursionStartArg + 1);
|
||||
}
|
||||
};
|
||||
return findEvenMatch(startValue);
|
||||
}
|
||||
if (parseInt(cronPart, 10)) {
|
||||
const match = parseInt(cronPart, 10);
|
||||
return match;
|
||||
}
|
||||
}
|
||||
|
||||
public getMsToNextTimeMatch() {
|
||||
const cronArray = this.cronArray;
|
||||
const secondExpression = cronArray[0];
|
||||
const minuteExpression = cronArray[1];
|
||||
const hourExpression = cronArray[2];
|
||||
const dayExpression = cronArray[3];
|
||||
const monthExpression = cronArray[4];
|
||||
const yearExpression = cronArray[5];
|
||||
|
||||
const currentDate = new Date();
|
||||
const currentSecond = currentDate.getSeconds() + 1;
|
||||
const currentMinute = currentDate.getMinutes();
|
||||
const currentHour = currentDate.getHours();
|
||||
const currentDay = currentDate.getDate();
|
||||
const currentMonth = currentDate.getMonth();
|
||||
const currentYear = currentDate.getFullYear();
|
||||
|
||||
const targetSecond = this.getNextPartMatch(secondExpression, currentSecond, 59);
|
||||
const targetMinute = this.getNextPartMatch(minuteExpression, currentMinute, 59);
|
||||
const targetHour = this.getNextPartMatch(hourExpression, currentHour, 23);
|
||||
const targetDay = currentDay;
|
||||
const targetMonth = currentMonth;
|
||||
const targetYear = currentYear;
|
||||
|
||||
const targetDate = new Date(
|
||||
targetYear,
|
||||
targetMonth,
|
||||
targetDay,
|
||||
targetHour,
|
||||
targetMinute,
|
||||
targetSecond
|
||||
);
|
||||
const targetTime = targetDate.getTime();
|
||||
return targetTime - Date.now();
|
||||
}
|
||||
}
|
@ -6,7 +6,6 @@ import * as smartpromise from '@pushrocks/smartpromise';
|
||||
export { lik, smartdelay, smartpromise };
|
||||
|
||||
// third parties
|
||||
import croner from 'croner';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
export { croner, dayjs };
|
||||
export { dayjs };
|
||||
|
Reference in New Issue
Block a user