-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathscheduler.registry.ts
116 lines (98 loc) · 3.36 KB
/
scheduler.registry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Injectable } from '@nestjs/common';
import { Job } from 'node-schedule';
import { DUPLICATE_SCHEDULER, NO_SCHEDULER_FOUND } from './schedule.messages';
import { Locker } from './interfaces/locker.interface';
import { TimeoutOptions } from './interfaces/timeout-options.interface';
import { CronObject, CronObjLiteral, CronOptions } from './interfaces/cron-options.interface';
interface TargetHost {
target: Function;
}
interface TimeoutHost {
timeout: number;
locker?: Locker;
options: TimeoutOptions;
}
interface RefHost<T> {
ref?: T;
}
interface CronHost {
rule: string | number | Date | CronObject | CronObjLiteral;
locker?: Locker;
options: CronOptions;
job?: Job;
}
export type IntervalJobOptions = TargetHost & TimeoutHost & RefHost<NodeJS.Timeout>;
export type TimeoutJobOptions = TargetHost & TimeoutHost & RefHost<NodeJS.Timeout>;
export type CronJobOptions = TargetHost & CronHost & RefHost<Job>;
@Injectable()
export class SchedulerRegistry {
private readonly cronJobs = new Map<string, CronJobOptions>();
private readonly timeouts = new Map<string, TimeoutJobOptions>();
private readonly intervals = new Map<string, IntervalJobOptions>();
getCronJob(name: string): CronJobOptions {
const job = this.cronJobs.get(name);
if (!job) {
throw new Error(NO_SCHEDULER_FOUND('Cron Job', name));
}
return job;
}
getIntervalJob(name: string): IntervalJobOptions {
const job = this.intervals.get(name);
if (typeof job === 'undefined') {
throw new Error(NO_SCHEDULER_FOUND('Interval', name));
}
return job;
}
getTimeoutJob(name: string): TimeoutJobOptions {
const job = this.timeouts.get(name);
if (typeof job === 'undefined') {
throw new Error(NO_SCHEDULER_FOUND('Timeout', name));
}
return job;
}
addCronJob(name: string, cronJob: CronJobOptions) {
const job = this.cronJobs.get(name);
if (job) {
throw new Error(DUPLICATE_SCHEDULER('Cron Job', name));
}
this.cronJobs.set(name, cronJob);
}
addIntervalJob(name: string, intervalJob: IntervalJobOptions) {
const job = this.intervals.get(name);
if (job) {
throw new Error(DUPLICATE_SCHEDULER('Interval', name));
}
this.intervals.set(name, intervalJob);
}
addTimeoutJob(name: string, timeoutJob: TimeoutJobOptions) {
const job = this.timeouts.get(name);
if (job) {
throw new Error(DUPLICATE_SCHEDULER('Timeout', name));
}
this.timeouts.set(name, timeoutJob);
}
getCronJobs(): Map<string, CronJobOptions> {
return this.cronJobs;
}
deleteCronJob(name: string) {
const cronJob = this.getCronJob(name);
cronJob.job.cancel();
this.cronJobs.delete(name);
}
getIntervalJobs(): Map<string, IntervalJobOptions> {
return this.intervals;
}
deleteIntervalJob(name: string) {
const job = this.getIntervalJob(name);
clearInterval(job.ref);
this.intervals.delete(name);
}
getTimeoutJobs(): Map<string, TimeoutJobOptions> {
return this.timeouts;
}
deleteTimeoutJob(name: string) {
const job = this.getTimeoutJob(name);
clearTimeout(job.ref);
this.timeouts.delete(name);
}
}