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 # Standard
import ast import ast
import importlib import inspect
import pydoc
import signal import signal
import socket import socket
import traceback import traceback
@@ -416,16 +417,7 @@ def worker(
f = task["func"] f = task["func"]
# if it's not an instance try to get it from the string # if it's not an instance try to get it from the string
if not callable(task["func"]): if not callable(task["func"]):
try: f = pydoc.locate(f)
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:
close_old_django_connections() close_old_django_connections()
timer_value = task.pop("timeout", timeout) timer_value = task.pop("timeout", timeout)
# signal execution # signal execution
@@ -495,10 +487,19 @@ def save_task(task, broker: Broker):
broker.acknowledge(task['ack_id']) broker.acknowledge(task['ack_id'])
else: 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( Task.objects.create(
id=task["id"], id=task["id"],
name=task["name"], name=task["name"],
func=task["func"], func=func,
hook=task.get("hook"), hook=task.get("hook"),
args=task["args"], args=task["args"],
kwargs=task["kwargs"], kwargs=task["kwargs"],