Fixes #314 - Convert func to string before saving task in databse so that resubmitting failed task works (#554)

This commit is contained in:
Kenny Heinonen
2021-05-14 13:48:08 +02:00
committed by GitHub
parent 728a1b85d0
commit 1eb5cf4b9b
+13 -12
View File
@@ -1,6 +1,7 @@
# Standard
import ast
import importlib
import inspect
import pydoc
import signal
import socket
import traceback
@@ -416,16 +417,7 @@ def worker(
f = task["func"]
# if it's not an instance try to get it from the string
if not callable(task["func"]):
try:
module, func = f.rsplit(".", 1)
m = importlib.import_module(module)
f = getattr(m, func)
except (ValueError, ImportError, AttributeError) as e:
result = (e, False)
if error_reporter:
error_reporter.report()
# We're still going
if not result:
f = pydoc.locate(f)
close_old_django_connections()
timer_value = task.pop("timeout", timeout)
# signal execution
@@ -495,10 +487,19 @@ def save_task(task, broker: Broker):
broker.acknowledge(task['ack_id'])
else:
func = task["func"]
# convert func to string
if inspect.isfunction(func):
func = f"{func.__module__}.{func.__name__}"
elif inspect.ismethod(func):
func = (
f'{func.__self__.__module__}.'
f'{func.__self__.__name__}.{func.__name__}'
)
Task.objects.create(
id=task["id"],
name=task["name"],
func=task["func"],
func=func,
hook=task.get("hook"),
args=task["args"],
kwargs=task["kwargs"],