Skip to main content

It's a plugin for Muffin framework which provides support for external APIs

Project description

Muffin-Donald is a plugin for the Muffin framework that provides support for asynchronous background tasks, workers, and scheduling.

Tests Status PYPI Version Python Versions

Contents

Features

  • ✅ Register async tasks

  • ✅ Run background workers

  • ✅ Schedule periodic tasks (cron or intervals)

  • ✅ RPC-style submit and wait for result

  • ✅ Muffin plugin integration with lifecycle management

Requirements

  • python >= 3.10

  • muffin >= 0.60.0

  • donald >= 0.1.0

Installation

Install via pip:

pip install muffin-donald

Usage

Initialize the plugin:

import muffin
from muffin_donald import Plugin

app = muffin.Application("example")

tasks = Plugin(app, backend="redis", backend_params={
    "url": "redis://localhost:6379/0"
}, start_worker=True, start_scheduler=True)

Register a task:

@tasks.task()
async def my_task(x, y):
    return x + y

Submit task for background execution:

my_task.submit(1, 2)

Submit and wait for result (RPC style):

result = await my_task.submit_and_wait(1, 2)
print("Result:", result)  # Result: 3

Schedule a periodic task:

@tasks.task()
async def periodic_task():
    print("Periodic task executed")

periodic_task.schedule("*/5 * * * *")  # every 5 minutes

Handle task errors with on_error:

@tasks.on_error
async def handle_error(exc):
    print("Task error:", exc)

Lifecycle hooks:

@tasks.on_start
async def startup():
    print("Tasks manager started")

@tasks.on_stop
async def shutdown():
    print("Tasks manager stopped")

Healthcheck command:

Muffin-Donald provides a CLI command for health checks:

muffin <app> tasks-healthcheck
  • Returns exit code 0 if healthy

  • Returns exit code 1 if unhealthy

Commands

Command

Description

tasks-worker

Run the worker process

tasks-scheduler

Run the scheduler

tasks-healthcheck

Check manager health

Configuration Options

You can configure the plugin via parameters or Muffin settings (with TASKS_ prefix):

Name

Default

Description

log_level

INFO

Logger level

log_config

None

Logger config

backend

memory

Backend: memory, redis, amqp

backend_params

{}

Backend connection params

worker_params

{}

Worker params

start_worker

False

Auto start a worker on startup

start_scheduler

False

Auto start a scheduler on startup

Example in Muffin settings:

TASKS_BACKEND = "redis"
TASKS_BACKEND_PARAMS = {"url": "redis://localhost:6379/0"}
TASKS_START_WORKER = True
TASKS_START_SCHEDULER = True

Testing

Example using manage_lifespan:

import pytest
from asgi_tools.tests import manage_lifespan

async def test_tasks(app, tasks):
    async with manage_lifespan(app):
        result = await my_task.submit_and_wait(1, 2)
        assert result == 3

Bug Tracker

Please report issues or suggestions at https://github.com/klen/muffin-donald/issues

Contributing

Development happens at: https://github.com/klen/muffin-donald

Contributors

  • klen (Kirill Klenov)

License

Licensed under the MIT license.

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

muffin_donald-1.0.3.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

muffin_donald-1.0.3-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file muffin_donald-1.0.3.tar.gz.

File metadata

  • Download URL: muffin_donald-1.0.3.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for muffin_donald-1.0.3.tar.gz
Algorithm Hash digest
SHA256 ffa3b92b149cb685db0930262b103a0afacf320014f47734975597e365ffd775
MD5 b1f29b28092dade5c2c68ef965dc722c
BLAKE2b-256 f4ee972d8809ef501d6e526362615153be39852b147ad33183c69bec5cc533a2

See more details on using hashes here.

File details

Details for the file muffin_donald-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: muffin_donald-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for muffin_donald-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1ccf294e0397d67ac0c27feb8936efa9e37b778c8c67ea1a80c2f60b884fedef
MD5 c8f67271bd3a6002e528a9fc42067ff8
BLAKE2b-256 0b0c0107d3f1215865e806d28d901f90c9b959517e1a2d903070ae6916c2779a

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