Skip to content

Commit

Permalink
Trigger distribute task after alert is committed (#3420)
Browse files Browse the repository at this point in the history
Fix issue triggering task retries because alert is not yet committed to
the DB.
Similar to #3001.
  • Loading branch information
matiasb authored Nov 24, 2023
1 parent 3436344 commit d730f6b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
4 changes: 3 additions & 1 deletion engine/apps/alerts/models/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ def create(
if settings.DEBUG:
tasks.distribute_alert(alert.pk)
else:
tasks.distribute_alert.apply_async((alert.pk,), countdown=TASK_DELAY_SECONDS)
transaction.on_commit(
partial(tasks.distribute_alert.apply_async, (alert.pk,), countdown=TASK_DELAY_SECONDS)
)

if group_created:
# all code below related to maintenance mode
Expand Down
30 changes: 20 additions & 10 deletions engine/apps/alerts/tests/test_alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,32 @@


@pytest.mark.django_db
def test_alert_create_default_channel_filter(make_organization, make_alert_receive_channel, make_channel_filter):
@patch("apps.alerts.tasks.distribute_alert.distribute_alert.apply_async", return_value=None)
def test_alert_create_default_channel_filter(
mocked_distribute_alert_task,
make_organization,
make_alert_receive_channel,
make_channel_filter,
django_capture_on_commit_callbacks,
):
organization = make_organization()
alert_receive_channel = make_alert_receive_channel(organization)
channel_filter = make_channel_filter(alert_receive_channel, is_default=True)

alert = Alert.create(
title="the title",
message="the message",
alert_receive_channel=alert_receive_channel,
raw_request_data={},
integration_unique_data={},
image_url=None,
link_to_upstream_details=None,
)
with django_capture_on_commit_callbacks(execute=True) as callbacks:
alert = Alert.create(
title="the title",
message="the message",
alert_receive_channel=alert_receive_channel,
raw_request_data={},
integration_unique_data={},
image_url=None,
link_to_upstream_details=None,
)

assert alert.group.channel_filter == channel_filter
assert len(callbacks) == 1
mocked_distribute_alert_task.assert_called_once_with((alert.pk,), countdown=1)


@pytest.mark.django_db
Expand Down

0 comments on commit d730f6b

Please sign in to comment.