Replace this task, with a new task inheriting the task id. Execution of the host task ends immediately and no subsequent statements will be run. .. versionadded:: 4.0 Arguments: sig (Signature): signature to replace with. Raises: ~@
(self, sig)
| 1124 | uuid=req.id, retry=retry, retry_policy=retry_policy, **fields) |
| 1125 | |
| 1126 | def replace(self, sig): |
| 1127 | """Replace this task, with a new task inheriting the task id. |
| 1128 | |
| 1129 | Execution of the host task ends immediately and no subsequent statements |
| 1130 | will be run. |
| 1131 | |
| 1132 | .. versionadded:: 4.0 |
| 1133 | |
| 1134 | Arguments: |
| 1135 | sig (Signature): signature to replace with. |
| 1136 | |
| 1137 | Raises: |
| 1138 | ~@Ignore: This is always raised when called in asynchronous context. |
| 1139 | It is best to always use ``return self.replace(...)`` to convey |
| 1140 | to the reader that the task won't continue after being replaced. |
| 1141 | """ |
| 1142 | chord = self.request.chord |
| 1143 | if 'chord' in sig.options: |
| 1144 | raise ImproperlyConfigured( |
| 1145 | "A signature replacing a task must not be part of a chord" |
| 1146 | ) |
| 1147 | if isinstance(sig, _chain) and not getattr(sig, "tasks", True): |
| 1148 | raise ImproperlyConfigured("Cannot replace with an empty chain") |
| 1149 | |
| 1150 | # Ensure callbacks or errbacks from the replaced signature are retained |
| 1151 | if isinstance(sig, group): |
| 1152 | # Groups get uplifted to a chord so that we can link onto the body |
| 1153 | sig |= self.app.tasks['celery.accumulate'].s(index=0) |
| 1154 | if isinstance(sig, _chain) and isinstance(sig.tasks[-1], group): |
| 1155 | sig.tasks = list(sig.tasks) |
| 1156 | sig.tasks[-1] |= self.app.tasks['celery.accumulate'].s(index=0) |
| 1157 | for callback in maybe_list(self.request.callbacks) or []: |
| 1158 | sig.link(callback) |
| 1159 | for errback in maybe_list(self.request.errbacks) or []: |
| 1160 | sig.link_error(errback) |
| 1161 | # If the replacement signature is a chain, we need to move callbacks |
| 1162 | # down to the final task so they run at the right time even if we |
| 1163 | # proceed to link further tasks from the original request below. |
| 1164 | # Leaving them on the chain as well would link them twice. |
| 1165 | if isinstance(sig, _chain) and "link" in sig.options: |
| 1166 | final_task_links = sig.tasks[-1].options.setdefault("link", []) |
| 1167 | final_task_links.extend(maybe_list(sig.options.pop("link"))) |
| 1168 | # We need to freeze the replacement signature with the current task's |
| 1169 | # ID to ensure that we don't disassociate it from the existing task IDs |
| 1170 | # which would break previously constructed results objects. |
| 1171 | sig.freeze(self.request.id) |
| 1172 | # Ensure the important options from the original signature are retained |
| 1173 | replaced_task_nesting = self.request.get('replaced_task_nesting', 0) + 1 |
| 1174 | sig.set( |
| 1175 | chord=chord, |
| 1176 | group_id=self.request.group, |
| 1177 | group_index=self.request.group_index, |
| 1178 | root_id=self.request.root_id, |
| 1179 | replaced_task_nesting=replaced_task_nesting |
| 1180 | ) |
| 1181 | |
| 1182 | # If the replaced task is a chain, we want to set all of the chain tasks |
| 1183 | # with the same replaced_task_nesting value to mark their replacement nesting level |