Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(task-processor): implement grace period for deleting old recurring task #3169

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/task_processor/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ def run_recurring_tasks(num_tasks: int = 1) -> typing.List[RecurringTaskRun]:
task_runs = []

for task in tasks:
# Remove the task if it's not registered anymore
if not task.is_task_registered:
task.delete()
logger.warning(
"Recurring task %s is not registered anymore", task.task_identifier
)
continue

if task.should_execute:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import time
import uuid
from datetime import timedelta
Expand Down Expand Up @@ -150,10 +151,13 @@ def _create_organisation(organisation_name):
assert RecurringTaskRun.objects.filter(task=task).count() == 1


def test_run_recurring_tasks_deletes_the_task_if_it_is_not_registered(
db, run_by_processor
):
def test_run_recurring_tasks_generates_warning_if_task_is_not_registered(
db: None, run_by_processor: None, caplog: pytest.LogCaptureFixture
) -> None:
# Given
task_processor_logger = logging.getLogger("task_processor")
task_processor_logger.propagate = True

task_identifier = "test_unit_task_processor_processor._a_task"

@register_recurring_task(run_every=timedelta(milliseconds=100))
Expand All @@ -168,7 +172,13 @@ def _a_task():

# Then
assert len(task_runs) == 0
assert not RecurringTask.objects.filter(task_identifier=task_identifier).exists()
assert RecurringTask.objects.filter(task_identifier=task_identifier).exists()
assert caplog.records[0].levelname == "WARNING"

assert (
caplog.records[0].message
== f"Recurring task {task_identifier} is not registered anymore"
)


def test_run_task_runs_task_and_creates_task_run_object_when_failure(db):
Expand Down