From 532199fff3ed872662efbc8ced4f7fac2919ac02 Mon Sep 17 00:00:00 2001 From: Ilan Steemers Date: Wed, 1 Jul 2020 14:18:04 +0200 Subject: [PATCH] Trigger cron validation --- django_q/tasks.py | 8 ++++++-- django_q/tests/test_scheduler.py | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/django_q/tasks.py b/django_q/tasks.py index 27145f1..1e6ca3c 100644 --- a/django_q/tasks.py +++ b/django_q/tasks.py @@ -106,7 +106,7 @@ def schedule(func, *args, **kwargs): raise IntegrityError("A schedule with the same name already exists.") # create and return the schedule - return Schedule.objects.create( + s = Schedule( name=name, func=func, hook=hook, @@ -116,8 +116,12 @@ def schedule(func, *args, **kwargs): minutes=minutes, repeats=repeats, next_run=next_run, - cron=cron + cron=cron, ) + # make sure we trigger validation + s.full_clean() + s.save() + return s def result(task_id, wait=0, cached=Conf.CACHED): diff --git a/django_q/tests/test_scheduler.py b/django_q/tests/test_scheduler.py index 7cd700e..4625bfa 100644 --- a/django_q/tests/test_scheduler.py +++ b/django_q/tests/test_scheduler.py @@ -3,6 +3,7 @@ from multiprocessing import Event, Value import arrow import pytest +from django.core.exceptions import ValidationError from django.db import IntegrityError from django.utils import timezone @@ -100,6 +101,12 @@ def test_scheduler(broker, monkeypatch): assert hasattr(cron_schedule, 'pk') is True assert cron_schedule.full_clean() is None assert cron_schedule.__unicode__() == 'django_q.tests.tasks.word_multiply' + with pytest.raises(ValidationError): + cron_schedule = create_schedule('django_q.tests.tasks.word_multiply', + 2, + word='django', + schedule_type=Schedule.CRON, + cron="0 22 * * 1-12") # All other types for t in Schedule.TYPE: if t[0] == Schedule.CRON: