mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-08 20:38:12 +08:00
Fix: handling exceptions inside job function (#51)
This commit is contained in:
+3
-10
@@ -485,19 +485,12 @@ def worker(
|
|||||||
try:
|
try:
|
||||||
res = f(*task["args"], **task["kwargs"])
|
res = f(*task["args"], **task["kwargs"])
|
||||||
result = (res, True)
|
result = (res, True)
|
||||||
except Exception:
|
except Exception as e:
|
||||||
result = (
|
result = (f"{e} : {traceback.format_exc()}", False)
|
||||||
_(
|
|
||||||
"Could not process '%(func_name)s'. Check the location of the "
|
|
||||||
"function and the args/kwargs."
|
|
||||||
)
|
|
||||||
% {"func_name": func_name},
|
|
||||||
False,
|
|
||||||
)
|
|
||||||
if error_reporter:
|
if error_reporter:
|
||||||
error_reporter.report()
|
error_reporter.report()
|
||||||
if task.get("sync", False):
|
if task.get("sync", False):
|
||||||
raise Exception(result)
|
raise
|
||||||
with timer.get_lock():
|
with timer.get_lock():
|
||||||
# Process result
|
# Process result
|
||||||
task["result"] = result[0]
|
task["result"] = result[0]
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ from django_q.tasks import (
|
|||||||
result,
|
result,
|
||||||
result_group,
|
result_group,
|
||||||
)
|
)
|
||||||
from django_q.tests.tasks import multiply
|
from django_q.tests.tasks import multiply, TaskError
|
||||||
from django_q.utils import add_months, add_years
|
from django_q.utils import add_months, add_years
|
||||||
|
|
||||||
myPath = os.path.dirname(os.path.abspath(__file__))
|
myPath = os.path.dirname(os.path.abspath(__file__))
|
||||||
@@ -64,7 +64,7 @@ def test_sync(broker):
|
|||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_sync_raise_exception(broker):
|
def test_sync_raise_exception(broker):
|
||||||
with pytest.raises(Exception):
|
with pytest.raises(TaskError):
|
||||||
async_task("django_q.tests.tasks.raise_exception", broker=broker, sync=True)
|
async_task("django_q.tests.tasks.raise_exception", broker=broker, sync=True)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user