forked from redwoodjs/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
229 lines (199 loc) · 6.2 KB
/
types.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Defines the basic shape of a logger that RedwoodJob will invoke to print
// debug messages. RedwoodJob will fallback to use `console` if no
// logger is passed in to RedwoodJob or any adapter. Luckily both Redwood's
import type { IntRange } from 'type-fest'
import type { BaseAdapter } from './adapters/BaseAdapter/BaseAdapter.js'
/** Redwood's logger and the standard console logger conform to this shape. */
export interface BasicLogger {
debug: (message?: any, ...optionalParams: any[]) => void
info: (message?: any, ...optionalParams: any[]) => void
warn: (message?: any, ...optionalParams: any[]) => void
error: (message?: any, ...optionalParams: any[]) => void
}
/**
*This is the minimum interface that a "job" must conform to in order to be
* scheduled and executed by Redwood's job engine.
*/
export interface BaseJob {
id: string | number
name: string
path: string
args: unknown[]
attempts: number
}
export type PossibleBaseJob = BaseJob | undefined
export type Adapters = Record<string, BaseAdapter>
export type QueueNames = readonly [string, ...string[]]
export interface WorkerConfig<
TAdapters extends Adapters,
TQueues extends QueueNames,
> extends WorkerSharedOptions {
/**
* The name of the adapter to use for this worker. This must be one of the keys
* in the `adapters` object when you created the `JobManager`.
*/
adapter: keyof TAdapters
/**
* The queue or queues that this worker should work on. You can pass a single
* queue name, an array of queue names, or the string `'*'` to work on all
* queues.
*/
queue: '*' | TQueues[number] | TQueues[number][]
/**
* The number of workers to spawn for this worker configuration.
*
* @default 1
*/
count?: number
/**
* The logger to use for this worker. If not provided, the logger from the
* `JobManager` will be used.
*/
logger?: BasicLogger
}
export interface WorkerSharedOptions {
/**
* The maximum number of retries to attempt for a job before giving up.
*
* @default 24
*/
maxAttempts?: number
/**
* The maximum amount of time in seconds that a job can run before another
* worker will attempt to retry it.
*
* @default 14,400 (4 hours)
*/
maxRuntime?: number
/**
* Whether a job that exceeds its `maxAttempts` should be deleted from the
* queue. If `false`, the job will remain in the queue but will not be
* processed further.
*
* @default false
*/
deleteFailedJobs?: boolean
/**
* Whether to keep succeeded jobs in the database after they have completed
* successfully.
*
* @default true
*
*/
deleteSuccessfulJobs?: boolean
/**
* The amount of time in seconds to wait between polling the queue for new
* jobs. Some adapters may not need this if they do not poll the queue and
* instead rely on a subscription model.
*
* @default 5
*/
sleepDelay?: number
}
export interface JobManagerConfig<
TAdapters extends Adapters,
TQueues extends QueueNames,
TLogger extends BasicLogger,
> {
/**
* An object containing all of the adapters that this job manager will use.
* The keys should be the names of the adapters and the values should be the
* adapter instances.
*/
adapters: TAdapters
/**
* The logger to use for this job manager. If not provided, the logger will
* default to the console.
*/
logger: TLogger
/**
* An array of all of queue names that jobs can be scheduled on to. Workers can
* be configured to work on a selection of these queues.
*
* This should be an array of string literals.
* If you're using TypeScript, you can use `as const`, like in
* `['default', 'critical', 'low'] as const` to construct an array of string
* literals
*/
queues: TQueues
/**
* An array of worker configurations that define how jobs should be processed.
*/
workers: WorkerConfig<TAdapters, TQueues>[]
}
export interface CreateSchedulerConfig<TAdapters extends Adapters> {
/**
* The name of the adapter to use for this scheduler. This must be one of the keys
* in the `adapters` object when you created the `JobManager`.
*/
adapter: keyof TAdapters
/**
* The logger to use for this scheduler. If not provided, the logger from the
* `JobManager` will be used.
*/
logger?: BasicLogger
}
export interface JobDefinition<
TQueues extends QueueNames,
TArgs extends unknown[] = [],
> {
/**
* The name of the queue that this job should always be scheduled on. This
* must be one of the values in the `queues` array when you created the
* `JobManager`.
*/
queue: TQueues[number]
/**
* The priority of the job in the range of 0-100. The lower the number, the
* higher the priority. The default is 50.
* @default 50
*/
priority?: PriorityValue
/**
* The function to run when this job is executed.
*
* @param args The arguments that were passed when the job was scheduled.
*/
perform: (...args: TArgs) => Promise<void> | void
}
export type JobComputedProperties = {
/**
* The name of the job that was defined in the job file.
*/
name: string
/**
* The path to the job file that this job was defined in.
*/
path: string
}
export type Job<
TQueues extends QueueNames,
TArgs extends unknown[] = [],
> = JobDefinition<TQueues, TArgs> & JobComputedProperties
export type ScheduleJobOptions =
| {
/**
* The number of seconds to wait before scheduling this job. This is mutually
* exclusive with `waitUntil`.
*/
wait: number
waitUntil?: never
}
| {
wait?: never
/**
* The date and time to schedule this job for. This is mutually exclusive with
* `wait`.
*/
waitUntil: Date
}
type PriorityValue = IntRange<1, 101>
// If the job has no arguments:
// - you may pass an empty array for the arguments and then optionally pass the scheduler options
// - you may optionally pass the scheduler options
// If the job has arguments:
// - you must pass the arguments and then optionally pass the scheduler options
export type CreateSchedulerArgs<TJob extends Job<QueueNames>> =
Parameters<TJob['perform']> extends []
? [ScheduleJobOptions?] | [[], ScheduleJobOptions?]
: [Parameters<TJob['perform']>, ScheduleJobOptions?]