Skip to content

Commit a75ce3b

Browse files
authored
fix(alerts): correct p-queue usage (#1355)
Avoid awating on the call to `add()`. Doing so causes the call to await not only for the function to be enqueued, but also finish execution. This section of the [documentation](https://www.npmjs.com/package/p-queue) is key: > [!IMPORTANT] If you await this promise, you will wait for the task to finish running, which may defeat the purpose of using a queue for concurrency. See the [Usage](https://www.npmjs.com/package/p-queue#usage) section for examples.
1 parent 63fcf14 commit a75ce3b

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

.changeset/odd-countries-judge.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hyperdx/api": patch
3+
---
4+
5+
Fix check alert to actually honor concurrent evaluation.

packages/api/src/tasks/checkAlerts/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ export default class CheckAlertTask implements HdxTask<CheckAlertsTaskArgs> {
793793
);
794794

795795
for (const alert of alerts) {
796-
await this.task_queue.add(() =>
796+
this.task_queue.add(async () =>
797797
processAlert(
798798
alertTask.now,
799799
alert,
@@ -855,7 +855,7 @@ export default class CheckAlertTask implements HdxTask<CheckAlertsTaskArgs> {
855855
for (const task of alertTasks) {
856856
const teamWebhooksById =
857857
teamToWebhooks.get(task.conn.team.toString()) ?? new Map();
858-
await this.task_queue.add(() =>
858+
this.task_queue.add(async () =>
859859
this.processAlertTask(task, teamWebhooksById),
860860
);
861861
}
@@ -866,6 +866,9 @@ export default class CheckAlertTask implements HdxTask<CheckAlertsTaskArgs> {
866866
'finished scheduling alert tasks on the task_queue',
867867
);
868868

869+
// make sure to await here to drain the work queue and allow
870+
// functions to execute. if not, execute will terminate without
871+
// executing all checks
869872
await this.task_queue.onIdle();
870873
logger.info(
871874
{

0 commit comments

Comments
 (0)