Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
ff16b70206 | |||
8e43ce7e9b | |||
f129ce42b6 | |||
56a802664f | |||
743c9fb847 | |||
ac2edd6d74 |
@ -19,23 +19,35 @@ mirror:
|
|||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
audit:
|
auditProductionDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
stage: security
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci command npm install --production --ignore-scripts
|
||||||
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
|
||||||
|
auditDevDependencies:
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
- npmci command npm audit --audit-level=high
|
- npmci command npm audit --audit-level=high --only=dev
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
allow_failure: true
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
@ -50,9 +62,7 @@ testStable:
|
|||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- priv
|
|
||||||
|
|
||||||
testBuild:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
@ -63,9 +73,7 @@ testBuild:
|
|||||||
- npmci command npm run build
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
@ -85,6 +93,8 @@ release:
|
|||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g tslint typescript
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -15,7 +15,7 @@
|
|||||||
"properties": {
|
"properties": {
|
||||||
"projectType": {
|
"projectType": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": ["website", "element", "service", "npm"]
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1813
package-lock.json
generated
1813
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
16
package.json
16
package.json
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smarttime",
|
"name": "@pushrocks/smarttime",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "3.0.24",
|
"version": "3.0.27",
|
||||||
"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",
|
||||||
@ -12,21 +12,21 @@
|
|||||||
"build": "(tsbuild --web && tsbundle npm)"
|
"build": "(tsbuild --web && tsbundle npm)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.11",
|
"@gitzone/tsbuild": "^2.1.25",
|
||||||
"@gitzone/tsbundle": "^1.0.72",
|
"@gitzone/tsbundle": "^1.0.78",
|
||||||
"@gitzone/tsrun": "^1.2.12",
|
"@gitzone/tsrun": "^1.2.12",
|
||||||
"@gitzone/tstest": "^1.0.41",
|
"@gitzone/tstest": "^1.0.44",
|
||||||
"@pushrocks/tapbundle": "^3.2.9",
|
"@pushrocks/tapbundle": "^3.2.9",
|
||||||
"@types/node": "^14.0.22",
|
"@types/node": "^14.6.2",
|
||||||
"tslint": "^6.1.2",
|
"tslint": "^6.1.3",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/lik": "^4.0.13",
|
"@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",
|
"croner": "^1.1.23",
|
||||||
"dayjs": "^1.8.29",
|
"dayjs": "^1.8.35",
|
||||||
"is-nan": "^1.3.0"
|
"is-nan": "^1.3.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
|
@ -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();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -21,11 +21,14 @@ export class CronJob {
|
|||||||
*/
|
*/
|
||||||
public checkExecution(): number {
|
public checkExecution(): number {
|
||||||
if (this.nextExecutionUnix === 0) {
|
if (this.nextExecutionUnix === 0) {
|
||||||
this.nextExecutionUnix = this.croner.msToNext();
|
this.nextExecutionUnix = Date.now() + this.croner.msToNext();
|
||||||
}
|
}
|
||||||
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 = Date.now() + this.croner.msToNext();
|
||||||
}
|
}
|
||||||
return this.nextExecutionUnix;
|
return this.nextExecutionUnix;
|
||||||
}
|
}
|
||||||
|
@ -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,17 +33,13 @@ 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 = () => {
|
|
||||||
console.log(
|
|
||||||
`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds`
|
|
||||||
);
|
|
||||||
this.executionTimeout.promise.then(() => {
|
|
||||||
let timeToNextOverallExecution: number;
|
let timeToNextOverallExecution: number;
|
||||||
for (const cronJob of this.cronjobs.getArray()) {
|
for (const cronJob of this.cronjobs.getArray()) {
|
||||||
const timeToNextJobExecution = cronJob.checkExecution();
|
const nextExecutionTime = cronJob.checkExecution();
|
||||||
|
const timeToNextJobExecution = nextExecutionTime - Date.now();
|
||||||
if (
|
if (
|
||||||
timeToNextJobExecution < timeToNextOverallExecution ||
|
timeToNextJobExecution < timeToNextOverallExecution ||
|
||||||
!timeToNextOverallExecution
|
!timeToNextOverallExecution
|
||||||
@ -52,11 +48,13 @@ export class CronManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.executionTimeout = new plugins.smartdelay.Timeout(timeToNextOverallExecution);
|
this.executionTimeout = new plugins.smartdelay.Timeout(timeToNextOverallExecution);
|
||||||
runCheckExecution();
|
console.log(
|
||||||
});
|
`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds`
|
||||||
|
);
|
||||||
|
await this.executionTimeout.promise;
|
||||||
|
} while (this.status === 'started');
|
||||||
};
|
};
|
||||||
|
runCronCycle();
|
||||||
runCheckExecution();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,8 +62,12 @@ export class CronManager {
|
|||||||
* stops all cronjobs
|
* stops all cronjobs
|
||||||
*/
|
*/
|
||||||
public stop() {
|
public stop() {
|
||||||
this.status = 'stopped';
|
if (this.status === 'started') {
|
||||||
this.executionTimeout.cancel();
|
this.status = 'stopped';
|
||||||
|
this.executionTimeout.cancel();
|
||||||
|
} else {
|
||||||
|
console.log(`You tried to stop a CronManager that was not actually started.`);
|
||||||
|
}
|
||||||
for (const cron of this.cronjobs.getArray()) {
|
for (const cron of this.cronjobs.getArray()) {
|
||||||
cron.stop();
|
cron.stop();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user