Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(bulk): signal a cancel only to a batch this process runs

Yudhi Armyndharis36ac0a7b96c3
diff --git a/src/modules/message/bulk-message.service.spec.ts b/src/modules/message/bulk-message.service.spec.ts
index 40c1a4a7c..00ccd2812 100644
--- a/src/modules/message/bulk-message.service.spec.ts
+++ b/src/modules/message/bulk-message.service.spec.ts
@@ -1548,6 +1548,26 @@ describe('BulkMessageService.cancelBatch', () => {
);
});
+ // Only the process running a batch removes its marker, and a marker makes the takeover reap skip
+ // the batch, so a cancel must not leave one behind for a batch this process is not running.
+ it('leaves no in-flight marker for a batch another process is running', async () => {
+ repo.findOne.mockResolvedValue(batchWithStatus(BatchStatus.PROCESSING));
+
+ await service.cancelBatch('s1', 'bx');
+
+ expect((service as unknown as { processingBatches: Map<string, boolean> }).processingBatches.has('b1')).toBe(false);
+ });
+
+ it('signals the run of a batch this process is running', async () => {
+ const markers = (service as unknown as { processingBatches: Map<string, boolean> }).processingBatches;
+ markers.set('b1', true);
+ repo.findOne.mockResolvedValue(batchWithStatus(BatchStatus.PROCESSING));
+
+ await service.cancelBatch('s1', 'bx');
+
+ expect(markers.get('b1')).toBe(false);
+ });
+
it('rejects when the batch turns terminal between the read and the guarded write (no relabelling)', async () => {
repo.findOne
.mockResolvedValueOnce(batchWithStatus(BatchStatus.PROCESSING)) // upfront read — still running
diff --git a/src/modules/message/bulk-message.service.ts b/src/modules/message/bulk-message.service.ts
index 9da97a312..498aa5574 100644
--- a/src/modules/message/bulk-message.service.ts
+++ b/src/modules/message/bulk-message.service.ts
@@ -394,8 +394,10 @@ export class BulkMessageService implements OnModuleInit, OnApplicationBootstrap
throw new BadRequestException(`Batch '${batchId}' is already ${batch.status}`);
}
- // Signal cancellation
- this.processingBatches.set(batch.id, false);
+ // Signal cancellation to this process's run of the batch. Only that run removes the marker, so a
+ // batch run elsewhere (or already finished here) must not get one: it would leak, and it would
+ // make a later takeover reap skip the batch.
+ if (this.processingBatches.has(batch.id)) this.processingBatches.set(batch.id, false);
// Update status — guarded to the non-terminal statuses IN the UPDATE, so a batch that reached a
// terminal state between the read above and this write is not relabelled CANCELLED after the