Skip to main content

A Redis backed job runner

Project description

Not Unother Task Scheduler

It's another Redis backed task scheduler. Supports queueable and cronable jobs and light DAG implementation for when you need to chain jobs together.

Installation

Nuts is available as a published Python package for versions >=3.8.

pip install nuts-scheduler

Worker Setup

The main functionality of NUTS is the Worker class. It accepts a list of jobs and manages their scheduling, logging and exectution. An example project using NUTS might have the structure.

/
-- jobs/
    -- a_job.py
    -- b_job.py
main.py

An example main.py might look like

from nuts import Worker
from .jobs import a_job, b_job
from redis import Redis

r = Redis()

worker = Worker(redis=r, jobs=[a_job, b_job])

while worker.should_run:
    worker.run()

You provide the list of jobs (discussed in the next section), a redis connection and you're off to the races. If you are running multiple workers connected to the same worker, NUTS will automatically handle leadership assignment for scheduling management.

You may pass in an arbitrary kwargs, it is suggested to use these to provide your worker with any shared functionality that your jobs may need (database connections, etc).

Jobs

NUTS workers run NutsJobs. You can create a simple job as

from nuts import NutsJob

class Job(NutsJob):

    def __init__(self, args, **kwargs):
        super().__init__()
        self.name = 'MyFirstJob'  # Required
        self.schedule = ''  # A 7 position cron statement, optional

    def run(self, job_args, **kwargs):


        self.result = job_args[0] + job_args[1]
        self.success = True
        return

Your job files should all contain a class named Job which extends NutsJob. Name is a required property for the worker state management. A schedule is optional. When provided, the Worker will run the job on at the specified frequency. All jobs should implement a run method which takes your job arguements as parameters and optional kwargs which will be provided by the worker. These kwargs are suggested as a way to pass in common functions or data source connections from your worker to reduce the amount of initializations you need to do in code.

Setting the result attribute at the completion of your job is optional, but improves the default logging for better traces, and allows you to use the DAG functionality that NUTS implements.

Setting success on completion of your job is required.

Chaining Jobs - DAG

NUTS supports a very basic directed acyclic graph style functionality. When defining your job, setting the next attribute of your class to the name of the next job you would like to run will tell the worker to enqueue that job with the data stored on your jobs result attribute as parameters. This is useful for breaking up functionality into logical components, or break up long processes into more controllable steps.

Workflows

NUTS supports complex workflows (DAGs) through YAML configuration files. Workflows allow you to define multi-job pipelines with dependencies, scheduling, and automatic error handling.

Creating a Workflow

Create a YAML file defining your workflow:

# data_pipeline.yaml
workflow:
  name: data-pipeline
  schedule: "0 0 2 ? * * *"  # Daily at 2 AM
  jobs:
    - name: ExtractData
      requires: null  # Root job - no dependencies
    - name: TransformData
      requires:
        - ExtractData  # Waits for ExtractData to complete
    - name: LoadData
      requires:
        - TransformData
    - name: SendNotification
      requires:
        - LoadData

Loading Workflows

Pass a directory containing workflow YAML files to the Worker:

from nuts import Worker
from redis import Redis
from .jobs import extract_data, transform_data, load_data, send_notification

r = Redis()

worker = Worker(
    redis=r,
    jobs=[extract_data, transform_data, load_data, send_notification],
    workflow_directory='./workflows'  # Directory containing .yaml files
)

while worker.should_run:
    worker.run()

Workflow Features

  • Dependency Management: Jobs automatically wait for their dependencies to complete
  • Parallel Execution: Jobs without dependencies can run in parallel
  • Error Handling: If any job fails, the workflow stops and marks as failed
  • Automatic Rescheduling: Workflows reschedule automatically based on their cron schedule
  • State Persistence: Worker failures don't lose workflow progress (state saved to Redis)

Workflow Job Requirements

Jobs used in workflows must be registered with the Worker and follow the standard NutsJob pattern:

from nuts import NutsJob

class Job(NutsJob):
    def __init__(self):
        super().__init__()
        self.name = 'ExtractData'  # Must match workflow YAML

    def run(self, **kwargs):
        # Job logic here
        self.result = {'data': 'extracted'}
        self.success = True

Important: Job names in the workflow YAML must exactly match the name attribute of your job classes.

Workflow Validation

Workflows are validated on load to catch common errors:

  • Circular dependencies (job A requires B, B requires A)
  • Missing job definitions (workflow references jobs not registered with Worker)
  • Orphaned jobs (no path from root jobs to job)

Invalid workflows are logged and skipped.

Example: Data Pipeline

See the examples/ directory for a complete data pipeline workflow implementation including:

  • ExtractData: Fetches data from an API
  • TransformData: Cleans and processes data
  • LoadData: Loads data to warehouse
  • SendNotification: Sends completion notification

This demonstrates a common ETL pattern with sequential dependencies.

Workflow vs DAG Jobs

NUTS supports two ways to chain jobs:

  1. Workflows (YAML): Best for complex pipelines, scheduled operations, and when you need visual workflow definitions
  2. DAG Jobs (job.next): Best for simple linear chains and dynamic job chaining based on results

Workflows are recommended for most use cases as they provide better visibility, validation, and error handling.

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

nuts_scheduler-0.4.0.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

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

nuts_scheduler-0.4.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file nuts_scheduler-0.4.0.tar.gz.

File metadata

  • Download URL: nuts_scheduler-0.4.0.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for nuts_scheduler-0.4.0.tar.gz
Algorithm Hash digest
SHA256 42345f5dd27405cbabf709873cae378398624ffd9ba496c1279a3fb2866140e6
MD5 8a08b283d7d5699ce94bfd9797bfa38f
BLAKE2b-256 02b90f9ec65cacc0840451d8d5e63d53849d6be51d67a3afa4de741da0f1a22b

See more details on using hashes here.

File details

Details for the file nuts_scheduler-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: nuts_scheduler-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for nuts_scheduler-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a1b56209214223466a71c6b5600167678cb69d82d0a35fe32024eb82f6b6f06e
MD5 3528ef52c5c5b130ed6f6655dbccf08b
BLAKE2b-256 7c6e1637c7cc3303ae73335874127efd73d2037541d62acefb235bd28714ca8a

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