mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-08 03:38:11 +08:00
@@ -400,6 +400,29 @@ def monitor(result_queue: Queue, broker: Broker = None):
|
|||||||
logger.info(_(f"{name} stopped monitoring results"))
|
logger.info(_(f"{name} stopped monitoring results"))
|
||||||
|
|
||||||
|
|
||||||
|
def _check_task_timed_out(key, task: dict):
|
||||||
|
result = None
|
||||||
|
broker = get_broker()
|
||||||
|
cache = broker.cache
|
||||||
|
working_set = cache.get(key) or set()
|
||||||
|
if task["id"] in working_set:
|
||||||
|
# the previous worker has timedout and wasn't given chance to clear
|
||||||
|
raise Exception(f"Task Timed-out: {task}.")
|
||||||
|
else:
|
||||||
|
working_set.add(task['id'])
|
||||||
|
cache.set(key, working_set, timeout=Conf.RETRY * 3)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def _clear_task_timeout_cache(key, task):
|
||||||
|
broker = get_broker()
|
||||||
|
cache = broker.cache
|
||||||
|
working_set = cache.get(key) or set()
|
||||||
|
if task["id"] in working_set:
|
||||||
|
working_set.remove(task['id'])
|
||||||
|
cache.set(key, working_set, timeout=Conf.RETRY * 3)
|
||||||
|
|
||||||
|
|
||||||
def worker(
|
def worker(
|
||||||
task_queue: Queue, result_queue: Queue, timer: Value, timeout: int = Conf.TIMEOUT
|
task_queue: Queue, result_queue: Queue, timer: Value, timeout: int = Conf.TIMEOUT
|
||||||
):
|
):
|
||||||
@@ -415,6 +438,8 @@ def worker(
|
|||||||
task_count = 0
|
task_count = 0
|
||||||
if timeout is None:
|
if timeout is None:
|
||||||
timeout = -1
|
timeout = -1
|
||||||
|
|
||||||
|
working_tasks_key = "DJANGO-Q-WORKING-TASKS"
|
||||||
# Start reading the task queue
|
# Start reading the task queue
|
||||||
for task in iter(task_queue.get, "STOP"):
|
for task in iter(task_queue.get, "STOP"):
|
||||||
result = None
|
result = None
|
||||||
@@ -434,7 +459,11 @@ def worker(
|
|||||||
pre_execute.send(sender="django_q", func=f, task=task)
|
pre_execute.send(sender="django_q", func=f, task=task)
|
||||||
# execute the payload
|
# execute the payload
|
||||||
timer.value = timer_value # Busy
|
timer.value = timer_value # Busy
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
if Conf.FAIL_ON_TIMEOUT:
|
||||||
|
_check_task_timed_out(working_tasks_key, task)
|
||||||
res = f(*task["args"], **task["kwargs"])
|
res = f(*task["args"], **task["kwargs"])
|
||||||
result = (res, True)
|
result = (res, True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -443,6 +472,9 @@ def worker(
|
|||||||
error_reporter.report()
|
error_reporter.report()
|
||||||
if task.get("sync", False):
|
if task.get("sync", False):
|
||||||
raise
|
raise
|
||||||
|
if Conf.FAIL_ON_TIMEOUT:
|
||||||
|
_clear_task_timeout_cache(working_tasks_key, task)
|
||||||
|
|
||||||
with timer.get_lock():
|
with timer.get_lock():
|
||||||
# Process result
|
# Process result
|
||||||
task["result"] = result[0]
|
task["result"] = result[0]
|
||||||
|
|||||||
@@ -123,6 +123,9 @@ class Conf:
|
|||||||
# Number of seconds to wait for a worker to finish.
|
# Number of seconds to wait for a worker to finish.
|
||||||
TIMEOUT = conf.get("timeout", None)
|
TIMEOUT = conf.get("timeout", None)
|
||||||
|
|
||||||
|
# Whether to fail the task when it times-out.
|
||||||
|
FAIL_ON_TIMEOUT = conf.get("fail_on_timeout", False)
|
||||||
|
|
||||||
# Whether to acknowledge unsuccessful tasks.
|
# Whether to acknowledge unsuccessful tasks.
|
||||||
# This causes failed tasks to be considered delivered, thereby removing them from
|
# This causes failed tasks to be considered delivered, thereby removing them from
|
||||||
# the task queue. Defaults to False.
|
# the task queue. Defaults to False.
|
||||||
|
|||||||
@@ -70,6 +70,12 @@ Set this to something that makes sense for your project. Can be overridden for i
|
|||||||
|
|
||||||
See :ref:`retry` for details how to set values for timeout and retry.
|
See :ref:`retry` for details how to set values for timeout and retry.
|
||||||
|
|
||||||
|
fail_on_timeout
|
||||||
|
~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
When set to ``True``, timeouts will result in error. Defaults to ``False``.
|
||||||
|
|
||||||
|
|
||||||
.. _ack_failures:
|
.. _ack_failures:
|
||||||
|
|
||||||
ack_failures
|
ack_failures
|
||||||
|
|||||||
Reference in New Issue
Block a user