From 88f9f118a3f98b5e2b7d37a1a1bd1d6fb2d4d929 Mon Sep 17 00:00:00 2001 From: Matias Bordese Date: Fri, 6 Oct 2023 15:31:11 -0300 Subject: [PATCH] Use partial instead of lambda when queuing ack task (#3134) 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 ``` --- engine/apps/alerts/tasks/acknowledge_reminder.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/engine/apps/alerts/tasks/acknowledge_reminder.py b/engine/apps/alerts/tasks/acknowledge_reminder.py index f1329eab9e..1f8b6f1da6 100644 --- a/engine/apps/alerts/tasks/acknowledge_reminder.py +++ b/engine/apps/alerts/tasks/acknowledge_reminder.py @@ -1,3 +1,5 @@ +from functools import partial + from django.conf import settings from django.db import transaction @@ -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) @@ -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()