Compare commits

..

4 Commits

Author SHA1 Message Date
cfcfde2132 3.0.35 2020-09-07 23:44:08 +00:00
b5ab945501 fix(core): update 2020-09-07 23:44:07 +00:00
88378e2f31 3.0.34 2020-09-07 17:16:12 +00:00
db92311daa fix(core): update 2020-09-07 17:16:11 +00:00
6 changed files with 45 additions and 15 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smarttime", "name": "@pushrocks/smarttime",
"version": "3.0.33", "version": "3.0.35",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,7 +1,7 @@
{ {
"name": "@pushrocks/smarttime", "name": "@pushrocks/smarttime",
"private": false, "private": false,
"version": "3.0.33", "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",
@ -25,6 +25,7 @@
"@pushrocks/lik": "^4.0.17", "@pushrocks/lik": "^4.0.17",
"@pushrocks/smartdelay": "^2.0.10", "@pushrocks/smartdelay": "^2.0.10",
"@pushrocks/smartpromise": "^3.0.2", "@pushrocks/smartpromise": "^3.0.2",
"croner": "^1.1.23",
"dayjs": "^1.8.35", "dayjs": "^1.8.35",
"is-nan": "^1.3.0" "is-nan": "^1.3.0"
}, },

View File

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

View File

@ -6,7 +6,7 @@ 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 cronParser: CronParser; 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;
@ -15,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.cronParser = new CronParser(cronExpressionArg); this.cronParser = plugins.croner(cronExpressionArg);
} }
/** /**
@ -43,7 +43,7 @@ export class CronJob {
* gets the time to next execution * gets the time to next execution
*/ */
public getTimeToNextExecution() { public getTimeToNextExecution() {
return this.cronParser.getMsToNextTimeMatch(); return this.cronParser.msToNext();
} }
public start() { public start() {

View File

@ -27,13 +27,13 @@ export class CronParser {
}; };
return findEvenMatch(startValue); return findEvenMatch(startValue);
} }
if (parseInt(cronPart, 10)) { if (parseInt(cronPart, 10) || cronPart === '0') {
const match = parseInt(cronPart, 10); const match = parseInt(cronPart, 10);
return match; return match;
} }
} }
public getMsToNextTimeMatch() { public msToNext() {
const cronArray = this.cronArray; const cronArray = this.cronArray;
const secondExpression = cronArray[0]; const secondExpression = cronArray[0];
const minuteExpression = cronArray[1]; const minuteExpression = cronArray[1];
@ -42,19 +42,36 @@ export class CronParser {
const monthExpression = cronArray[4]; const monthExpression = cronArray[4];
const yearExpression = cronArray[5]; const yearExpression = cronArray[5];
const currentDate = new Date(); let currentDate = new Date();
const currentSecond = currentDate.getSeconds() + 1; let currentSecond = currentDate.getSeconds() + 1;
const currentMinute = currentDate.getMinutes(); let currentMinute = currentDate.getMinutes();
const currentHour = currentDate.getHours(); let currentHour = currentDate.getHours();
const currentDay = currentDate.getDate(); let currentDay = currentDate.getDate();
const currentMonth = currentDate.getMonth(); let currentMonth = currentDate.getMonth();
const currentYear = currentDate.getFullYear(); let currentYear = currentDate.getFullYear();
const targetSecond = this.getNextPartMatch(secondExpression, currentSecond, 59); const targetSecond = this.getNextPartMatch(secondExpression, currentSecond, 59);
if (targetSecond < currentSecond) {
currentMinute = (currentMinute + 1) % 59;
}
const targetMinute = this.getNextPartMatch(minuteExpression, currentMinute, 59); const targetMinute = this.getNextPartMatch(minuteExpression, currentMinute, 59);
if (targetMinute < currentMinute) {
currentHour = (currentHour + 1) % 23;
}
const targetHour = this.getNextPartMatch(hourExpression, currentHour, 23); const targetHour = this.getNextPartMatch(hourExpression, currentHour, 23);
if (targetHour < currentHour) {
currentDay = (currentDay + 1) % 30;
}
const targetDay = currentDay; const targetDay = currentDay;
if (targetDay < currentDay) {
currentMonth = (currentMonth + 1) % 11;
}
const targetMonth = currentMonth; const targetMonth = currentMonth;
if (targetMonth < currentMonth) {
currentYear = (currentYear + 1);
}
const targetYear = currentYear; const targetYear = currentYear;
const targetDate = new Date( const targetDate = new Date(

View File

@ -6,6 +6,7 @@ import * as smartpromise from '@pushrocks/smartpromise';
export { lik, smartdelay, smartpromise }; export { lik, smartdelay, smartpromise };
// third parties // third parties
import croner from 'croner';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
export { dayjs }; export { croner, dayjs };