groupbullmq 1.0.0 → 1.0.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/dist/index.cjs CHANGED
@@ -176,7 +176,7 @@ var BullBoardGroupMQAdapter = class extends import_base.BaseAdapter {
176
176
  }
177
177
  async empty() {
178
178
  this.assertWritable();
179
- throw new Error("Not implemented");
179
+ await this.queue.empty(this.options.emptyStatuses);
180
180
  }
181
181
  async promoteAll() {
182
182
  this.assertWritable();
@@ -1623,6 +1623,62 @@ var Queue = class {
1623
1623
  return false;
1624
1624
  }
1625
1625
  }
1626
+ async removeMany(jobIds) {
1627
+ if (jobIds.length === 0) return 0;
1628
+ const sha = await loadScript(this.r, "remove");
1629
+ const chunkSize = 500;
1630
+ let removed = 0;
1631
+ for (let i = 0; i < jobIds.length; i += chunkSize) {
1632
+ const chunk = jobIds.slice(i, i + chunkSize);
1633
+ const pipeline = this.r.multi();
1634
+ for (const jobId of chunk) pipeline.evalsha(sha, 1, this.ns, jobId);
1635
+ const results = await pipeline.exec();
1636
+ for (const result of results || []) if (result?.[1] === 1) removed += 1;
1637
+ }
1638
+ return removed;
1639
+ }
1640
+ /**
1641
+ * Remove pending jobs from the queue.
1642
+ * @param statuses Pending statuses to remove (default: ['waiting', 'delayed'])
1643
+ * @note Removing 'waiting' also clears staged jobs created by ordering delays.
1644
+ * @returns Number of jobs removed
1645
+ */
1646
+ async empty(statuses = ["waiting", "delayed"]) {
1647
+ const target = /* @__PURE__ */ new Set();
1648
+ for (const status of statuses) if (status === "waiting" || status === "delayed") target.add(status);
1649
+ if (target.size === 0) return 0;
1650
+ try {
1651
+ const delayedIds = target.has("delayed") || target.has("waiting") ? await this.getDelayedJobs() : [];
1652
+ let waitingIds = [];
1653
+ if (target.has("waiting")) {
1654
+ waitingIds = await this.getWaitingJobs();
1655
+ if (!target.has("delayed") && delayedIds.length > 0) {
1656
+ const delayedSet = new Set(delayedIds);
1657
+ waitingIds = waitingIds.filter((id) => !delayedSet.has(id));
1658
+ }
1659
+ }
1660
+ const stagedIds = target.has("waiting") ? await this.r.zrange(`${this.ns}:stage`, 0, -1) : [];
1661
+ const idsToRemove = /* @__PURE__ */ new Set();
1662
+ for (const id of delayedIds) idsToRemove.add(id);
1663
+ for (const id of waitingIds) idsToRemove.add(id);
1664
+ for (const id of stagedIds) idsToRemove.add(id);
1665
+ if (idsToRemove.size === 0) return 0;
1666
+ const removed = await this.removeMany(Array.from(idsToRemove));
1667
+ if (stagedIds.length > 0) {
1668
+ const stageKey = `${this.ns}:stage`;
1669
+ const chunkSize = 500;
1670
+ for (let i = 0; i < stagedIds.length; i += chunkSize) {
1671
+ const chunk = stagedIds.slice(i, i + chunkSize);
1672
+ await this.r.zrem(stageKey, ...chunk);
1673
+ }
1674
+ if (await this.r.zcard(stageKey) === 0) await this.r.del(`${this.ns}:stage:timer`);
1675
+ }
1676
+ return removed;
1677
+ } catch (error) {
1678
+ this.logger.error("Error emptying queue:", error);
1679
+ return 0;
1680
+ }
1681
+ }
1626
1682
  /**
1627
1683
  * Clean jobs of a given status older than graceTimeMs
1628
1684
  * @param graceTimeMs Remove jobs with finishedOn <= now - graceTimeMs (for completed/failed)