Skip to main content
Dramatiq logo

Dramatiq

v1.13.0 Apache-2.0

z4j-dramatiq ยท Upstream project

Middleware-driven Dramatiq observability.

Ships as a single Z4JMiddleware with no monkey-patching. Supports Redis and RabbitMQ. Pending-task cancellation is promoted only with z4j-dramatiq[abort] and a real dramatiq-abort Abortable middleware; z4j never interrupts running Dramatiq work.

Latest
v1.13.0
Released
2026-10-19
License
Apache-2.0

Install

bash
pip install z4j-dramatiq

Native actions

  • submit_task
  • retry_task (complete replacement inputs)
  • purge_queue
  • cancel_task (pending only; requires [abort] and real Abortable middleware)
  • list_dead_letters (Redis lists the entries; RabbitMQ reports the count only)

Marked entries are not in the adapter's default capability set. The adapter advertises them only when the stated condition holds (an installed extra and its middleware, or a supported broker), so the control does not appear otherwise.

Not supported

  • running-task cancellation
  • bulk_retry
  • requeue_dead_letter
  • restart_worker
  • rate_limit

These are engine-level limitations: the underlying engine does not expose the primitives required to support these actions.

Wiring

How z4j plugs in

Event capture

Z4JMiddleware, Dramatiq's blessed observability hook.

Reconciliation

Reports unknown; the brain's event-derived snapshot remains authoritative.

Install

Add z4j to a Dramatiq app

z4j itself is one container or one Python process. The agent is a pip package in your app's environment that opens one outbound connection to it.

1. Run z4j

bash
pip install z4j && z4j serve

First boot prints a setup URL to stderr. Open it, create the first admin, and mint a token and HMAC secret for the agent. Docker and PostgreSQL setups are in the install guide.

Django + Dramatiq

2. Install the agent

bash
pip install z4j-django[dramatiq]

3. Wire it into Django

python
# settings.py
INSTALLED_APPS += ["z4j_django"]

Z4J = {
    "brain_url":   env("Z4J_BRAIN_URL"),
    "token":       env("Z4J_TOKEN"),
    "hmac_secret": env("Z4J_HMAC_SECRET"),
    "project_id":  env("Z4J_PROJECT_ID"),
    "agent_name":  env("Z4J_AGENT_NAME", default=None),
}
# z4j-django discovers only Celery in the web process; the Dramatiq
# worker process needs its own hook (see below).

Task lifecycle events come from the Dramatiq worker process, which needs the z4j-dramatiq worker hook. Full guide: Django integration and Dramatiq engine docs.

Flask + Dramatiq

z4j-flask handles registration in the web process; z4j-dramatiq instruments the broker layer (Redis or RabbitMQ) without changing your actor definitions.

2. Install the agent

bash
pip install z4j-flask[dramatiq]

3. Wire it into Flask

python
from flask import Flask
from z4j_flask import Z4J

app = Flask(__name__)
app.config["DRAMATIQ_BROKER"] = "myapp.broker:broker"
z4j = Z4J(app)  # reads Z4J_* from the environment or app.config["Z4J_*"]

Task lifecycle events come from the Dramatiq worker process, which needs the z4j-dramatiq worker hook. Full guide: Flask integration and Dramatiq engine docs.

FastAPI + Dramatiq

2. Install the agent

bash
pip install z4j-fastapi[dramatiq]

3. Wire it into FastAPI

python
from myapp.broker import broker  # your Dramatiq broker
from fastapi import FastAPI
from z4j_fastapi import z4j_lifespan

# The engine handle registers the Dramatiq adapter in the web process.
app = FastAPI(
    lifespan=z4j_lifespan(
        dramatiq_broker=broker,
    ),
)

Task lifecycle events come from the Dramatiq worker process, which needs the z4j-dramatiq worker hook. Full guide: FastAPI integration and Dramatiq engine docs.

Dramatiq in a plain Python worker

2. Install the agent

bash
pip install z4j-bare z4j-dramatiq

3. Start it at worker boot

python
# The module that sets up your broker and actors
from dramatiq import get_broker
from z4j_bare import install_agent
from z4j_dramatiq import DramatiqEngineAdapter

# install_agent connects the z4j middleware; do not call
# connect_signals() yourself.
runtime = install_agent(engines=[DramatiqEngineAdapter(broker=get_broker())])

Full guide: Plain Python integration and Dramatiq engine docs.

Scheduler

APScheduler with z4j

Dashboard management for the APScheduler 3 jobstore.

APScheduler pairs with Dramatiq through the dramatiq extras. Creates, edits, pauses and resumes native APScheduler jobs. Manual runs use its executor without moving the repeating schedule. Registered callables define the allowed task choices.

What z4j can do with APScheduler

  • List schedules
  • Create schedule
  • Update schedule
  • Delete schedule
  • Enable / disable
  • Trigger-now

Limitations

  • Requires matching z4j package versions and a running APScheduler 3 instance
  • Use a persistent jobstore for restart persistence
  • Do not share an APScheduler 3 jobstore between scheduler processes

Package z4j-apscheduler. Reference: APScheduler docs. For schedules edited from the dashboard on any engine, see z4j-scheduler; every option side by side is on the schedulers overview.

Included

What you get on top of Dramatiq

  • Persistent Dramatiq task history in PostgreSQL or SQLite
  • Task controls the adapter advertises, and nothing it cannot execute safely
  • Issues: repeated failures grouped by fingerprint
  • RBAC with invitations and password reset
  • HMAC-chained audit log of operator actions
  • Notifications: email, Slack, Telegram, webhook
License

The agent packages (z4j-dramatiq and the framework adapters) are Apache 2.0. Importing them into your app does not affect its licensing. z4j itself is AGPL-3.0-or-later and runs in its own process.

More engines

Other z4j engines

Try z4j with Dramatiq