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 deleting schedules #25

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions django_toosimple_q/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ def last_due_(self, obj):

@admin.display()
def next_due_(self, obj):
# for schedule not in the code anymore
if not obj.schedule:
return "invalid"

if len(obj.past_dues) >= 1:
next_due = obj.past_dues[0]
else:
Expand Down
4 changes: 4 additions & 0 deletions django_toosimple_q/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ def icon(self):

@cached_property
def past_dues(self):
if self.schedule is None:
# Deal with invalid schedule (e.g. deleted from the code but still in the DB)
return []

if self.schedule.cron == "manual":
# A manual schedule is never due
return []
Expand Down
27 changes: 25 additions & 2 deletions django_toosimple_q/tests/tests_regression.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import time

from django.core import management

from django_toosimple_q.decorators import register_task, schedule_task
from django_toosimple_q.models import TaskExec
from django_toosimple_q.registry import schedules_registry, tasks_registry

from .base import TooSimpleQBackgroundTestCase
from .base import TooSimpleQBackgroundTestCase, TooSimpleQRegularTestCase


class TestRegression(TooSimpleQBackgroundTestCase):
class TestRegressionBackground(TooSimpleQBackgroundTestCase):
def test_regr_schedule_short(self):
# Regression test for an issue where a schedule with smaller periods was not always processed

Expand All @@ -17,3 +21,22 @@ def test_regr_schedule_short(self):

# It should do almost 20 tasks
self.assertGreaterEqual(TaskExec.objects.all().count(), 18)


class TestRegressionRegular(TooSimpleQRegularTestCase):
def test_deleting_schedule(self):
# Regression test for an issue where deleting a schedule in code would crash the admin view

@schedule_task(cron="0 12 * * *", datetime_kwarg="scheduled_on")
@register_task(name="normal")
def a(scheduled_on):
return f"{scheduled_on:%Y-%m-%d %H:%M}"

management.call_command("worker", "--until_done")

schedules_registry.clear()
tasks_registry.clear()

# the admin view still works even for deleted schedules
response = self.client.get("/admin/toosimpleq/scheduleexec/")
self.assertEqual(response.status_code, 200)
Loading