Skip to content

Commit

Permalink
Fix issue #13
Browse files Browse the repository at this point in the history
  • Loading branch information
sallyruthstruik committed Feb 1, 2018
1 parent f455abb commit d7dad0f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 31 deletions.
22 changes: 20 additions & 2 deletions django_apscheduler/jobstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from django.core.exceptions import ObjectDoesNotExist
from django.db import connections
from django.db.utils import OperationalError
from django.db.utils import OperationalError, ProgrammingError

from django_apscheduler.models import DjangoJob
from django_apscheduler.result_storage import DjangoResultStorage
Expand All @@ -25,7 +25,7 @@ def ignore_database_error(func):
def inner(*a, **k):
try:
return func(*a, **k)
except OperationalError as e:
except (OperationalError, ProgrammingError) as e:
warnings.warn(
"Got OperationalError: {}. "
"Please, check that you have migrated the database via python manage.py migrate".format(e),
Expand Down Expand Up @@ -209,6 +209,24 @@ def register_events(scheduler, result_storage=None):


def register_job(scheduler, *a, **k):
"""
Helper decorator for job registration.
Automatically fills id parameter to prevent jobs duplication.
See this comment for explanation: https://github.com/jarekwg/django-apscheduler/pull/9#issuecomment-342074372
Usage example::
@register_job(scheduler, "interval", seconds=1)
def test_job():
time.sleep(4)
print("I'm a test job!")
:param scheduler: Scheduler instance
:type scheduler: BaseScheduler
:param a, k: Params, will be passed to scheduler.add_job method. See :func:`BaseScheduler.add_job`
"""
# type: (BaseScheduler)->callable

def inner(func):
Expand Down
36 changes: 35 additions & 1 deletion django_apscheduler/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
# coding=utf-8
from django.db import models
from django.db import models, connection
from django.utils.safestring import mark_safe
import time
import logging

LOGGER = logging.getLogger("django_apscheduler")

class DjangoJobManager(models.Manager):
"""
This manager pings database each request after 30s IDLE to prevent MysqlGoneAway error
"""
_last_ping = 0
_ping_interval = 30

def get_queryset(self):
self.__ping()
return super(DjangoJobManager, self).get_queryset()

def __ping(self):
if time.time() - self._last_ping < self._ping_interval:
return

try:
with connection.cursor() as c:
c.execute("SELECT 1")
except Exception as e:
self.__reconnect()

self._last_ping = time.time()

def __reconnect(self):
LOGGER.warning("Mysql closed the connection. Perform reconnect...")
connection.connection.close()
connection.connection = None


class DjangoJob(models.Model):
Expand All @@ -9,6 +41,8 @@ class DjangoJob(models.Model):
# Perhaps consider using PickleField down the track.
job_state = models.BinaryField()

objects = DjangoJobManager()

def __str__(self):
status = 'next run at: %s' % self.next_run_time if self.next_run_time else 'paused'
return '%s (%s)' % (self.name, status)
Expand Down
3 changes: 1 addition & 2 deletions examples/example_apscheduler/example_apscheduler/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
scheduler.add_jobstore(DjangoJobStore(), "default")


@register_job(scheduler, "interval", seconds=1)
@register_job(scheduler, "interval", seconds=30, replace_existing=True)
def test_job():
time.sleep(4)
print("I'm a test job!")
# raise ValueError("Olala!")

Expand Down
37 changes: 13 additions & 24 deletions examples/example_apscheduler/example_apscheduler/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,40 +76,29 @@
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'HOST': '127.0.0.1',
'PORT': 3308
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME': 'test',
# 'USER': 'root',
# 'HOST': '127.0.0.1',
# 'PORT': 3308
# }
# }


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},

]


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.2',
version='0.2.3',
description='APScheduler for Django',
classifiers=[
"Development Status :: 4 - Beta",
Expand Down
7 changes: 7 additions & 0 deletions tests/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

try:
import mock
except:
from unittest import mock

mock_compat = mock
26 changes: 25 additions & 1 deletion tests/test_jobstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
from apscheduler.events import JobExecutionEvent, JobSubmissionEvent
from apscheduler.executors.debug import DebugExecutor
from apscheduler.schedulers.base import BaseScheduler
from django.db import connection, transaction
from django.db.backends.utils import CursorWrapper
from django.db.models.sql.compiler import SQLCompiler
from django.db.utils import OperationalError
from pytz import utc

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

logging.basicConfig()

Expand Down Expand Up @@ -100,3 +105,22 @@ def test_job_events(db, scheduler):
scheduler._dispatch_event(JobSubmissionEvent(32768, "job", None, [now]))

assert DjangoJobExecution.objects.count() == 1

@pytest.mark.test_reconnect_on_db_error
def test_reconnect_on_db_error(transactional_db):

counter = [0]
def mocked_execute(self, *a, **k):
counter[0] += 1

if counter[0] == 1:
raise OperationalError()
else:
return []

with mock_compat.patch.object(CursorWrapper, "execute", mocked_execute):
store = DjangoJobStore()
DjangoJob.objects._last_ping = 0

assert store.get_due_jobs(now=datetime.datetime.now()) == []

0 comments on commit d7dad0f

Please sign in to comment.