@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.
Files changed (36) hide show
  1. package/dist/cjs/src/background/BaseBackgroundedModel.js +49 -25
  2. package/dist/cjs/src/background/BaseBackgroundedService.js +24 -12
  3. package/dist/cjs/src/background/BaseScheduledService.js +1 -1
  4. package/dist/cjs/src/background/index.js +383 -98
  5. package/dist/cjs/src/error/background/DebouncedJobOutpacesRateLimit.js +64 -0
  6. package/dist/cjs/src/error/background/DebouncedJobRequiresMinimumDelay.js +52 -0
  7. package/dist/cjs/src/error/background/NamedWorkstreamRateLimitMissingMaxOrDuration.js +31 -0
  8. package/dist/cjs/src/error/background/RateLimitedPsychicJob.js +82 -0
  9. package/dist/cjs/src/error/background/RateLimitedPsychicJobThrownFromWorkerWithoutLimiter.js +51 -0
  10. package/dist/cjs/src/package-exports/errors.js +7 -0
  11. package/dist/cjs/src/test-utils/WorkerTestUtils.js +78 -13
  12. package/dist/esm/src/background/BaseBackgroundedModel.js +49 -25
  13. package/dist/esm/src/background/BaseBackgroundedService.js +24 -12
  14. package/dist/esm/src/background/BaseScheduledService.js +1 -1
  15. package/dist/esm/src/background/index.js +383 -98
  16. package/dist/esm/src/error/background/DebouncedJobOutpacesRateLimit.js +64 -0
  17. package/dist/esm/src/error/background/DebouncedJobRequiresMinimumDelay.js +52 -0
  18. package/dist/esm/src/error/background/NamedWorkstreamRateLimitMissingMaxOrDuration.js +31 -0
  19. package/dist/esm/src/error/background/RateLimitedPsychicJob.js +82 -0
  20. package/dist/esm/src/error/background/RateLimitedPsychicJobThrownFromWorkerWithoutLimiter.js +51 -0
  21. package/dist/esm/src/package-exports/errors.js +7 -0
  22. package/dist/esm/src/test-utils/WorkerTestUtils.js +78 -13
  23. package/dist/types/src/background/BaseBackgroundedModel.d.ts +49 -25
  24. package/dist/types/src/background/BaseBackgroundedService.d.ts +24 -12
  25. package/dist/types/src/background/BaseScheduledService.d.ts +2 -2
  26. package/dist/types/src/background/index.d.ts +109 -5
  27. package/dist/types/src/error/background/DebouncedJobOutpacesRateLimit.d.ts +31 -0
  28. package/dist/types/src/error/background/DebouncedJobRequiresMinimumDelay.d.ts +23 -0
  29. package/dist/types/src/error/background/NamedWorkstreamRateLimitMissingMaxOrDuration.d.ts +16 -0
  30. package/dist/types/src/error/background/RateLimitedPsychicJob.d.ts +73 -0
  31. package/dist/types/src/error/background/RateLimitedPsychicJobThrownFromWorkerWithoutLimiter.d.ts +16 -0
  32. package/dist/types/src/package-exports/errors.d.ts +1 -0
  33. package/dist/types/src/psychic-app-workers/index.d.ts +102 -53
  34. package/dist/types/src/test-utils/WorkerTestUtils.d.ts +32 -1
  35. package/dist/types/src/types/background.d.ts +88 -6
  36. 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.queueId - the id of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
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, including a delay in seconds, which
61
- * can be used to hold off the job for a certain amount of time after
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 background must be awaited, the resolution of the promise
68
- * is an indication that the job was put in the queue, not that it has
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 delaySeconds - the amount of time (in seconds) you want to hold off before allowing the job to run
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 the job was put in the queue, not that it has
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) the amount of time you want to hold off before allowing the job to run, and an optional `jobId` which debounces repeated calls within the delay window
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 static method you wish to run in the background
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, including a delay in seconds, which
162
- * can be used to hold off the job for a certain amount of time after
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 background must be awaited, the resolution of the promise
170
- * is an indication that the job was put in the queue, not that it has
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 delaySeconds - the amount of time (in seconds) you want to hold off before allowing the job to run
180
- * @param methodName - the name of the static method you wish to run in the background
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 the job was put in the queue, not that it has
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) the amount of time you want to hold off before allowing the job to run, and an optional `jobId` which debounces repeated calls within the delay window
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.queueId - the id of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
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, including a delay in seconds, which
72
- * can be used to hold off the job for a certain amount of time after
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 background must be awaited, the resolution of the promise
79
- * is an indication that the job was put in the queue, not that it has
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 delaySeconds - the amount of time (in seconds) you want to hold off before allowing the job to run
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 the job was put in the queue, not that it has
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) the amount of time you want to hold off before allowing the job to run, and an optional `jobId` which debounces repeated calls within the delay window
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.queueId - the id of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
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() {