Tasks
- class django_declarative_apis.machinery.attributes.EndpointTask(task_runner=None, depends_on=None, priority=0, **kwargs)[source]
taskis used as a decorator on a function. It encapsulate the side-effect operations of an endpoint. For instance, if hitting an endpoint causes an operation to happen in another resource or it causes an operation to be queued and run as a background task.taskruns synchronously, which means it will be executed before the response is returned to the user. It can also affect the response by making changes to theEndpointDefinition.resource().Example
from django_declarative_apis.machinery import task class SampleClass: # code @task def sample_function(): # your code goes here
- Parameters:
task_runner (optional) – A callable that dictates how the task is executed. Defaults to
None.depends_on (optional) – A reference to another task that should be run before this one. Overrides
priority. Defaults toNone. It is important to note thatdeferrable_taskcannot be used as adepends_onargument.priority (optional) – Specifies the priority of the task. Tasks with lower priority are executed first. Defaults to
0.
Example
from django_declarative_apis.machinery import task class SampleEndpointDefinition: def is_authorized(self): return True @task def set_response_filter(self): self.response._api_filter = filters.SampleFilters
Deferrable Task
- class django_declarative_apis.machinery.attributes.DeferrableEndpointTask(task_runner=None, delay=None, always_defer=True, task_args_factory=None, queue=None, routing_key=None, retries=0, retry_exception_filter=(), execute_unless=None, **kwargs)[source]
deferrable_taskis used as a decorator on a function. It is similar totaskin that it encapsulates side-effects, but can be automatically executed in a deferred queue outside of the request-response cycle.deferrable_taskruns asynchronously and because of that it is used for operations that take time and when we want to avoid delaying the response to the user.Deferrable Task Rules:
Deferrable task methods must always be a
staticmethod. Therefore, anything a deferrable task needs to know should be saved in theEndpointDefinition.resource().The
staticmethoddecorator should come afterdeferrable_taskdecorator.from django_declarative_apis.machinery import deferrable_task class SampleClass: # code @deferrable_task @staticmethod def sample_method(arg): # your code goes here
Works only with a Django Model instance as the resource
Note
Depending on the parameters used, a deferrable task can be run in different time intervals. In some cases, it can be made to run synchronously.
- Parameters:
task_runner (optional) – A callable that dictates how the task is executed. Defaults to
None.delay (optional) – Sets the delay in seconds before running the task. Requires
always_defer=True.Defaults toNone.always_defer (optional) – Runs task in deferred queue even when
delay=0.Defaults toFalse.task_args_factory (optional) – Stores task args and kwargs.
task_args_factorymust be a callable. Defaults toNone.queue (optional) – Sets the celery queue that will be used for storing the tasks. Defaults to
None.routing_key (optional) – It is used to determine which queue the task should be routed to. Defaults to
None.retries (optional) – Specifies the number of times the current task has been retried. Defaults to
0retry_exception_filter (optional) – It is used to store retry exception information that is used in logs. Defaults to
()- empty tuple.execute_unless (optional) – Execute the task unless a condition is met.It must be a callable. Defaults to
None.
Example
from django_declarative_apis.machinery import deferrable_task class SampleClass: # code @deferrable_task(execute_unless=<condition>) @staticmethod def sample_method(arg): # your code goes here