def girder_before_task_publish(sender=None, body=None, exchange=None, routing_key=None, headers=None, properties=None, declare=None, retry_policy=None, **kwargs): if is_builtin_celery_task(sender): return job = None try: context = get_context() if 'jobInfoSpec' not in headers: job = context.create_task_job( Task.girder_job_defaults(), sender=sender, body=body, exchange=exchange, routing_key=routing_key, headers=headers, properties=properties, declare=declare, retry_policy=retry_policy, **kwargs) if 'girder_api_url' not in headers: context.attach_girder_api_url(sender=sender, body=body, exchange=exchange, routing_key=routing_key, headers=headers, properties=properties, declare=declare, retry_policy=retry_policy, **kwargs) if 'girder_client_token' not in headers: context.attach_girder_client_token(sender=sender, body=body, exchange=exchange, routing_key=routing_key, headers=headers, properties=properties, declare=declare, retry_policy=retry_policy, **kwargs) if 'girder_result_hooks' in headers: if job is not None: for result_hook in headers['girder_result_hooks']: if isinstance(result_hook, ResultTransform): result_hook.job = job # Celery task headers are not automatically serialized by celery # before being passed off to ampq for byte packing. We will have # to do that here. p = jsonpickle.pickler.Pickler() headers['girder_result_hooks'] = \ [p.flatten(grh) for grh in headers['girder_result_hooks']] # Finally, remove all reserved_options from headers for key in Task.reserved_options: headers.pop(key, None) except Exception: logger.exception('An error occurred in girder_before_task_publish.') raise
def job(self): context = get_context() if self._job is None: self._job = context.get_async_result_job_property(self) return self._job