Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Inherit the timeout from a lower leverl exception, so user code is le…
Browse files Browse the repository at this point in the history
…ss likely to catch it
  • Loading branch information
stumpylog committed Aug 31, 2022
1 parent 73c3ccb commit cf4f247
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
13 changes: 3 additions & 10 deletions django_q/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,10 @@ def worker(
with UnixSignalDeathPenalty(timeout=timeout):
res = f(*task["args"], **task["kwargs"])
result = (res, True)
except Exception as e:
# Minor QoL, parse the exception chain as far as
# possible to check if this was a timeout or just an error
exception_chain = [e]
next_exception = e.__cause__
while next_exception is not None:
exception_chain.append(next_exception)
next_exception = next_exception.__cause__
if any(isinstance(x, JobTimeoutException) for x in exception_chain):
timed_out = True
except (JobTimeoutException, Exception) as e:
result = (f"{e} : {traceback.format_exc()}", False)
if isinstance(e, JobTimeoutException):
timed_out = True
if error_reporter:
error_reporter.report()
if task.get("sync", False):
Expand Down
5 changes: 3 additions & 2 deletions django_q/timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import signal


class JobTimeoutException(Exception):
class JobTimeoutException(SystemExit):
"""Raised when a job takes longer to complete than the allowed maximum
timeout value.
timeout value. Inherits from SystemExit to prevent user code which catches
Exception from not timing out correctly.
"""
pass

Expand Down

0 comments on commit cf4f247

Please sign in to comment.