Compare commits

..

2 Commits

Author SHA1 Message Date
19b5ba4a5d 3.0.29 2020-09-04 06:39:22 +00:00
457182a97a fix(core): update 2020-09-04 06:39:22 +00:00
4 changed files with 21 additions and 11 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smarttime", "name": "@pushrocks/smarttime",
"version": "3.0.28", "version": "3.0.29",
"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.28", "version": "3.0.29",
"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",

View File

@ -21,18 +21,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.croner.msToNext();
}
public start() { public start() {
this.status = 'started'; this.status = 'started';
} }

View File

@ -36,18 +36,17 @@ 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); 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`
); );