Skip to content

Commit

Permalink
add tests, fixtures and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
jefer94 committed Jun 6, 2024
1 parent 333c848 commit 5ad3f44
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 10 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/v1.5.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# v1.5.0

- All tasks with the status `PENDING`, `PAUSED` or `SCHEDULED` won't be deleted.
- All tasks will be processed in batches of `100`.
- All tasks will be deleted in batches of `1000`.
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ nav:
- "getting-started/set-up.md"
- "getting-started/task.md"
- "getting-started/schedule-tasks.md"
- Changelog:
- "changelog/v1.5.0.md"

plugins:
- search
Expand Down
2 changes: 1 addition & 1 deletion src/task_manager/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2024-present jefer94 <[email protected]>
#
# SPDX-License-Identifier: LGPL-3.0-or-later
__version__ = "1.4.0"
__version__ = "1.5.0"
1 change: 1 addition & 0 deletions src/task_manager/core/pytest/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .fixtures import * # NOQA F401
1 change: 1 addition & 0 deletions src/task_manager/core/pytest/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .dont_wait_for_rescheduling_tasks import * # NOQA F401
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from unittest.mock import patch

import pytest

__all__ = ["dont_wait_for_rescheduling_tasks"]


@pytest.fixture(autouse=True)
def dont_wait_for_rescheduling_tasks():
"""
Don't wait for rescheduling tasks by default.
You can re-enable it within a test by calling the provided wrapper.
"""

from task_manager.core.settings import set_settings

set_settings(RETRIES_LIMIT=2)

with patch("task_manager.core.decorators.Task.reattempt_settings", lambda *args, **kwargs: dict()):
with patch("task_manager.core.decorators.Task.circuit_breaker_settings", lambda *args, **kwargs: dict()):
yield
24 changes: 16 additions & 8 deletions src/task_manager/management/commands/task_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

# from breathecode.notify.actions import send_email_message
from django.core.management.base import BaseCommand
from django.db.models import Q
from django.db.models import Q, QuerySet
from django.utils import timezone
from django.db.models import QuerySet

from task_manager.django import tasks
from task_manager.django.models import ScheduledTask
Expand Down Expand Up @@ -68,22 +67,29 @@ def delete_items(self, qs: QuerySet[Any]):

while True:
to_delete = qs[0:limit]
pks = to_delete.values_list("pk")

if to_delete.count() == 0:
if len(pks) == 0:
break

to_delete.delete()
qs.filter(pk__in=pks).delete()

def clean_older_tasks(self):

date_limit = self.utc_now - timedelta(days=5)
# print("errors =", TaskManager.objects.filter(created_at__lt=date_limit, status="ERROR"))
errors = TaskManager.objects.filter(created_at__lt=date_limit, status="ERROR")

date_limit = self.utc_now - timedelta(days=2)
alright = TaskManager.objects.filter(created_at__lt=date_limit).exclude(Q(status="ERROR") | Q(status="PENDING"))
# print("alright =", TaskManager.objects.filter(created_at__lt=date_limit).exclude(status="ERROR"))
alright = TaskManager.objects.filter(created_at__lt=date_limit).exclude(
Q(status="ERROR") | Q(status="PENDING") | Q(status="SCHEDULED") | Q(status="PAUSED")
)

count_errors = errors.count()
count_alright = alright.count()
print(errors)
print(alright)
self.delete_items(errors)
self.delete_items(alright)

Expand Down Expand Up @@ -119,7 +125,8 @@ def run_scheduled_tasks(self):
handler.delay(*scheduled_task.arguments["args"], **scheduled_task.arguments["kwargs"])
scheduled += 1

scheduled_tasks.delete()
pks = scheduled_tasks.values_list("pk", flat=True)
ScheduledTask.objects.filter(pk__in=pks).delete()

self.delete_items(cancelled_tasks)

Expand All @@ -129,7 +136,7 @@ def rerun_pending_tasks(self):
tolerance = timedelta(minutes=TOLERANCE)

task_managers = TaskManager.objects.filter(last_run__lt=self.utc_now - tolerance, status="PENDING")
if count := task_managers.count() == 0:
if (count := task_managers.count()) == 0:
msg = self.style.SUCCESS("No TaskManager's available to re-run")
self.stdout.write(self.style.SUCCESS(msg))
return
Expand All @@ -139,10 +146,11 @@ def rerun_pending_tasks(self):
page = 0

while True:
a = page * limit

if a >= count:
break

a = page * limit
page += 1
b = page * limit

Expand Down
24 changes: 23 additions & 1 deletion tests/management/commands/test_task_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_clean_older_tasks__with_2__all_tasks_is_old(database, arrange, set_date

utc_now = timezone.now()

_ = arrange(2)
_ = arrange(2, {"status": "DONE"})

set_datetime(utc_now + delta)

Expand All @@ -132,6 +132,28 @@ def test_clean_older_tasks__with_2__all_tasks_is_old(database, arrange, set_date
assert database.list_of("task_manager.TaskManager") == []


# When: 2 TaskManager's, all tasks is old
# Then: remove all tasks
@pytest.mark.parametrize("delta", clean_older_tasks["long_delta_list"][:1])
@pytest.mark.parametrize("status", ["PENDING", "PAUSED", "SCHEDULED"])
def test_clean_older_tasks__with_2__all_tasks_is_old__but_these_statuses_cannot_be_deleted(
database, arrange, set_datetime, delta, patch, status, get_json_obj
):
patch(clean_older_tasks=True, rerun_pending_tasks=False, daily_report=False)

utc_now = timezone.now()

model = arrange(2, {"status": status})

set_datetime(utc_now + delta)

command = Command()
res = command.handle()

assert res is None
assert database.list_of("task_manager.TaskManager") == get_json_obj(model.task_manager)


# When: 0 TaskManager's
# Then: nothing happens
def test_rerun_pending_tasks__with_0(database, capsys, patch, get_json_obj):
Expand Down

0 comments on commit 5ad3f44

Please sign in to comment.