Trigger distribute task after alert is committed (#3420)

Fix issue triggering task retries because alert is not yet committed to
the DB.
Similar to https://github.com/grafana/oncall/pull/3001.
This commit is contained in:
Matias Bordese 2023-11-24 09:02:32 -03:00 committed by GitHub
parent 3436344f1d
commit d730f6b2bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 11 deletions

View file

@ -144,7 +144,9 @@ class Alert(models.Model):
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

View file

@ -7,22 +7,32 @@ from apps.alerts.tasks import distribute_alert, escalate_alert_group
@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