Skip to content

Commit

Permalink
Fixed #26
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislav Kaledin committed Jul 10, 2018
1 parent fcc9c07 commit af1e2d4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
11 changes: 7 additions & 4 deletions django_apscheduler/jobstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 20 additions & 2 deletions tests/test_jobstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())

Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit af1e2d4

Please sign in to comment.