Queues
Titancore delegates durable jobs to Taskiq Redis Streams. Register
ConcurrencyServiceProvider() and QueueServiceProvider(MyJob) in both
producer and worker applications. Job dependencies resolve through the
existing container in the worker's own process.
from titancore.facades import Queue
from titancore.queue import Job
class MyJob(Job):
async def handle(self, payload: dict[str, object]) -> object:
return {"processed": payload["id"]}
receipt = await Queue.dispatch(MyJob, {"id": 1})
result = await receipt.wait(seconds=30)
# Use a saved receipt ID to inspect a job from the same queue.
receipt = await Queue.result(receipt.id)
if await receipt.is_ready():
result = await receipt.wait()
Configure config/queue.yaml with an application-specific namespace:
url: !env [REDIS_URL, "redis://localhost:6379/0"]
name: my-application:queue
concurrency: 8
timeout: 60
retries: 3
backoff: 1.0
result_ttl: 3600
Only registered classes execute. Payloads and results must be JSON-compatible,
finite, and at most 64 KiB. No pickle or message-controlled importing is used.
Results retain safe status and bounded values. They exclude logs, labels, and
exception details. JobFailedError reports terminal failure. is_ready()
returns false for pending, unknown, or expired results. A job ID does not grant
access to its result. Apply application authorization before looking up an ID.
Use a version field in payloads that cross deployments. A worker must be able to read every payload version in its queue. Keep Redis write access limited to trusted producers and workers. The queue does not provide a public input API.
The Console kernel adds queue:work when a queue provider is registered.
Run it in an application-owned worker process. Native signal handlers drain
work on SIGINT and SIGTERM, returning the conventional signal exit status.
Code embedding a worker can await manager.work(stop_event) instead.
Application shutdown requests worker stop before closing its broker pools.
Taskiq owns retries, acknowledgements, and pending-message recovery. Set
queue.retries to the total attempt limit, including the initial attempt. A
value of 1 disables retries. queue.backoff sets the first retry delay. Later
delays increase by that interval, up to 60 seconds. The worker holds its slot
and leaves the delivery unacknowledged until it publishes the retry with the
same job ID. No separate scheduler is necessary.
The queue acknowledges a successful or terminal delivery after saving its result. A result-storage failure stops the worker before acknowledgement. After restart, a worker can reclaim the pending job even if no new jobs arrive. A crash between an application side effect and acknowledgement can cause the job to execute again. Jobs must be idempotent.
queue.redelivery_ms controls pending-entry idle time. The default allows for
prefetch, execution, and retry delays. If you override it, allow time for both
execution and waiting in the worker. Sync work already in a thread cannot be
forcibly stopped by an asyncio deadline. CPU-intensive jobs may inject Parallel.
Process jobs construct their own resources.
dispatch_after_commit(session, JobClass, payload) copies the payload and
publishes only after the PostgreSQL transaction commits. A rollback publishes
nothing. This does not provide atomic database-and-broker publication. Use an
application outbox when you need that guarantee.
A worker makes one Redis delivery request per available slot. It accepts up to two deliveries per configured concurrency slot, including prefetch. The Redis operator controls total backlog, stream retention, persistence, and memory. The result expiry does not remove entries from the stream. Do not trim entries that a consumer still needs to acknowledge.
Native claim locks expire after twice the maximum worker drain interval. An upgrade does not add an expiry to locks left by an older worker. Do not remove a claim lock while workers run.
Integration tests check that a two-slot worker holds at most four pending entries with 32 jobs in the stream. A second test kills a worker and a native claimant, then requires the restarted worker to finish the recovered job and a 24-job backlog within the 15-second result wait. These are local regression criteria, not a production throughput or recovery guarantee. Measure capacity with your payload sizes, arrival rate, and Redis persistence settings.
The queue provides no exactly-once delivery, scheduler, distributed batch API, or administration dashboard.
Titancore uses Taskiq 0.12 and taskiq-redis 1.2.2. This adapter can claim pending entries when no new jobs arrive and supports a finite claim lease. The later adapter skips pending claims when the new-message read is empty.
Backend choice¶
The Taskiq Redis adapter provides a Redis Streams consumer group and a native async worker boundary. Titancore uses these APIs directly for delivery, recovery, and results. Celery's Redis transport uses a visibility timeout for unacknowledged messages. Dramatiq has a Redis broker and an AsyncIO middleware for coroutines. Replacing Taskiq with one of these alternatives would change the worker and recovery model.