Skip to content

Commit

Permalink
Merge pull request #99 from jcass77/fix/98_event_classes
Browse files Browse the repository at this point in the history
Fix mapping of event listener codes to APScheduler event classes
  • Loading branch information
jcass77 authored Aug 11, 2020
2 parents 0d41a07 + 7b41683 commit 9041827
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 43 deletions.
69 changes: 40 additions & 29 deletions django_apscheduler/jobstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,41 @@ def handle_submission_event(cls, event: JobSubmissionEvent):
:param event: JobExecutionEvent instance
:return: DjangoJobExecution ID
"""
if event.code != events.EVENT_JOB_SUBMITTED:
if event.code == events.EVENT_JOB_SUBMITTED:
# Start logging a new job execution
job_execution = DjangoJobExecution.atomic_update_or_create(
cls.lock,
event.job_id,
event.scheduled_run_times[0],
DjangoJobExecution.SENT,
)
elif event.code == events.EVENT_JOB_MAX_INSTANCES:
status = DjangoJobExecution.MAX_INSTANCES

exception = (
f"Execution of job '{event.job_id}' skipped: maximum number of running "
f"instances reached!"
)

job_execution = DjangoJobExecution.atomic_update_or_create(
cls.lock,
event.job_id,
event.scheduled_run_times[0],
status,
exception=exception,
)
else:
raise NotImplementedError(
f"Don't know how to handle JobSubmissionEvent '{event.code}'. Expected "
f"'{events.EVENT_JOB_SUBMITTED}'."
f"one of '{[events.EVENT_JOB_SUBMITTED, events.EVENT_JOB_MAX_INSTANCES]}'."
)

# Start logging a new job execution
job_execution = DjangoJobExecution.atomic_update_or_create(
cls.lock,
event.job_id,
event.scheduled_run_times[0],
DjangoJobExecution.SENT,
)

return job_execution.id

@classmethod
def handle_execution_event(cls, event: JobExecutionEvent) -> int:
"""
Store successful job execution status in the database.
Store "successful" job execution status in the database.
:param event: JobExecutionEvent instance
:return: DjangoJobExecution ID
Expand All @@ -80,7 +95,7 @@ def handle_execution_event(cls, event: JobExecutionEvent) -> int:
@classmethod
def handle_error_event(cls, event: JobExecutionEvent) -> int:
"""
Store failed job execution status in the database.
Store "failed" job execution status in the database.
:param event: JobExecutionEvent instance
:return: DjangoJobExecution ID
Expand All @@ -103,19 +118,10 @@ def handle_error_event(cls, event: JobExecutionEvent) -> int:
traceback=traceback,
)

elif event.code in [events.EVENT_JOB_MAX_INSTANCES, events.EVENT_JOB_MISSED]:
elif event.code == events.EVENT_JOB_MISSED:
# Job execution will not have been logged yet - do so now
if event.code == events.EVENT_JOB_MAX_INSTANCES:
status = DjangoJobExecution.MAX_INSTANCES

exception = (
f"Execution of job '{event.job_id}' skipped: maximum number of running "
f"instances reached!"
)

else:
status = DjangoJobExecution.MISSED
exception = f"Run time of job '{event.job_id}' was missed!"
status = DjangoJobExecution.MISSED
exception = f"Run time of job '{event.job_id}' was missed!"

job_execution = DjangoJobExecution.atomic_update_or_create(
cls.lock,
Expand All @@ -134,19 +140,24 @@ def handle_error_event(cls, event: JobExecutionEvent) -> int:
return job_execution.id

def register_event_listeners(self):
"""
Register various event listeners.
See: https://github.com/agronholm/apscheduler/blob/master/docs/modules/events.rst for details on which event
class is used for each event code.
"""
self._scheduler.add_listener(
self.handle_submission_event, events.EVENT_JOB_SUBMITTED
self.handle_submission_event,
events.EVENT_JOB_SUBMITTED | events.EVENT_JOB_MAX_INSTANCES,
)

self._scheduler.add_listener(
self.handle_execution_event, events.EVENT_JOB_EXECUTED
)

self._scheduler.add_listener(
self.handle_error_event,
events.EVENT_JOB_MAX_INSTANCES
| events.EVENT_JOB_ERROR
| events.EVENT_JOB_MISSED,
self.handle_error_event, events.EVENT_JOB_ERROR | events.EVENT_JOB_MISSED,
)


Expand Down
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
This changelog is used to track all major changes to django_apscheduler.


## v0.4.2 (UNRELEASED)

**Fixes**

- Fix mapping of event listener APScheduler codes to event classes (Fixes [#98](https://github.com/jarekwg/django-apscheduler/issues/98)).


## v0.4.1 (2020-07-09)

**Fixes**
Expand Down
22 changes: 8 additions & 14 deletions tests/test_jobstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ def test_handle_submission_event_not_supported_raises_exception(self, jobstore):
jobstore.handle_submission_event(event)

@pytest.mark.django_db
@pytest.mark.parametrize(
"event_code", [events.EVENT_JOB_SUBMITTED, events.EVENT_JOB_MAX_INSTANCES,],
)
def test_handle_submission_event_creates_job_execution(
self, jobstore, create_add_job
self, event_code, jobstore, create_add_job
):
job = create_add_job(jobstore, dummy_job, datetime(2016, 5, 3))
event = JobSubmissionEvent(
events.EVENT_JOB_SUBMITTED, job.id, jobstore, [timezone.now()]
)
event = JobSubmissionEvent(event_code, job.id, jobstore, [timezone.now()])
jobstore.handle_submission_event(event)

assert DjangoJobExecution.objects.filter(job_id=event.job_id).exists()
Expand Down Expand Up @@ -75,12 +76,7 @@ def test_handle_error_event_not_supported_raises_exception(self, jobstore):

@pytest.mark.django_db
@pytest.mark.parametrize(
"event_code",
[
events.EVENT_JOB_MAX_INSTANCES,
events.EVENT_JOB_MISSED,
events.EVENT_JOB_ERROR,
],
"event_code", [events.EVENT_JOB_MISSED, events.EVENT_JOB_ERROR,],
)
def test_handle_error_event_creates_job_execution(
self, jobstore, create_add_job, event_code
Expand Down Expand Up @@ -113,11 +109,9 @@ def test_register_event_listeners_registers_listeners(self, jobstore):
assert all(
event_code in registered_event_codes
for event_code in [
events.EVENT_JOB_SUBMITTED,
events.EVENT_JOB_SUBMITTED | events.EVENT_JOB_MAX_INSTANCES,
events.EVENT_JOB_EXECUTED,
events.EVENT_JOB_MAX_INSTANCES
| events.EVENT_JOB_ERROR
| events.EVENT_JOB_MISSED,
events.EVENT_JOB_ERROR | events.EVENT_JOB_MISSED,
]
)

Expand Down

0 comments on commit 9041827

Please sign in to comment.