Skip to main content

Create and manage workflows using Celery tasks

Project description

Rhythm

Rhythm allows you to design and control workflows made of Celery tasks. A workflow is a sequence of steps to run one after the other. Rhythm simplifies the process of executing workflows consisting of long-running tasks with reliability.

The following are the features of Rhythm workflows:

  • If a workflow consisting of three steps (S1, S2, and S3) encounters a failure while executing S2 (even after retries by Celery), it is possible to resume the workflow later. Resuming the workflow with restart S2 with previous arguments and after its completion S3 will be run.
  • A workflow can be paused and resumed later.
  • You can keep track of which step is currently running, as well as its progress.

Installation

pip install sca-rhythm

see on pypi

Prerequisites

Celery app should be configured with a mongo database backend.

Create Tasks with WorkflowTask class

import os
import time

from celery import Celery

from sca_rhythm import WorkflowTask

app = Celery("tasks")


@app.task(base=WorkflowTask, bind=True)
def task1(self, batch_id, **kwargs):
    print(f'task - {os.getpid()} 1 starts with {batch_id}')
    # do work
    time.sleep(1)

    # update progress to result backend
    # sets the task's state as "PROGRESS"
    self.update_progress({
        'done': 2873,
        'total': 100000
    })

    # do some more work
    return batch_id, {'return_obj': 'foo'}

:warning: Task Constraints :warning:

  1. The task signature must contain **kwargs for the workflow orchestration to function.
  2. The return type must be of list / tuple type and the first element of the return value is sent to the next task as its argument.

Create Workflows with Workflow class

from celery import Celery

from sca_rhythm import Workflow

steps = [
    {
        'name': 'inspect',
        'task': 'tasks.inspect'
    },
    {
        'name': 'archive',
        'task': 'tasks.archive'
    },
    {
        'name': 'stage',
        'task': 'tasks.stage'
    }
]

wf = Workflow(app, steps=steps, name='archive_batch')
wf.start('batch-id-test')

Pause and Resume Workflows

Pausing a workflow stop the current running task and resuming a workflow will restart the stopped task with the same arguments.

wf = Workflow(app, workflow_id='2f87decb-a431-472b-b26e-32c894993881')

wf.pause()

wf.resume()

Build & Publish

poetry install
poetry publish --build

Task Status

  • PENDING: Task state is unknown (assumed pending since you know the id).
  • STARTED: Task was started by a worker (task_track_started = True)
  • SUCCESS: Task succeeded
  • FAILURE: Task failed
  • REVOKED: Task was revoked
  • RETRY: Task is waiting for retry.

Workflow Status

The workflow status is a summative status that is determined by the status of the initial step that is not marked as " SUCCESS," which is referred to as a "pending step".

  • PENDING - the pending step is the first step in the workflow and its status is pending.
  • STARTED - the status of the pending step is one of STARTED, RETRY, PENDING.
  • REVOKED - the pending step was revoked, the workflow can be resumed.
  • FAILURE - the pending step was failed, the workflow can be resumed.
  • SUCCESS - all steps have succeeded.

Status Groups:

  • DONE: { SUCCESS, FAILURE, REVOKED }
  • ACTIVE: !DONE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sca_rhythm-0.6.8.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sca_rhythm-0.6.8-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file sca_rhythm-0.6.8.tar.gz.

File metadata

  • Download URL: sca_rhythm-0.6.8.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.10 Darwin/23.0.0

File hashes

Hashes for sca_rhythm-0.6.8.tar.gz
Algorithm Hash digest
SHA256 d889c6bbc41de05d0059759a8fb89802609602bc459cbd0eaae565372369d267
MD5 8b4c0e41c15387d2152c3f2b7ca75817
BLAKE2b-256 16abe7f001d43155cccd25c3d7daa6c340c9e7693422e731304891650d6c2e1e

See more details on using hashes here.

File details

Details for the file sca_rhythm-0.6.8-py3-none-any.whl.

File metadata

  • Download URL: sca_rhythm-0.6.8-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.10 Darwin/23.0.0

File hashes

Hashes for sca_rhythm-0.6.8-py3-none-any.whl
Algorithm Hash digest
SHA256 340961c2efa7edd3988aee8e6792ca53629ed0b96a96d37213e68b1a92178e02
MD5 e339d36258b0828dff1865ecb2d452a2
BLAKE2b-256 07bfd0b52e4a1c2c19b7f9ad8c07e4e8ed6cbee41be45fd09baa77c193adf1f5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page