forked from boringnode/queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule.ts
More file actions
175 lines (145 loc) · 3.94 KB
/
schedule.ts
File metadata and controls
175 lines (145 loc) · 3.94 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { QueueManager } from './queue_manager.js'
import { JobDispatcher } from './job_dispatcher.js'
import type { ScheduleData, ScheduleListOptions, ScheduleStatus } from './types/main.js'
/**
* Represents a persisted job schedule.
*
* Use `Schedule.find()` or `Schedule.list()` to retrieve schedules,
* then use instance methods to manage them.
*
* @example
* ```typescript
* const schedule = await Schedule.find('cleanup-daily')
* if (schedule) {
* await schedule.pause()
* // Later...
* await schedule.resume()
* }
*
* // List all active schedules
* const activeSchedules = await Schedule.list({ status: 'active' })
* ```
*/
export class Schedule {
readonly #data: ScheduleData
constructor(data: ScheduleData) {
this.#data = data
}
get id(): string {
return this.#data.id
}
get name(): string {
return this.#data.name
}
get payload(): unknown {
return this.#data.payload
}
get cronExpression(): string | null {
return this.#data.cronExpression
}
get everyMs(): number | null {
return this.#data.everyMs
}
get timezone(): string {
return this.#data.timezone
}
get from(): Date | null {
return this.#data.from
}
get to(): Date | null {
return this.#data.to
}
get limit(): number | null {
return this.#data.limit
}
get runCount(): number {
return this.#data.runCount
}
get nextRunAt(): Date | null {
return this.#data.nextRunAt
}
get lastRunAt(): Date | null {
return this.#data.lastRunAt
}
get status(): ScheduleStatus {
return this.#data.status
}
get createdAt(): Date {
return this.#data.createdAt
}
/**
* Find a schedule by ID.
*
* @param id - The schedule ID
* @returns The schedule instance, or null if not found
*/
static async find(id: string): Promise<Schedule | null> {
const adapter = QueueManager.use()
const data = await adapter.getSchedule(id)
if (!data) return null
return new Schedule(data)
}
/**
* List all schedules matching the given options.
*
* @param options - Optional filters for listing
* @returns Array of schedule instances
*/
static async list(options?: ScheduleListOptions): Promise<Schedule[]> {
const adapter = QueueManager.use()
const schedules = await adapter.listSchedules(options)
return schedules.map((data) => new Schedule(data))
}
/**
* Pause this schedule.
* No jobs will be dispatched while paused.
*/
async pause(): Promise<void> {
const adapter = QueueManager.use()
await adapter.updateSchedule(this.#data.id, { status: 'paused' })
this.#data.status = 'paused'
}
/**
* Resume this schedule.
* Jobs will be dispatched according to the schedule.
*/
async resume(): Promise<void> {
const adapter = QueueManager.use()
await adapter.updateSchedule(this.#data.id, { status: 'active' })
this.#data.status = 'active'
}
/**
* Delete this schedule permanently.
*/
async delete(): Promise<void> {
const adapter = QueueManager.use()
await adapter.deleteSchedule(this.#data.id)
}
/**
* Trigger immediate execution of this schedule's job.
* Also updates runCount and lastRunAt.
*
* If the schedule has reached its limit, the job will not be dispatched.
*
* @param payload - Optional custom payload for the job
*/
async trigger(payload?: any): Promise<void> {
// Check if limit is reached
if (this.#data.limit !== null && this.#data.runCount >= this.#data.limit) {
return
}
const adapter = QueueManager.use()
// Dispatch the job
const dispatcher = new JobDispatcher(this.#data.name, payload ?? this.#data.payload)
await dispatcher.run()
// Update run metadata
const now = new Date()
const newRunCount = this.#data.runCount + 1
await adapter.updateSchedule(this.#data.id, {
runCount: newRunCount,
lastRunAt: now,
})
this.#data.runCount = newRunCount
this.#data.lastRunAt = now
}
}