Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
4d0fb3eb30 | |||
3f0b230d23 | |||
0c0b08e2be | |||
dd722146f4 | |||
c968e156ae | |||
b5509711e7 |
@ -4,6 +4,7 @@
|
|||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
},
|
},
|
||||||
"gitzone": {
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "gitlab.com",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "pushrocks",
|
||||||
|
3137
package-lock.json
generated
3137
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
16
package.json
16
package.json
@ -1,30 +1,32 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smarttime",
|
"name": "@pushrocks/smarttime",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "3.0.11",
|
"version": "3.0.14",
|
||||||
"description": "handle time in smart ways",
|
"description": "handle time in smart ways",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/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)"
|
"build": "(tsbuild && tsbundle npm)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.11",
|
"@gitzone/tsbuild": "^2.1.11",
|
||||||
|
"@gitzone/tsbundle": "^1.0.69",
|
||||||
"@gitzone/tsrun": "^1.2.6",
|
"@gitzone/tsrun": "^1.2.6",
|
||||||
"@gitzone/tstest": "^1.0.24",
|
"@gitzone/tstest": "^1.0.24",
|
||||||
"@pushrocks/tapbundle": "^3.0.9",
|
"@pushrocks/tapbundle": "^3.0.9",
|
||||||
"@types/node": "^12.0.8",
|
"@types/node": "^14.0.5",
|
||||||
"tslint": "^5.17.0",
|
"tslint": "^6.1.2",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@pushrocks/smartdelay": "^2.0.6",
|
||||||
"@pushrocks/smartpromise": "^3.0.2",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"@types/cron": "^1.7.1",
|
"@types/cron": "^1.7.1",
|
||||||
"@types/luxon": "^1.15.1",
|
"@types/luxon": "^1.15.1",
|
||||||
"cron": "^1.7.1",
|
"cron-parser": "^2.14.0",
|
||||||
"luxon": "^1.16.0"
|
"luxon": "^1.16.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
|
45
ts/smarttime.classes.cronjob.ts
Normal file
45
ts/smarttime.classes.cronjob.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import * as plugins from './smarttime.plugins';
|
||||||
|
import { CronManager } from './smarttime.classes.cronmanager';
|
||||||
|
|
||||||
|
export type TJobFunction = (() => void) | (() => Promise<any>);
|
||||||
|
|
||||||
|
export class CronJob {
|
||||||
|
public status: 'started' | 'stopped' | 'initial' = 'initial';
|
||||||
|
public cronExpression: string;
|
||||||
|
public jobFunction: TJobFunction;
|
||||||
|
|
||||||
|
private cronInterval= plugins.cronParser.parseExpression('* * * * * *');
|
||||||
|
private nextExecutionUnix: number = 0;
|
||||||
|
|
||||||
|
constructor(cronManager: CronManager, cronExpressionArg: string, jobFunction: TJobFunction ) {
|
||||||
|
this.cronExpression = cronExpressionArg;
|
||||||
|
this.jobFunction = jobFunction;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* checks wether the cronjob needs to be executed
|
||||||
|
*/
|
||||||
|
public checkExecution() {
|
||||||
|
if (this.nextExecutionUnix === 0) {
|
||||||
|
this.nextExecutionUnix = this.cronInterval.next().toDate().getTime();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (Date.now() > this.nextExecutionUnix) {
|
||||||
|
this.jobFunction();
|
||||||
|
this.nextExecutionUnix = this.cronInterval.next().toDate().getTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public start() {
|
||||||
|
this.cronInterval = this.getCronInterval();
|
||||||
|
this.status = 'started';
|
||||||
|
}
|
||||||
|
|
||||||
|
public stop() {
|
||||||
|
this.status = 'stopped';
|
||||||
|
}
|
||||||
|
|
||||||
|
private getCronInterval () {
|
||||||
|
return plugins.cronParser.parseExpression(this.cronExpression);
|
||||||
|
}
|
||||||
|
}
|
@ -1,16 +1,28 @@
|
|||||||
import * as plugins from './smarttime.plugins';
|
import * as plugins from './smarttime.plugins';
|
||||||
|
import { CronJob } from './smarttime.classes.cronjob';
|
||||||
|
import { Timer } from './smarttime.classes.timer';
|
||||||
|
import { Interval } from './smarttime.classes.interval';
|
||||||
|
|
||||||
export class CronManager {
|
export class CronManager {
|
||||||
|
public cronInterval = new Interval(1000);
|
||||||
|
|
||||||
public status: 'started' | 'stopped' = 'stopped';
|
public status: 'started' | 'stopped' = 'stopped';
|
||||||
|
public cronjobs: CronJob[] = [];
|
||||||
|
|
||||||
public cronjobs: plugins.cron.CronJob[] = [];
|
constructor() {
|
||||||
|
this.cronInterval.addIntervalJob(() => {
|
||||||
|
for (const cronJob of this.cronjobs) {
|
||||||
|
cronJob.checkExecution();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public addCronjob(cronIdentifierArg: string, cronFunctionArg: plugins.cron.CronCommand) {
|
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => any) {
|
||||||
const newCronJob = new plugins.cron.CronJob(cronIdentifierArg, cronFunctionArg);
|
const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg);
|
||||||
|
this.cronjobs.push(newCronJob);
|
||||||
if (this.status === 'started') {
|
if (this.status === 'started') {
|
||||||
newCronJob.start();
|
newCronJob.start();
|
||||||
}
|
}
|
||||||
this.cronjobs.push(newCronJob);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,6 +33,7 @@ export class CronManager {
|
|||||||
for (const cron of this.cronjobs) {
|
for (const cron of this.cronjobs) {
|
||||||
cron.start();
|
cron.start();
|
||||||
}
|
}
|
||||||
|
this.cronInterval.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,5 +44,6 @@ export class CronManager {
|
|||||||
for (const cron of this.cronjobs) {
|
for (const cron of this.cronjobs) {
|
||||||
cron.stop();
|
cron.stop();
|
||||||
}
|
}
|
||||||
|
this.cronInterval.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,10 @@ export class ExtendedDate extends Date {
|
|||||||
return new ExtendedDate(milliSeconds);
|
return new ExtendedDate(milliSeconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static fromDate(dateArg: Date) {
|
||||||
|
return new ExtendedDate(dateArg.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
public static fromEuropeanDate(europeanDate: string) {
|
public static fromEuropeanDate(europeanDate: string) {
|
||||||
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDate);
|
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDate);
|
||||||
const luxonDate = plugins.luxon.DateTime.utc(
|
const luxonDate = plugins.luxon.DateTime.utc(
|
||||||
@ -68,6 +72,9 @@ export class ExtendedDate extends Date {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// INSTANCE
|
// INSTANCE
|
||||||
|
public timezone: TAvailableZone;
|
||||||
|
|
||||||
|
|
||||||
constructor(unixMilli: number) {
|
constructor(unixMilli: number) {
|
||||||
super(unixMilli);
|
super(unixMilli);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
import * as process from 'process';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* easy high resolution time measurement
|
* easy high resolution time measurement
|
||||||
*/
|
*/
|
||||||
|
40
ts/smarttime.classes.interval.ts
Normal file
40
ts/smarttime.classes.interval.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import * as plugins from './smarttime.plugins';
|
||||||
|
|
||||||
|
export class Interval {
|
||||||
|
public status: 'started' | 'stopped' | 'initial' = 'initial';
|
||||||
|
private statusAuthorization: any = null;
|
||||||
|
|
||||||
|
public intervalMilliseconds: number;
|
||||||
|
public intervalJobs: Array<() => any> = [];
|
||||||
|
constructor(intervalMillisencondsArg: number) {
|
||||||
|
this.intervalMilliseconds = intervalMillisencondsArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public start() {
|
||||||
|
this.status = 'started';
|
||||||
|
const statusAuth = new Date();
|
||||||
|
this.statusAuthorization = statusAuth;
|
||||||
|
const runInterval = async () => {
|
||||||
|
while (this.status === 'started' && this.statusAuthorization === statusAuth) {
|
||||||
|
await plugins.smartdelay.delayFor(this.intervalMilliseconds);
|
||||||
|
this.executeIntervalJobs();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
runInterval();
|
||||||
|
}
|
||||||
|
|
||||||
|
public stop () {
|
||||||
|
this.status = 'stopped';
|
||||||
|
this.statusAuthorization = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public addIntervalJob(funcArg: () => any) {
|
||||||
|
this.intervalJobs.push(funcArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private executeIntervalJobs () {
|
||||||
|
for (const funcArg of this.intervalJobs) {
|
||||||
|
funcArg();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,11 @@
|
|||||||
// @pushrocks scope
|
// @pushrocks scope
|
||||||
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
export { smartpromise };
|
export { smartdelay, smartpromise };
|
||||||
|
|
||||||
// third parties
|
// third parties
|
||||||
|
import cronParser from 'cron-parser';
|
||||||
import * as luxon from 'luxon';
|
import * as luxon from 'luxon';
|
||||||
import * as cron from 'cron';
|
|
||||||
|
|
||||||
export { luxon, cron };
|
export { cronParser, luxon };
|
||||||
|
Reference in New Issue
Block a user