Compare commits

..

10 Commits

Author SHA1 Message Date
dd783b455f 3.0.32 2020-09-07 12:13:28 +00:00
996685e7fb fix(core): update 2020-09-07 12:13:27 +00:00
110b2c00cf 3.0.31 2020-09-04 15:18:41 +00:00
c8b455b8e2 fix(core): update 2020-09-04 15:18:41 +00:00
d16fd8b69b 3.0.30 2020-09-04 14:24:42 +00:00
f7ed88cd57 fix(core): update 2020-09-04 14:24:42 +00:00
19b5ba4a5d 3.0.29 2020-09-04 06:39:22 +00:00
457182a97a fix(core): update 2020-09-04 06:39:22 +00:00
a7a961c869 3.0.28 2020-09-03 20:14:23 +00:00
f1d7771e30 fix(core): update 2020-09-03 20:14:23 +00:00
6 changed files with 35 additions and 19 deletions

2
package-lock.json generated
View File

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

View File

@ -1,14 +1,14 @@
{ {
"name": "@pushrocks/smarttime", "name": "@pushrocks/smarttime",
"private": false, "private": false,
"version": "3.0.27", "version": "3.0.32",
"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": {

View File

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

View File

@ -13,7 +13,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.croner = plugins.croner(this.cronExpression);
} }
/** /**
@ -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 600000;
}
public start() { public start() {
this.status = 'started'; this.status = 'started';
} }

View File

@ -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) {
console.log( this.executionTimeout = new plugins.smartdelay.Timeout(nextRunningCronjob.getTimeToNextExecution());
`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds` 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; await this.executionTimeout.promise;
} while (this.status === 'started'); } while (this.status === 'started');
}; };

View File

@ -6,7 +6,6 @@ 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 { croner, dayjs }; export { dayjs };