diff --git a/django_q/cluster.py b/django_q/cluster.py index 42f750d6..bd3e4001 100644 --- a/django_q/cluster.py +++ b/django_q/cluster.py @@ -368,6 +368,8 @@ def worker(task_queue, result_queue, timer, timeout=Conf.TIMEOUT): result = (e, False) if error_reporter: error_reporter.report() + if Conf.WORKER_FUNC_DECORATOR: + f = Conf.WORKER_FUNC_DECORATOR(f) # We're still going if not result: db.close_old_connections() diff --git a/django_q/conf.py b/django_q/conf.py index 6db61260..0cd70883 100644 --- a/django_q/conf.py +++ b/django_q/conf.py @@ -1,4 +1,5 @@ import logging +import importlib from copy import deepcopy from signal import signal from multiprocessing import cpu_count @@ -98,6 +99,13 @@ class Conf(object): # Option to undaemonize the workers and allow them to spawn child processes DAEMONIZE_WORKERS = conf.get('daemonize_workers', True) + # Makes possible to decorate all task functions similarly + WORKER_FUNC_DECORATOR = conf.get('worker_func_decorator', None) + if WORKER_FUNC_DECORATOR: + module, func = WORKER_FUNC_DECORATOR.rsplit('.', 1) + m = importlib.import_module(module) + WORKER_FUNC_DECORATOR = getattr(m, func) + # Maximum number of tasks that each cluster can work on QUEUE_LIMIT = conf.get('queue_limit', int(WORKERS) ** 2) diff --git a/docs/configure.rst b/docs/configure.rst index 15070806..450772ff 100644 --- a/docs/configure.rst +++ b/docs/configure.rst @@ -46,6 +46,13 @@ daemonize_workers Set the daemon flag when spawning workers. You may need to disable this flag if your worker needs to spawn child process but be careful with orphaned child processes in case of sudden termination of the main process. Defaults to ``True``. +worker_func_decorator +~~~~~~~~~~~~~~~~~~~~~ + +Can be used to decorate all task functions before calling. Should be a string in the form 'package.module.my_function'. +The function gets ``func`` as parameter and must return decorated function. +Defaults to ``None``. + recycle ~~~~~~~