@prsm/queue 2.1.0 → 2.1.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prsm/queue",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "Redis-backed distributed task queue with grouped concurrency, retries, and rate limiting",
5
5
  "type": "module",
6
6
  "exports": {
package/src/queue.js CHANGED
@@ -156,8 +156,13 @@ export default class Queue extends EventEmitter {
156
156
  async push(payload) {
157
157
  if (this._closed) throw new Error("Queue is closed")
158
158
  const task = { uuid: randomUUID(), payload, createdAt: Date.now(), attempts: 0 }
159
- await this._redis.lPush("queue:tasks", JSON.stringify(task))
160
159
  this._pushed++
160
+ try {
161
+ await this._redis.lPush("queue:tasks", JSON.stringify(task))
162
+ } catch (err) {
163
+ this._pushed--
164
+ throw err
165
+ }
161
166
  this.emit("new", { task })
162
167
  return task.uuid
163
168
  }
@@ -170,12 +175,15 @@ export default class Queue extends EventEmitter {
170
175
  pushAndWait(payload, timeout = 0) {
171
176
  if (this._closed) return Promise.reject(new Error("Queue is closed"))
172
177
  const task = { uuid: randomUUID(), payload, createdAt: Date.now(), attempts: 0 }
178
+ this._pushed++
173
179
  const result = this._awaitTask(task.uuid, timeout)
174
180
  result.catch(() => {})
175
181
  return this._redis.lPush("queue:tasks", JSON.stringify(task)).then(() => {
176
- this._pushed++
177
182
  this.emit("new", { task })
178
183
  return result
184
+ }, (err) => {
185
+ this._pushed--
186
+ throw err
179
187
  })
180
188
  }
181
189
 
@@ -191,24 +199,28 @@ export default class Queue extends EventEmitter {
191
199
 
192
200
  const commit = (task) => {
193
201
  return this._redis.lPush(`queue:groups:${key}`, JSON.stringify(task)).then(async () => {
194
- this._pushed++
195
202
  this.emit("new", { task })
196
203
  if (!this._groupWorkers.has(key)) {
197
204
  this._groupWorkers.set(key, new Map())
198
205
  this._groupInFlight.set(key, 0)
199
206
  await this._startGroupWorkers(key)
200
207
  }
208
+ }, (err) => {
209
+ this._pushed--
210
+ throw err
201
211
  })
202
212
  }
203
213
 
204
214
  return {
205
215
  push: async (payload) => {
206
216
  const task = makeTask(payload)
217
+ this._pushed++
207
218
  await commit(task)
208
219
  return task.uuid
209
220
  },
210
221
  pushAndWait: (payload, timeout = 0) => {
211
222
  const task = makeTask(payload)
223
+ this._pushed++
212
224
  const result = this._awaitTask(task.uuid, timeout)
213
225
  result.catch(() => {})
214
226
  return commit(task).then(() => result)
@@ -259,6 +271,7 @@ export default class Queue extends EventEmitter {
259
271
  await this._readyPromise.catch(() => {})
260
272
 
261
273
  if (this._cleanupTimer) clearInterval(this._cleanupTimer)
274
+ clearTimeout(this._drainTimer)
262
275
 
263
276
  this._workers.clear()
264
277
  for (const groupWorkers of this._groupWorkers.values()) groupWorkers.clear()
@@ -502,7 +515,10 @@ export default class Queue extends EventEmitter {
502
515
  }
503
516
 
504
517
  _emitDrain() {
505
- if (this._inFlight === 0 && this._totalSettled >= this._pushed) this.emit("drain")
518
+ clearTimeout(this._drainTimer)
519
+ this._drainTimer = setTimeout(() => {
520
+ if (this._inFlight === 0 && this._totalSettled >= this._pushed) this.emit("drain")
521
+ }, 0)
506
522
  }
507
523
 
508
524
  async _periodicCleanup() {
package/types/queue.d.ts CHANGED
@@ -4704,6 +4704,7 @@ export default class Queue extends EventEmitter<[never]> {
4704
4704
  _processTask(task: any, opts: any): Promise<void>;
4705
4705
  _settle(): void;
4706
4706
  _emitDrain(): void;
4707
+ _drainTimer: NodeJS.Timeout;
4707
4708
  _periodicCleanup(): Promise<void>;
4708
4709
  }
4709
4710
  export type QueueOptions = {