Skip to content

Commit

Permalink
Add WORKER_FUNC_DECORATOR feature
Browse files Browse the repository at this point in the history
  • Loading branch information
JereMalinen committed Sep 12, 2019
1 parent 563292c commit 16bceaa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions django_q/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions django_q/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import importlib
from copy import deepcopy
from signal import signal
from multiprocessing import cpu_count
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions docs/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~

Expand Down

0 comments on commit 16bceaa

Please sign in to comment.