Skip to content

API Reference parrot.handlers.render_jobs

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.handlers.render_jobs

View source

Redis-backed job store for async infographic renders (FEAT-327, Module 4).

async=true on POST /api/v1/agents/infographic/render turns a render into a background job: 202 {job_id} immediately, with the actual render running as an asyncio task and its outcome polled via GET /api/v1/agents/infographic/render/jobs/{job_id}.

All job state lives in Redis (never process memory) so polling works regardless of which worker created or rendered the job (resolved decision: multi-worker safety). Terminal jobs (done/failed) carry a 1-day TTL; non-terminal jobs (pending/running) carry no TTL — an orphaned pending OR running job (worker died before or during the render) is instead caught by the max-runtime watchdog: every job stores a deadline timestamp from the moment it is created, and :meth:RenderJobStore.get flips a past-deadline pending/running job to failed at poll time — no background daemon.

set_terminal is a best-effort guard against a job's own terminal write racing the watchdog's: it refuses to overwrite an ALREADY-terminal record with a different terminal status (e.g. a slow render completing just after the watchdog already flipped it to failed). This is a plain read-then-write check (not a Redis transaction/Lua CAS), so it narrows but does not fully close the race under truly concurrent multi-process writers — see the method docstring.

This is NEW code — there is no generic KV/job store in parrot.memory (only ConversationMemory subclasses); :class:RenderJobStore follows the SAME Redis client-construction pattern as :class:~parrot.memory.redis.RedisConversation but is not a subclass of it.

Attribute DEFAULT_MAX_RUNTIME_SECONDS

Class RenderJobStore

Redis-backed store for :class:RenderJob records.

Multi-worker safe: ALL state lives in Redis (or an injected Redis-shaped client, e.g. a test double) — no process-memory job state.

Parameter Type Default Description
redis_client Optional[Any] None An existing async Redis-shaped client to use (injected for tests). When omitted, a real client is built exactly like :class:~parrot.memory.redis.RedisConversation.
redis_url Optional[str] None Overrides REDIS_HISTORY_URL when building a new client (ignored when redis_client is given).

Method RenderJobStore.create

Create a new job record — no TTL, but a real watchdog deadline for pending.

When job.status == "pending" (the normal case — every job starts pending via _enqueue_render_job), the deadline is stamped from creation time, NOT left at whatever placeholder the caller passed in — so a job whose asyncio task never runs at all (e.g. the worker died, or the task was garbage-collected before its first await) is STILL recovered by the watchdog instead of sitting pending forever with no TTL. set_running stamps a FRESH deadline once the render actually starts, giving it the full runtime budget from that point rather than whatever was left of the enqueue-time one.

For any OTHER status, the caller's job.deadline is stored as-is — this lets tests (and any direct-construction caller) seed a job already in a specific state/deadline without this method silently overriding it.

create(job: RenderJob, *, max_runtime_seconds: Optional[int] = None) -> None

Parameter Type Default Description
job RenderJob The job record to store.
max_runtime_seconds Optional[int] None Overrides :func:resolve_max_runtime_seconds (only consulted when job.status == "pending").

Method RenderJobStore.get

Return the job record, applying the watchdog check first.

A pending OR running job whose deadline has passed is flipped to failed (with a structured watchdog error) and persisted with the terminal TTL before being returned — this is how an orphaned job (its worker died before starting, or mid-render) is recovered, without a background daemon (resolved decision: poll-time check only).

get(job_id: str) -> Optional[RenderJob]

Parameter Type Default Description
job_id str The job identifier.

Returns: Optional[RenderJob] — The job record, or None when unknown/expired (the route maps this to 404).

Method RenderJobStore.set_running

Transition a job to running, stamping a FRESH watchdog deadline.

set_running(job_id: str, *, max_runtime_seconds: Optional[int] = None) -> RenderJob

Parameter Type Default Description
job_id str The job identifier.
max_runtime_seconds Optional[int] None Overrides :func:resolve_max_runtime_seconds.

Returns: RenderJob — The updated job record.

Raises:

  • KeyError — The job does not exist.

Method RenderJobStore.set_terminal

Persist a job in a terminal state (done/failed) with the TTL.

Best-effort guard against a job's OWN terminal write racing the watchdog's: if the record already holds a DIFFERENT terminal status (e.g. the watchdog already flipped it to failed while this call's render was still finishing), the existing terminal record wins and this write is skipped (logged) — a completed-late render must never silently resurrect a job the watchdog already closed out. This is a plain GET then SET (no Redis transaction/Lua CAS), so it narrows the race window considerably for the common single-writer-per-job case but is NOT a fully atomic guarantee under truly concurrent multi-process writers to the SAME job id — pass force=True to bypass the guard when the caller has already established it is the sole writer.

set_terminal(job: RenderJob, *, force: bool = False) -> RenderJob

Parameter Type Default Description
job RenderJob The job record, with status already set to done or failed.
force bool False Skip the already-terminal guard and overwrite unconditionally.

Returns: RenderJob — The record that ended up persisted (job normally; the EXISTING record when the guard refused the overwrite).

Attribute TERMINAL_JOB_TTL_SECONDS

Function resolve_max_runtime_seconds

Return the max-runtime (seconds) before the watchdog flips a job.

v1: a constant. Kept behind this single resolver function so a future resource-aware computation can replace it without changing :class:RenderJobStore's API (resolved decision).

resolve_max_runtime_seconds() -> int

Returns: int — The max-runtime, in seconds.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally