@rvoh/psychic-workers 2.6.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/src/background/BaseBackgroundedModel.js +49 -25
- package/dist/cjs/src/background/BaseBackgroundedService.js +24 -12
- package/dist/cjs/src/background/BaseScheduledService.js +1 -1
- package/dist/cjs/src/background/index.js +383 -98
- package/dist/cjs/src/error/background/DebouncedJobOutpacesRateLimit.js +64 -0
- package/dist/cjs/src/error/background/DebouncedJobRequiresMinimumDelay.js +52 -0
- package/dist/cjs/src/error/background/NamedWorkstreamRateLimitMissingMaxOrDuration.js +31 -0
- package/dist/cjs/src/error/background/RateLimitedPsychicJob.js +82 -0
- package/dist/cjs/src/error/background/RateLimitedPsychicJobThrownFromWorkerWithoutLimiter.js +51 -0
- package/dist/cjs/src/package-exports/errors.js +7 -0
- package/dist/cjs/src/test-utils/WorkerTestUtils.js +78 -13
- package/dist/esm/src/background/BaseBackgroundedModel.js +49 -25
- package/dist/esm/src/background/BaseBackgroundedService.js +24 -12
- package/dist/esm/src/background/BaseScheduledService.js +1 -1
- package/dist/esm/src/background/index.js +383 -98
- package/dist/esm/src/error/background/DebouncedJobOutpacesRateLimit.js +64 -0
- package/dist/esm/src/error/background/DebouncedJobRequiresMinimumDelay.js +52 -0
- package/dist/esm/src/error/background/NamedWorkstreamRateLimitMissingMaxOrDuration.js +31 -0
- package/dist/esm/src/error/background/RateLimitedPsychicJob.js +82 -0
- package/dist/esm/src/error/background/RateLimitedPsychicJobThrownFromWorkerWithoutLimiter.js +51 -0
- package/dist/esm/src/package-exports/errors.js +7 -0
- package/dist/esm/src/test-utils/WorkerTestUtils.js +78 -13
- package/dist/types/src/background/BaseBackgroundedModel.d.ts +49 -25
- package/dist/types/src/background/BaseBackgroundedService.d.ts +24 -12
- package/dist/types/src/background/BaseScheduledService.d.ts +2 -2
- package/dist/types/src/background/index.d.ts +109 -5
- package/dist/types/src/error/background/DebouncedJobOutpacesRateLimit.d.ts +31 -0
- package/dist/types/src/error/background/DebouncedJobRequiresMinimumDelay.d.ts +23 -0
- package/dist/types/src/error/background/NamedWorkstreamRateLimitMissingMaxOrDuration.d.ts +16 -0
- package/dist/types/src/error/background/RateLimitedPsychicJob.d.ts +73 -0
- package/dist/types/src/error/background/RateLimitedPsychicJobThrownFromWorkerWithoutLimiter.d.ts +16 -0
- package/dist/types/src/package-exports/errors.d.ts +1 -0
- package/dist/types/src/psychic-app-workers/index.d.ts +102 -53
- package/dist/types/src/test-utils/WorkerTestUtils.d.ts +32 -1
- package/dist/types/src/types/background.d.ts +88 -6
- package/package.json +28 -25
|
@@ -12,7 +12,7 @@ export default class BaseBackgroundedModel extends Dream {
|
|
|
12
12
|
* @returns {object} config - the background job config
|
|
13
13
|
* @returns {string} config.priority - 'default' | 'urgent' | 'not_urgent' | 'last'
|
|
14
14
|
* @returns {string} config.workstream - a workstream name. This would be the name of a workstream, as defined in conf/workers.ts
|
|
15
|
-
* @returns {string} config.
|
|
15
|
+
* @returns {string} config.queue - the name of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
|
|
16
16
|
* @returns {string} config.groupId - the groupId of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
|
|
17
17
|
*/
|
|
18
18
|
static get backgroundJobConfig() {
|
|
@@ -57,16 +57,22 @@ export default class BaseBackgroundedModel extends Dream {
|
|
|
57
57
|
}
|
|
58
58
|
/**
|
|
59
59
|
* runs the specified method in a background queue, driven by BullMQ,
|
|
60
|
-
* sending in the provided args,
|
|
61
|
-
*
|
|
62
|
-
* it is entered into the queue.
|
|
60
|
+
* sending in the provided args, along with a delay object — one or more of
|
|
61
|
+
* `{ seconds, minutes, hours, days }` — which holds the job off for that much
|
|
62
|
+
* time after it is entered into the queue.
|
|
63
|
+
*
|
|
64
|
+
* The delay object also accepts an optional `jobId`, which turns the delay
|
|
65
|
+
* into a **debounce**: repeated calls carrying the same `jobId` collapse into
|
|
66
|
+
* a single execution, which runs once the delay has elapsed without another
|
|
67
|
+
* call arriving. `jobId` is a deduplication key rather than a BullMQ job id.
|
|
68
|
+
*
|
|
69
|
+
* See {@link DelayedJobOpts.jobId}.
|
|
63
70
|
*
|
|
64
71
|
* ```ts
|
|
65
|
-
* await User.backgroundWithDelay('myMethod', 'abc', 123)
|
|
72
|
+
* await User.backgroundWithDelay({ minutes: 5 }, 'myMethod', 'abc', 123)
|
|
66
73
|
* ```
|
|
67
|
-
* though calling
|
|
68
|
-
* is an indication that
|
|
69
|
-
* completed.
|
|
74
|
+
* though calling backgroundWithDelay must be awaited, the resolution of the promise
|
|
75
|
+
* is an indication that a run is pending, not that it has completed.
|
|
70
76
|
*
|
|
71
77
|
* NOTE: in test environments, psychic will immediately invoke the underlying
|
|
72
78
|
* method, preventing you from needing to explicitly wait for queues to flush
|
|
@@ -74,7 +80,7 @@ export default class BaseBackgroundedModel extends Dream {
|
|
|
74
80
|
*
|
|
75
81
|
* @deprecated use `backgroundWith({ delay }, methodName, ...args)` instead. This method will be removed in a future major version.
|
|
76
82
|
*
|
|
77
|
-
* @param
|
|
83
|
+
* @param delay - how long you want to hold off before allowing the job to run, given as at least one of `seconds`, `minutes`, `hours` or `days`, plus an optional `jobId` (a deduplication key) which debounces repeated calls into a single run
|
|
78
84
|
* @param methodName - the name of the static method you wish to run in the background
|
|
79
85
|
* @param args - a variadic list of arguments to be sent to your method
|
|
80
86
|
*/
|
|
@@ -94,19 +100,25 @@ export default class BaseBackgroundedModel extends Dream {
|
|
|
94
100
|
* be used to delay the job and/or override the priority provided by
|
|
95
101
|
* the `backgroundJobConfig` getter on this model.
|
|
96
102
|
*
|
|
103
|
+
* Adding a `jobId` to the delay turns it into a **debounce**: repeated calls
|
|
104
|
+
* carrying the same `jobId` collapse into a single execution, which runs once
|
|
105
|
+
* the delay has elapsed without another call arriving. `jobId` is a
|
|
106
|
+
* deduplication key rather than a BullMQ job id.
|
|
107
|
+
*
|
|
108
|
+
* See {@link DelayedJobOpts.jobId}.
|
|
109
|
+
*
|
|
97
110
|
* ```ts
|
|
98
111
|
* await User.backgroundWith({ delay: { seconds: 30, jobId: 'my-unique-job-id' }, priority: 'urgent' }, 'myMethod', 'abc', 123)
|
|
99
112
|
* ```
|
|
100
113
|
* though calling backgroundWith must be awaited, the resolution of the promise
|
|
101
|
-
* is an indication that
|
|
102
|
-
* completed.
|
|
114
|
+
* is an indication that a run is pending, not that it has completed.
|
|
103
115
|
*
|
|
104
116
|
* NOTE: in test environments, psychic will immediately invoke the underlying
|
|
105
117
|
* method, preventing you from needing to explicitly wait for queues to flush
|
|
106
118
|
* before making assertions.
|
|
107
119
|
*
|
|
108
120
|
* @param opts - options for backgrounding this job
|
|
109
|
-
* @param opts.delay - (optional)
|
|
121
|
+
* @param opts.delay - (optional) how long you want to hold off before allowing the job to run, given as at least one of `seconds`, `minutes`, `hours` or `days`, plus an optional `jobId` (a deduplication key) which debounces repeated calls into a single run
|
|
110
122
|
* @param opts.priority - (optional) a priority which, when provided, overrides the priority provided by `backgroundJobConfig`
|
|
111
123
|
* @param methodName - the name of the static method you wish to run in the background
|
|
112
124
|
* @param args - a variadic list of arguments to be sent to your method
|
|
@@ -146,7 +158,7 @@ export default class BaseBackgroundedModel extends Dream {
|
|
|
146
158
|
* method, preventing you from needing to explicitly wait for queues to flush
|
|
147
159
|
* before making assertions.
|
|
148
160
|
*
|
|
149
|
-
* @param methodName - the name of the
|
|
161
|
+
* @param methodName - the name of the instance method you wish to run in the background
|
|
150
162
|
* @param args - a variadic list of arguments to be sent to your method
|
|
151
163
|
*/
|
|
152
164
|
async background(methodName, ...args) {
|
|
@@ -158,17 +170,23 @@ export default class BaseBackgroundedModel extends Dream {
|
|
|
158
170
|
}
|
|
159
171
|
/**
|
|
160
172
|
* runs the specified method in a background queue, driven by BullMQ,
|
|
161
|
-
* sending in the provided args,
|
|
162
|
-
*
|
|
163
|
-
* it is entered into the queue.
|
|
173
|
+
* sending in the provided args, along with a delay object — one or more of
|
|
174
|
+
* `{ seconds, minutes, hours, days }` — which holds the job off for that much
|
|
175
|
+
* time after it is entered into the queue.
|
|
176
|
+
*
|
|
177
|
+
* The delay object also accepts an optional `jobId`, which turns the delay
|
|
178
|
+
* into a **debounce**: repeated calls carrying the same `jobId` collapse into
|
|
179
|
+
* a single execution, which runs once the delay has elapsed without another
|
|
180
|
+
* call arriving. `jobId` is a deduplication key rather than a BullMQ job id.
|
|
181
|
+
*
|
|
182
|
+
* See {@link DelayedJobOpts.jobId}.
|
|
164
183
|
*
|
|
165
184
|
* ```ts
|
|
166
185
|
* const user = await User.lastOrFail()
|
|
167
|
-
* await user.backgroundWithDelay('myMethod', 'abc', 123)
|
|
186
|
+
* await user.backgroundWithDelay({ minutes: 5 }, 'myMethod', 'abc', 123)
|
|
168
187
|
* ```
|
|
169
|
-
* though calling
|
|
170
|
-
* is an indication that
|
|
171
|
-
* completed.
|
|
188
|
+
* though calling backgroundWithDelay must be awaited, the resolution of the promise
|
|
189
|
+
* is an indication that a run is pending, not that it has completed.
|
|
172
190
|
*
|
|
173
191
|
* NOTE: in test environments, psychic will immediately invoke the underlying
|
|
174
192
|
* method, preventing you from needing to explicitly wait for queues to flush
|
|
@@ -176,8 +194,8 @@ export default class BaseBackgroundedModel extends Dream {
|
|
|
176
194
|
*
|
|
177
195
|
* @deprecated use `backgroundWith({ delay }, methodName, ...args)` instead. This method will be removed in a future major version.
|
|
178
196
|
*
|
|
179
|
-
* @param
|
|
180
|
-
* @param methodName - the name of the
|
|
197
|
+
* @param delay - how long you want to hold off before allowing the job to run, given as at least one of `seconds`, `minutes`, `hours` or `days`, plus an optional `jobId` (a deduplication key) which debounces repeated calls into a single run
|
|
198
|
+
* @param methodName - the name of the instance method you wish to run in the background
|
|
181
199
|
* @param args - a variadic list of arguments to be sent to your method
|
|
182
200
|
*/
|
|
183
201
|
async backgroundWithDelay(delay, methodName, ...args) {
|
|
@@ -195,20 +213,26 @@ export default class BaseBackgroundedModel extends Dream {
|
|
|
195
213
|
* be used to delay the job and/or override the priority provided by
|
|
196
214
|
* the `backgroundJobConfig` getter on this model.
|
|
197
215
|
*
|
|
216
|
+
* Adding a `jobId` to the delay turns it into a **debounce**: repeated calls
|
|
217
|
+
* carrying the same `jobId` collapse into a single execution, which runs once
|
|
218
|
+
* the delay has elapsed without another call arriving. `jobId` is a
|
|
219
|
+
* deduplication key rather than a BullMQ job id.
|
|
220
|
+
*
|
|
221
|
+
* See {@link DelayedJobOpts.jobId}.
|
|
222
|
+
*
|
|
198
223
|
* ```ts
|
|
199
224
|
* const user = await User.lastOrFail()
|
|
200
225
|
* await user.backgroundWith({ delay: { seconds: 30, jobId: 'my-unique-job-id' }, priority: 'urgent' }, 'myMethod', 'abc', 123)
|
|
201
226
|
* ```
|
|
202
227
|
* though calling backgroundWith must be awaited, the resolution of the promise
|
|
203
|
-
* is an indication that
|
|
204
|
-
* completed.
|
|
228
|
+
* is an indication that a run is pending, not that it has completed.
|
|
205
229
|
*
|
|
206
230
|
* NOTE: in test environments, psychic will immediately invoke the underlying
|
|
207
231
|
* method, preventing you from needing to explicitly wait for queues to flush
|
|
208
232
|
* before making assertions.
|
|
209
233
|
*
|
|
210
234
|
* @param opts - options for backgrounding this job
|
|
211
|
-
* @param opts.delay - (optional)
|
|
235
|
+
* @param opts.delay - (optional) how long you want to hold off before allowing the job to run, given as at least one of `seconds`, `minutes`, `hours` or `days`, plus an optional `jobId` (a deduplication key) which debounces repeated calls into a single run
|
|
212
236
|
* @param opts.priority - (optional) a priority which, when provided, overrides the priority provided by `backgroundJobConfig`
|
|
213
237
|
* @param methodName - the name of the instance method you wish to run in the background
|
|
214
238
|
* @param args - a variadic list of arguments to be sent to your method
|
|
@@ -11,7 +11,7 @@ export default class BaseBackgroundedService {
|
|
|
11
11
|
* @returns {object} config - the background job config
|
|
12
12
|
* @returns {string} config.priority - 'default' | 'urgent' | 'not_urgent' | 'last'
|
|
13
13
|
* @returns {string} config.workstream - a workstream name. This would be the name of a workstream, as defined in conf/workers.ts
|
|
14
|
-
* @returns {string} config.
|
|
14
|
+
* @returns {string} config.queue - the name of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
|
|
15
15
|
* @returns {string} config.groupId - the groupId of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
|
|
16
16
|
*/
|
|
17
17
|
static get backgroundJobConfig() {
|
|
@@ -68,16 +68,22 @@ export default class BaseBackgroundedService {
|
|
|
68
68
|
}
|
|
69
69
|
/**
|
|
70
70
|
* runs the specified method in a background queue, driven by BullMQ,
|
|
71
|
-
* sending in the provided args,
|
|
72
|
-
*
|
|
73
|
-
* it is entered into the queue.
|
|
71
|
+
* sending in the provided args, along with a delay object — one or more of
|
|
72
|
+
* `{ seconds, minutes, hours, days }` — which holds the job off for that much
|
|
73
|
+
* time after it is entered into the queue.
|
|
74
|
+
*
|
|
75
|
+
* The delay object also accepts an optional `jobId`, which turns the delay
|
|
76
|
+
* into a **debounce**: repeated calls carrying the same `jobId` collapse into
|
|
77
|
+
* a single execution, which runs once the delay has elapsed without another
|
|
78
|
+
* call arriving. `jobId` is a deduplication key rather than a BullMQ job id.
|
|
79
|
+
*
|
|
80
|
+
* See {@link DelayedJobOpts.jobId}.
|
|
74
81
|
*
|
|
75
82
|
* ```ts
|
|
76
|
-
* await MyBackgroundableClass.backgroundWithDelay('myMethod', 'abc', 123)
|
|
83
|
+
* await MyBackgroundableClass.backgroundWithDelay({ minutes: 5 }, 'myMethod', 'abc', 123)
|
|
77
84
|
* ```
|
|
78
|
-
* though calling
|
|
79
|
-
* is an indication that
|
|
80
|
-
* completed.
|
|
85
|
+
* though calling backgroundWithDelay must be awaited, the resolution of the promise
|
|
86
|
+
* is an indication that a run is pending, not that it has completed.
|
|
81
87
|
*
|
|
82
88
|
* NOTE: in test environments, psychic will immediately invoke the underlying
|
|
83
89
|
* method, preventing you from needing to explicitly wait for queues to flush
|
|
@@ -85,7 +91,7 @@ export default class BaseBackgroundedService {
|
|
|
85
91
|
*
|
|
86
92
|
* @deprecated use `backgroundWith({ delay }, methodName, ...args)` instead. This method will be removed in a future major version.
|
|
87
93
|
*
|
|
88
|
-
* @param
|
|
94
|
+
* @param delay - how long you want to hold off before allowing the job to run, given as at least one of `seconds`, `minutes`, `hours` or `days`, plus an optional `jobId` (a deduplication key) which debounces repeated calls into a single run
|
|
89
95
|
* @param methodName - the name of the static method you wish to run in the background
|
|
90
96
|
* @param args - a variadic list of arguments to be sent to your method
|
|
91
97
|
*/
|
|
@@ -105,6 +111,13 @@ export default class BaseBackgroundedService {
|
|
|
105
111
|
* be used to delay the job and/or override the priority provided by
|
|
106
112
|
* the `backgroundJobConfig` getter on this service.
|
|
107
113
|
*
|
|
114
|
+
* Adding a `jobId` to the delay turns it into a **debounce**: repeated calls
|
|
115
|
+
* carrying the same `jobId` collapse into a single execution, which runs once
|
|
116
|
+
* the delay has elapsed without another call arriving. `jobId` is a
|
|
117
|
+
* deduplication key rather than a BullMQ job id.
|
|
118
|
+
*
|
|
119
|
+
* See {@link DelayedJobOpts.jobId}.
|
|
120
|
+
*
|
|
108
121
|
* ```ts
|
|
109
122
|
* await MyBackgroundableClass.backgroundWith(
|
|
110
123
|
* { delay: { seconds: 30, jobId: 'my-unique-job-id' }, priority: 'urgent' },
|
|
@@ -114,15 +127,14 @@ export default class BaseBackgroundedService {
|
|
|
114
127
|
* )
|
|
115
128
|
* ```
|
|
116
129
|
* though calling backgroundWith must be awaited, the resolution of the promise
|
|
117
|
-
* is an indication that
|
|
118
|
-
* completed.
|
|
130
|
+
* is an indication that a run is pending, not that it has completed.
|
|
119
131
|
*
|
|
120
132
|
* NOTE: in test environments, psychic will immediately invoke the underlying
|
|
121
133
|
* method, preventing you from needing to explicitly wait for queues to flush
|
|
122
134
|
* before making assertions.
|
|
123
135
|
*
|
|
124
136
|
* @param opts - options for backgrounding this job
|
|
125
|
-
* @param opts.delay - (optional)
|
|
137
|
+
* @param opts.delay - (optional) how long you want to hold off before allowing the job to run, given as at least one of `seconds`, `minutes`, `hours` or `days`, plus an optional `jobId` (a deduplication key) which debounces repeated calls into a single run
|
|
126
138
|
* @param opts.priority - (optional) a priority which, when provided, overrides the priority provided by `backgroundJobConfig`
|
|
127
139
|
* @param methodName - the name of the static method you wish to run in the background
|
|
128
140
|
* @param args - a variadic list of arguments to be sent to your method
|
|
@@ -10,7 +10,7 @@ export default class BaseScheduledService {
|
|
|
10
10
|
* @returns {object} config - the background job config
|
|
11
11
|
* @returns {string} config.priority - 'default' | 'urgent' | 'not_urgent' | 'last'
|
|
12
12
|
* @returns {string} config.workstream - a workstream name. This would be the name of a workstream, as defined in conf/workers.ts
|
|
13
|
-
* @returns {string} config.
|
|
13
|
+
* @returns {string} config.queue - the name of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
|
|
14
14
|
* @returns {string} config.groupId - the groupId of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
|
|
15
15
|
*/
|
|
16
16
|
static get backgroundJobConfig() {
|