Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
cfcfde2132 | |||
b5ab945501 | |||
88378e2f31 | |||
db92311daa | |||
36bff0a70a | |||
fa8fd9fa35 | |||
dd783b455f | |||
996685e7fb | |||
110b2c00cf | |||
c8b455b8e2 | |||
d16fd8b69b | |||
f7ed88cd57 | |||
19b5ba4a5d | |||
457182a97a | |||
a7a961c869 | |||
f1d7771e30 |
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smarttime",
|
"name": "@pushrocks/smarttime",
|
||||||
"version": "3.0.27",
|
"version": "3.0.35",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smarttime",
|
"name": "@pushrocks/smarttime",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "3.0.27",
|
"version": "3.0.35",
|
||||||
"description": "handle time in smart ways",
|
"description": "handle time in smart ways",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest ./test/)",
|
"test": "(tstest ./test)",
|
||||||
"build": "(tsbuild --web && tsbundle npm)"
|
"build": "(tsbuild --web && tsbundle npm)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -12,7 +12,7 @@ tap.test('should create a valid instance of cronmanager', async () => {
|
|||||||
tap.test('should create a valid cronJon', async (tools) => {
|
tap.test('should create a valid cronJon', async (tools) => {
|
||||||
const done = tools.defer();
|
const done = tools.defer();
|
||||||
let counter = 0;
|
let counter = 0;
|
||||||
testCronManager.addCronjob('*/2 * * * * *', () => {
|
testCronManager.addCronjob('*/2 * * * * *', async () => {
|
||||||
if (counter === 10) {
|
if (counter === 10) {
|
||||||
done.resolve();
|
done.resolve();
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,17 @@ tap.test('should create a valid cronJon', async (tools) => {
|
|||||||
await done2.promise;
|
await done2.promise;
|
||||||
await done3.promise;
|
await done3.promise;
|
||||||
testCronManager.stop();
|
testCronManager.stop();
|
||||||
|
testCronManager.removeCronjob(cronJob3);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('runs every full minute', async (tools) => {
|
||||||
|
const done = tools.defer();
|
||||||
|
const cronJob = testCronManager.addCronjob('0 * * * * *', async () => {
|
||||||
|
done.resolve();
|
||||||
|
});
|
||||||
|
testCronManager.start();
|
||||||
|
await done.promise;
|
||||||
|
testCronManager.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
import * as plugins from './smarttime.plugins';
|
import * as plugins from './smarttime.plugins';
|
||||||
import { CronManager } from './smarttime.classes.cronmanager';
|
import { CronManager } from './smarttime.classes.cronmanager';
|
||||||
|
|
||||||
|
import { CronParser } from './smarttime.classes.cronparser';
|
||||||
|
|
||||||
export type TJobFunction = (() => void) | (() => Promise<any>);
|
export type TJobFunction = (() => void) | (() => Promise<any>);
|
||||||
|
|
||||||
export class CronJob {
|
export class CronJob {
|
||||||
public croner;
|
public cronParser: CronParser | typeof plugins.croner;
|
||||||
public status: 'started' | 'stopped' | 'initial' = 'initial';
|
public status: 'started' | 'stopped' | 'initial' = 'initial';
|
||||||
public cronExpression: string;
|
public cronExpression: string;
|
||||||
public jobFunction: TJobFunction;
|
public jobFunction: TJobFunction;
|
||||||
@ -13,7 +15,7 @@ export class CronJob {
|
|||||||
constructor(cronManager: CronManager, cronExpressionArg: string, jobFunction: TJobFunction) {
|
constructor(cronManager: CronManager, cronExpressionArg: string, jobFunction: TJobFunction) {
|
||||||
this.cronExpression = cronExpressionArg;
|
this.cronExpression = cronExpressionArg;
|
||||||
this.jobFunction = jobFunction;
|
this.jobFunction = jobFunction;
|
||||||
this.croner = plugins.croner(this.cronExpression);
|
this.cronParser = plugins.croner(cronExpressionArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,18 +23,29 @@ export class CronJob {
|
|||||||
*/
|
*/
|
||||||
public checkExecution(): number {
|
public checkExecution(): number {
|
||||||
if (this.nextExecutionUnix === 0) {
|
if (this.nextExecutionUnix === 0) {
|
||||||
this.nextExecutionUnix = Date.now() + this.croner.msToNext();
|
this.getNextExecutionTime();
|
||||||
}
|
}
|
||||||
if (Date.now() > this.nextExecutionUnix) {
|
if (Date.now() > this.nextExecutionUnix) {
|
||||||
const maybePromise = this.jobFunction();
|
const maybePromise = this.jobFunction();
|
||||||
if (maybePromise instanceof Promise) {
|
if (maybePromise instanceof Promise) {
|
||||||
maybePromise.catch(e => console.log(e));
|
maybePromise.catch(e => console.log(e));
|
||||||
}
|
}
|
||||||
this.nextExecutionUnix = Date.now() + this.croner.msToNext();
|
this.nextExecutionUnix = this.getNextExecutionTime();
|
||||||
}
|
}
|
||||||
return this.nextExecutionUnix;
|
return this.nextExecutionUnix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getNextExecutionTime() {
|
||||||
|
return this.nextExecutionUnix = Date.now() + this.getTimeToNextExecution();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets the time to next execution
|
||||||
|
*/
|
||||||
|
public getTimeToNextExecution() {
|
||||||
|
return this.cronParser.msToNext();
|
||||||
|
}
|
||||||
|
|
||||||
public start() {
|
public start() {
|
||||||
this.status = 'started';
|
this.status = 'started';
|
||||||
}
|
}
|
||||||
|
@ -36,21 +36,27 @@ export class CronManager {
|
|||||||
const runCronCycle = async () => {
|
const runCronCycle = async () => {
|
||||||
this.executionTimeout = new plugins.smartdelay.Timeout(0);
|
this.executionTimeout = new plugins.smartdelay.Timeout(0);
|
||||||
do {
|
do {
|
||||||
let timeToNextOverallExecution: number;
|
let nextRunningCronjob: CronJob;
|
||||||
for (const cronJob of this.cronjobs.getArray()) {
|
for (const cronJob of this.cronjobs.getArray()) {
|
||||||
const nextExecutionTime = cronJob.checkExecution();
|
cronJob.checkExecution();
|
||||||
const timeToNextJobExecution = nextExecutionTime - Date.now();
|
|
||||||
if (
|
if (
|
||||||
timeToNextJobExecution < timeToNextOverallExecution ||
|
!nextRunningCronjob ||
|
||||||
!timeToNextOverallExecution
|
cronJob.getTimeToNextExecution() < nextRunningCronjob.getTimeToNextExecution()
|
||||||
) {
|
) {
|
||||||
timeToNextOverallExecution = timeToNextJobExecution;
|
nextRunningCronjob = cronJob;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.executionTimeout = new plugins.smartdelay.Timeout(timeToNextOverallExecution);
|
if (nextRunningCronjob) {
|
||||||
|
this.executionTimeout = new plugins.smartdelay.Timeout(nextRunningCronjob.getTimeToNextExecution());
|
||||||
console.log(
|
console.log(
|
||||||
`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds`
|
`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;
|
await this.executionTimeout.promise;
|
||||||
} while (this.status === 'started');
|
} while (this.status === 'started');
|
||||||
};
|
};
|
||||||
|
88
ts/smarttime.classes.cronparser.ts
Normal file
88
ts/smarttime.classes.cronparser.ts
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
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) || cronPart === '0') {
|
||||||
|
const match = parseInt(cronPart, 10);
|
||||||
|
return match;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public msToNext() {
|
||||||
|
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];
|
||||||
|
|
||||||
|
let currentDate = new Date();
|
||||||
|
let currentSecond = currentDate.getSeconds() + 1;
|
||||||
|
let currentMinute = currentDate.getMinutes();
|
||||||
|
let currentHour = currentDate.getHours();
|
||||||
|
let currentDay = currentDate.getDate();
|
||||||
|
let currentMonth = currentDate.getMonth();
|
||||||
|
let currentYear = currentDate.getFullYear();
|
||||||
|
|
||||||
|
const targetSecond = this.getNextPartMatch(secondExpression, currentSecond, 59);
|
||||||
|
if (targetSecond < currentSecond) {
|
||||||
|
currentMinute = (currentMinute + 1) % 59;
|
||||||
|
}
|
||||||
|
const targetMinute = this.getNextPartMatch(minuteExpression, currentMinute, 59);
|
||||||
|
if (targetMinute < currentMinute) {
|
||||||
|
currentHour = (currentHour + 1) % 23;
|
||||||
|
}
|
||||||
|
const targetHour = this.getNextPartMatch(hourExpression, currentHour, 23);
|
||||||
|
if (targetHour < currentHour) {
|
||||||
|
currentDay = (currentDay + 1) % 30;
|
||||||
|
}
|
||||||
|
|
||||||
|
const targetDay = currentDay;
|
||||||
|
if (targetDay < currentDay) {
|
||||||
|
currentMonth = (currentMonth + 1) % 11;
|
||||||
|
}
|
||||||
|
|
||||||
|
const targetMonth = currentMonth;
|
||||||
|
if (targetMonth < currentMonth) {
|
||||||
|
currentYear = (currentYear + 1);
|
||||||
|
}
|
||||||
|
const targetYear = currentYear;
|
||||||
|
|
||||||
|
const targetDate = new Date(
|
||||||
|
targetYear,
|
||||||
|
targetMonth,
|
||||||
|
targetDay,
|
||||||
|
targetHour,
|
||||||
|
targetMinute,
|
||||||
|
targetSecond
|
||||||
|
);
|
||||||
|
const targetTime = targetDate.getTime();
|
||||||
|
return targetTime - Date.now();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user