Skip to content

Commit

Permalink
Use partial instead of lambda when queuing ack task (#3134)
Browse files Browse the repository at this point in the history
Prefer partial since it is the what the [docs
suggests](https://docs.djangoproject.com/en/4.2/topics/db/transactions/#performing-actions-after-commit).
Also because partial is evaluated immediately while lambda is evaluated
at runtime (which may be causing some issues):

```
>>> from functools import partial
>>> def foo(a, b, c):
...   print(a, b, c)
... 
>>> x = 10
>>> bar_partial = partial(foo, 1, 2, x)
>>> bar_lambda = lambda: foo(1, 2, x)
>>> x = 20
>>> bar_partial()
1 2 10
>>> bar_lambda()
1 2 20
```
  • Loading branch information
matiasb authored Oct 6, 2023
1 parent 525c963 commit 88f9f11
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions engine/apps/alerts/tasks/acknowledge_reminder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from functools import partial

from django.conf import settings
from django.db import transaction

Expand Down Expand Up @@ -60,7 +62,7 @@ def acknowledge_reminder_task(alert_group_pk: int, unacknowledge_process_id: str
log_record = alert_group.log_records.create(
type=AlertGroupLogRecord.TYPE_ACK_REMINDER_TRIGGERED, author=alert_group.acknowledged_by_user
)
transaction.on_commit(lambda: send_alert_group_signal.delay(log_record.pk))
transaction.on_commit(partial(send_alert_group_signal.delay, log_record.pk))


@shared_dedicated_queue_retry_task(autoretry_for=(Exception,), retry_backoff=True, max_retries=MAX_RETRIES)
Expand Down Expand Up @@ -108,6 +110,6 @@ def unacknowledge_timeout_task(alert_group_pk: int, unacknowledge_process_id: st
log_record = alert_group.log_records.create(
type=AlertGroupLogRecord.TYPE_AUTO_UN_ACK, author=alert_group.acknowledged_by_user
)
transaction.on_commit(lambda: send_alert_group_signal.delay(log_record.pk))
transaction.on_commit(partial(send_alert_group_signal.delay, log_record.pk))
alert_group.unacknowledge()
alert_group.start_escalation_if_needed()

0 comments on commit 88f9f11

Please sign in to comment.