Celery

Viewflow uses Celery to run workflow steps in the background. Two nodes are available in viewflow.contrib.celery:

  • celery.Job runs a step as a Celery task. The workflow continues when the task finishes. If the task raises an exception, the workflow task is marked as an error, and a flow manager can revive it from the workflow UI. Celery’s own Retry and Reject are passed through, so Celery can deliver the task again. Use .Eta(callable) to start the job at a given time.
  • celery.Timer waits for a time interval, then moves the process to the next node.

Usage

from celery import shared_task
from viewflow.contrib import celery

class MyFlow(Flow):
    ...

    perform = celery.Job(this.perform_task)

    @staticmethod
    @shared_task
    def perform_task(cls, activation_ref:str) -> None:
        with celery.Job.activate(activation_ref) as activation:
            print(activation.process.message)

            # Beware of race conditions. Unlike user tasks, for
            # long-running Celery jobs, Viewflow does not set up a lock.
            activation.process.sent_at = datetime.now()
            activation.process.save(update_fields="sent_at")

            # You can also set the lock manually for a short time
            with activation.flow_class.lock(activation.process.pk):
                activation.process.refresh_from_db()
                activation.process.sent_at = datetime.now()
                activation.process.save()

See also

see the Job

See also

see the Timer