Explicitly close all connection for any scheduled task, ref issue #4
This commit is contained in:
@@ -1,11 +1,33 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from functools import wraps
|
||||
|
||||
from django import db
|
||||
|
||||
from .exceptions import AlreadyScheduled
|
||||
from .runtime import scheduler
|
||||
|
||||
registered_jobs = {}
|
||||
|
||||
|
||||
def close_connections(func):
|
||||
"""
|
||||
Wrapper that closes all db connection before and after execution of
|
||||
its wrapped function
|
||||
"""
|
||||
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
# This ensures the task gets a fresh db connection
|
||||
db.close_connection()
|
||||
result = func(*args, **kwargs)
|
||||
# This ensures no open connections remain after the task finishes executing
|
||||
db.close_connection()
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def register_interval_job(name, title, func, weeks=0, days=0, hours=0, minutes=0,
|
||||
seconds=0, start_date=None, args=None,
|
||||
kwargs=None, job_name=None, **options):
|
||||
@@ -13,7 +35,8 @@ def register_interval_job(name, title, func, weeks=0, days=0, hours=0, minutes=0
|
||||
if name in registered_jobs:
|
||||
raise AlreadyScheduled
|
||||
|
||||
job = scheduler.add_interval_job(func=func, weeks=weeks, days=days,
|
||||
# Wrap the user function before adding it to the scheduler
|
||||
job = scheduler.add_interval_job(func=close_connections(func), weeks=weeks, days=days,
|
||||
hours=hours, minutes=minutes, seconds=seconds,
|
||||
start_date=start_date, args=args, kwargs=kwargs, **options)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user