10
10
from django .db import transaction
11
11
from django .utils .functional import cached_property
12
12
from redis .client import Redis
13
+ from rq .defaults import UNSERIALIZABLE_RETURN_VALUE_PAYLOAD
13
14
from rq .exceptions import NoSuchJobError
14
15
from rq .job import Callback , JobStatus
15
16
from rq .job import Job as BaseJob
@@ -116,9 +117,6 @@ def task_result(self) -> TaskResult:
116
117
117
118
exception_classes = self .meta .get ("_django_tasks_exceptions" , []).copy ()
118
119
119
- if self .worker_name and task_result .status == ResultStatus .RUNNING :
120
- task_result .worker_ids .append (self .worker_name )
121
-
122
120
rq_results = self .results ()
123
121
124
122
for rq_result in rq_results :
@@ -135,11 +133,30 @@ def task_result(self) -> TaskResult:
135
133
)
136
134
)
137
135
136
+ if self .worker_name and task_result .status == ResultStatus .RUNNING :
137
+ task_result .worker_ids .append (self .worker_name )
138
+
138
139
if rq_results :
139
- object .__setattr__ (task_result , "_return_value" , rq_results [0 ].return_value )
140
140
object .__setattr__ (
141
- task_result , "last_attempted_at " , rq_results [0 ]. created_at
141
+ task_result , "_return_value " , rq_results [- 1 ]. return_value
142
142
)
143
+ object .__setattr__ (
144
+ task_result , "last_attempted_at" , rq_results [- 1 ].created_at
145
+ )
146
+
147
+ # If the return value couldn't be serialized, a specific string is saved instead.
148
+ if task_result ._return_value == UNSERIALIZABLE_RETURN_VALUE_PAYLOAD :
149
+ # In these cases, the task should be marked as failed instead
150
+ object .__setattr__ (task_result , "status" , ResultStatus .FAILED )
151
+
152
+ task_result .errors .append (
153
+ TaskError (
154
+ exception_class_path = get_module_path (Exception ),
155
+ traceback = task_result ._return_value , # The traceback isn't stored, so this is the best we can do
156
+ )
157
+ )
158
+
159
+ object .__setattr__ (task_result , "_return_value" , None )
143
160
144
161
return task_result
145
162
0 commit comments