Skip to content

Commit

Permalink
Fix issue #15
Browse files Browse the repository at this point in the history
Added exception catching in float(job_execution.finished) to prevent float(None)
Added tests for such situation
Now next_run_time allowed to be blank and null. Added migration for it
  • Loading branch information
Stanislav Kaledin committed Apr 12, 2018
1 parent 80da545 commit 6313bce
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
20 changes: 20 additions & 0 deletions django_apscheduler/migrations/0002_auto_20180412_0758.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.12 on 2018-04-12 07:58
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('django_apscheduler', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='djangojob',
name='next_run_time',
field=models.DateTimeField(blank=True, db_index=True, null=True),
),
]
2 changes: 1 addition & 1 deletion django_apscheduler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __reconnect(self):

class DjangoJob(models.Model):
name = models.CharField(max_length=255, unique=True) # id of job
next_run_time = models.DateTimeField(db_index=True)
next_run_time = models.DateTimeField(db_index=True, blank=True, null=True)
# Perhaps consider using PickleField down the track.
job_state = models.BinaryField()

Expand Down
7 changes: 6 additions & 1 deletion django_apscheduler/result_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def get_or_create_job_execution(self, job, event):
"""
Create and return new job execution item.
:param job: DjangoJob instance
:type job: django_apscheduler.models.DjangoJob
:param event: JobSubmissionEvent instance
:return: JobExecution id
"""
Expand All @@ -29,7 +30,11 @@ def get_or_create_job_execution(self, job, event):

if job_execution and job_execution.started is None:
job_execution.started = time.time()
job_execution.duration = float(job_execution.finished) - float(job_execution.started)
try:
job_execution.duration = float(job_execution.finished) - float(job_execution.started)
except:
job_execution.duration = None

job_execution.save()
return job_execution.id

Expand Down
25 changes: 24 additions & 1 deletion tests/test_jobstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job
from django_apscheduler.models import DjangoJob, DjangoJobExecution, DjangoJobManager
from django_apscheduler.result_storage import DjangoResultStorage
from django_apscheduler.util import serialize_dt
from tests.compat import mock_compat

logging.basicConfig()
Expand Down Expand Up @@ -106,7 +108,28 @@ def test_job_events(db, scheduler):

assert DjangoJobExecution.objects.count() == 1

@pytest.mark.test_reconnect_on_db_error

def test_issue_15(db):
"""
This test covers bug from https://github.com/jarekwg/django-apscheduler/issues/15
"""

storage = DjangoResultStorage()

srt = datetime.datetime.now()

job = DjangoJob.objects.create(name="test", next_run_time=datetime.datetime.now())
DjangoJobExecution.objects.create(
job=job,
run_time=serialize_dt(srt)
)

storage.get_or_create_job_execution(
job,
mock_compat.Mock(scheduled_run_times=[srt])
)


def test_reconnect_on_db_error(transactional_db):

counter = [0]
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = {py36}-{dj1,dj2},{py27-dj1}
envlist = {py36}-{dj1,dj2},{py27-dj1},{py35}-{dj1,dj2}
skipsdist = true

[pytest]
Expand Down

0 comments on commit 6313bce

Please sign in to comment.