-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtoadScheduler.ts
More file actions
131 lines (111 loc) · 3.04 KB
/
toadScheduler.ts
File metadata and controls
131 lines (111 loc) · 3.04 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { SimpleIntervalEngine } from './engines/simple-interval/SimpleIntervalEngine'
import { SimpleIntervalJob } from './engines/simple-interval/SimpleIntervalJob'
import { Job, JobStatus } from './common/Job'
import { LongIntervalJob } from './engines/simple-interval/LongIntervalJob'
import { CronJob } from './engines/cron/CronJob'
import { CronJobEngine } from './engines/cron/CronJobEngine'
type EngineRegistry = {
simpleIntervalEngine?: SimpleIntervalEngine
cronJobEngine?: CronJobEngine
}
export class ToadScheduler {
private readonly engines: EngineRegistry
private readonly jobRegistry: Record<string, Job>
constructor() {
this.engines = {}
this.jobRegistry = {}
}
/**
* @deprecated in favor of unified addJob method
*/
addIntervalJob(job: SimpleIntervalJob | LongIntervalJob) {
if (!this.engines.simpleIntervalEngine) {
this.engines.simpleIntervalEngine = new SimpleIntervalEngine()
}
this.registerJob(job)
this.engines.simpleIntervalEngine.add(job)
}
/**
* @deprecated in favor of unified addJob method
*/
addLongIntervalJob(job: LongIntervalJob): void {
return this.addIntervalJob(job)
}
/**
* @deprecated in favor of unified addJob method
*/
addSimpleIntervalJob(job: SimpleIntervalJob): void {
return this.addIntervalJob(job)
}
private registerJob(job: Job): void {
if (job.id) {
if (this.jobRegistry[job.id]) {
throw new Error(`Job with an id ${job.id} is already registered.`)
}
this.jobRegistry[job.id] = job
}
}
/**
* @deprecated in favor of unified addJob method
*/
addCronJob(job: CronJob): void {
if (!this.engines.cronJobEngine) {
this.engines.cronJobEngine = new CronJobEngine()
}
this.registerJob(job)
this.engines.cronJobEngine.add(job)
}
addJob(job: Job): void {
if (job instanceof SimpleIntervalJob || job instanceof LongIntervalJob) {
this.addIntervalJob(job)
} else if (job instanceof CronJob) {
this.addCronJob(job)
} else {
throw new Error('Unsupported job type.')
}
}
stop(): void {
for (const engine of Object.values(this.engines)) {
engine?.stop()
}
}
getById(id: string): Job {
const job = this.jobRegistry[id]
if (!job) {
throw new Error(`Job with an id ${id} is not registered.`)
}
return job
}
existsById(id: string): boolean {
const job = this.jobRegistry[id]
if (!job) {
return false
}
return true
}
removeById(id: string): Job | undefined {
const job = this.jobRegistry[id]
if (!job) {
return
}
job.stop()
delete this.jobRegistry[id]
return job
}
stopById(id: string): void {
const job = this.getById(id)
job.stop()
}
startById(id: string): void {
const job = this.getById(id)
job.start()
}
getAllJobs(): Job[] {
return Object.values(this.jobRegistry)
}
getAllJobsByStatus(status: JobStatus): Job[] {
return Object.values(this.jobRegistry).filter((value) => {
return value.getStatus() === status
})
}
}