Skip to main content

qx-worker

NATS JetStream consumer runtime for Qx integration-event handlers. Pulls messages in configurable batches, deserialises them via EventRegistry, dispatches to handlers through the Mediator, and acks/naks based on the handler result.

What lives here

  • qx.worker.WorkerRuntime — the main consumer loop. Fetches batches concurrently, opens a per-message DI scope, sets RequestContext from the event envelope (correlation_id, tenant_id, trace context), calls mediator.consume_integration(), and acks on success or naks on failure (letting JetStream handle retry and DLQ semantics via max_deliver).

Usage

from qx.events import EventRegistry, NatsConsumer, NatsSettings, create_nats_connection
from qx.worker import WorkerRuntime
import asyncio, signal

async def main() -> None:
    nc = await create_nats_connection(settings.nats.url)
    consumer = NatsConsumer(nc, settings.nats)

    registry = EventRegistry()
    registry.register(UserRegisteredIntegration)

    worker = WorkerRuntime(
        container=container,
        consumer=consumer,
        registry=registry,
        mediator=mediator,
        concurrency=8,
    )

    loop = asyncio.get_running_loop()
    loop.add_signal_handler(signal.SIGTERM, lambda: asyncio.create_task(worker.stop()))

    await worker.run()

Integration event handler (registered via Mediator)

from qx.cqrs import integration_event_handler

@integration_event_handler(UserRegisteredIntegration)
class SendWelcomeEmailHandler:
    async def handle(self, event: UserRegisteredIntegration) -> Result[None]:
        await self._mailer.send_welcome(event.email, event.name)
        return Result.success(None)

Design rules

  • No in-process retry loop — the worker only decides ack vs nak. JetStream redelivers nak'd messages with exponential backoff up to max_deliver; after that, the message lands in the DLQ stream.
  • Concurrent processing — concurrency controls how many messages are processed in parallel per fetch() batch using asyncio.gather. Each message gets its own DI scope so shared state (sessions, connections) does not leak between concurrent handlers.
  • Observability — each message processing opens an OTel span linked to the incoming trace context extracted from NATS headers, so the full trace chain (HTTP → outbox relay → worker) is visible in your tracing backend.
  • Graceful shutdown — worker.stop() sets an internal asyncio.Event; the loop drains the current batch before exiting.

Metadata

Release files for qx-worker 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for qx-worker 1.1.0
File Size Uploaded Provenance Actions
qx_worker-1.1.0.tar.gz 12.0 kB Without provenance Details for qx_worker-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for qx-worker 1.1.0
File Interpreter ABI Platform Actions
qx_worker-1.1.0-py3-none-any.whl Python 3 none any Details for qx_worker-1.1.0-py3-none-any.whl

Total release size: 22.6 kB

Release files / qx_worker-1.1.0.tar.gz

Download URL qx_worker-1.1.0.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
54d226331add7ee7e2b34ae03f1091883bdd0df97fdb97b7cfadcecb54e3380e
BLAKE2b-256 checksum
How to use checksums
e353debe6a4e7d7ef825b20365687931e77abc073e9f896f8d1650581d845d71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / qx_worker-1.1.0-py3-none-any.whl

Download URL qx_worker-1.1.0-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6390db5361929f574ae148fe5c6a6ce624248e8c6e05b3f83b0181054d9a5da8
BLAKE2b-256 checksum
How to use checksums
588616e5ff46d7755f3850653ba128c93656120c2d0ab227a162f30daf856b0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Security for qx-worker 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page