Compare commits

..

8 Commits

Author SHA1 Message Date
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
ff16b70206 3.0.27 2020-09-03 20:06:03 +00:00
8e43ce7e9b fix(core): update 2020-09-03 20:06:02 +00:00
6 changed files with 61 additions and 28 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smarttime", "name": "@pushrocks/smarttime",
"version": "3.0.26", "version": "3.0.30",
"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.26", "version": "3.0.30",
"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

@ -11,16 +11,38 @@ 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();
const done2 = tools.defer();
const done3 = tools.defer();
let counter = 0; let counter = 0;
testCronManager.addCronjob('*/2 * * * * *', () => { let counter2 = 0;
let counter3 = 0;
const cronJob = testCronManager.addCronjob('*/2 * * * * *', async () => {
if (counter === 10) { if (counter === 10) {
testCronManager.removeCronjob(cronJob);
done.resolve(); done.resolve();
} }
counter++; counter++;
console.log(`hey ${counter}`); console.log(`${new Date().getSeconds()} hey ${counter} -> runs every 2 seconds`);
});
const cronJob2 = testCronManager.addCronjob('*/3 * * * * *', async () => {
if (counter2 === 10) {
testCronManager.removeCronjob(cronJob2);
done2.resolve();
}
counter2++;
console.log(`${new Date().getSeconds()} hey ${counter2} -> runs every 3 seconds`);
});
const cronJob3 = testCronManager.addCronjob('*/4 * * * * *', async () => {
if (counter3 === 10) {
done3.resolve();
}
counter3++;
console.log(`${new Date().getSeconds()} hey ${counter3} -> runs every 4 seconds`);
}); });
testCronManager.start(); testCronManager.start();
await done.promise; await done.promise;
await done2.promise;
await done3.promise;
testCronManager.stop(); testCronManager.stop();
}); });

View File

@ -21,15 +21,29 @@ export class CronJob {
*/ */
public checkExecution(): number { public checkExecution(): number {
if (this.nextExecutionUnix === 0) { if (this.nextExecutionUnix === 0) {
this.nextExecutionUnix = this.croner.msToNext(); this.getNextExecutionTime();
} }
if (Date.now() > this.nextExecutionUnix) { if (Date.now() > this.nextExecutionUnix) {
this.jobFunction(); const maybePromise = this.jobFunction();
this.nextExecutionUnix = this.croner.msToNext(); if (maybePromise instanceof Promise) {
maybePromise.catch(e => console.log(e));
}
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

@ -9,7 +9,7 @@ export class CronManager {
constructor() {} constructor() {}
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => any) { public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => Promise<void>) {
const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg); const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg);
this.cronjobs.add(newCronJob); this.cronjobs.add(newCronJob);
if (this.status === 'started') { if (this.status === 'started') {
@ -33,30 +33,27 @@ export class CronManager {
for (const cronJob of this.cronjobs.getArray()) { for (const cronJob of this.cronjobs.getArray()) {
cronJob.start(); cronJob.start();
} }
this.executionTimeout = new plugins.smartdelay.Timeout(0); const runCronCycle = async () => {
this.executionTimeout = new plugins.smartdelay.Timeout(0);
// recursion do {
const runCheckExecution = () => { let nextRunningCronjob: CronJob;
console.log(
`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds`
);
this.executionTimeout.promise.then(() => {
let timeToNextOverallExecution: number;
for (const cronJob of this.cronjobs.getArray()) { for (const cronJob of this.cronjobs.getArray()) {
const timeToNextJobExecution = cronJob.checkExecution(); cronJob.checkExecution();
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());
runCheckExecution(); console.log(
}); `Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds`
);
await this.executionTimeout.promise;
} while (this.status === 'started');
}; };
runCronCycle();
runCheckExecution();
} }
} }