diff --git a/django_apscheduler/jobstores.py b/django_apscheduler/jobstores.py index 73d1656..9156660 100644 --- a/django_apscheduler/jobstores.py +++ b/django_apscheduler/jobstores.py @@ -72,13 +72,14 @@ def get_due_jobs(self, now): LOGGER.exception("Exception during getting jobs") return [] - @ignore_database_error() def get_next_run_time(self): try: return deserialize_dt(DjangoJob.objects.filter(next_run_time__isnull=False).earliest('next_run_time').next_run_time) - except AttributeError: # no active jobs - return None + except ObjectDoesNotExist: # no active jobs + return + except: + LOGGER.exception("Exception during get_next_run_time for jobs") @ignore_database_error(on_error_value=[]) def get_all_jobs(self): @@ -157,7 +158,9 @@ def _get_jobs(self, **filters): failed_job_ids.add(job_id) # Remove all the jobs we failed to restore - DjangoJob.objects.filter(name__in=failed_job_ids).delete() + if failed_job_ids: + LOGGER.warning("Remove bad jobs: %s", failed_job_ids) + DjangoJob.objects.filter(name__in=failed_job_ids).delete() def map_jobs(job): job.next_run_time = deserialize_dt(job.next_run_time) diff --git a/setup.py b/setup.py index 5859319..357db30 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='django-apscheduler', - version='0.2.10', + version='0.2.12', description='APScheduler for Django', classifiers=[ "Development Status :: 4 - Beta", diff --git a/tests/test_jobstore.py b/tests/test_jobstore.py index 0aad6ab..a7d3e3a 100644 --- a/tests/test_jobstore.py +++ b/tests/test_jobstore.py @@ -4,6 +4,7 @@ import logging import pytest +import pytz from apscheduler.events import JobExecutionEvent, JobSubmissionEvent from apscheduler.executors.debug import DebugExecutor from apscheduler.schedulers.base import BaseScheduler @@ -28,12 +29,12 @@ def shutdown(self, wait=True): pass def wakeup(self): - pass + self._process_jobs() @pytest.fixture() def scheduler(): - scheduler = DebugScheduler() + scheduler = DebugScheduler(timezone=pytz.timezone("Europe/Moscow")) scheduler.add_jobstore(DjangoJobStore()) scheduler.add_executor(DebugExecutor()) @@ -60,6 +61,7 @@ def test_add_job(db, scheduler): assert DjangoJob.objects.count() == 1 + def test_issue_20(db, scheduler): assert isinstance(scheduler, DebugScheduler) scheduler.add_job(job, trigger="interval", seconds=1, id="job") @@ -87,6 +89,22 @@ def test_remove_job(db, scheduler): assert len(scheduler.get_jobs()) == 0 +def job_for_tests(): + job_for_tests.mock() + + +job_for_tests.mock = mock_compat.Mock() + + +def test_try_add_job_then_start(db, scheduler): + assert isinstance(scheduler, DebugScheduler) + + scheduler.add_job(job_for_tests, next_run_time=datetime.datetime.now(pytz.timezone("Europe/Moscow")), misfire_grace_time=None) + scheduler.start() + scheduler._process_jobs() + assert job_for_tests.mock.call_count == 1 + + def test_register_job_dec(db, scheduler): register_job(scheduler, "interval", seconds=1)(job)