background-task

Implement and debug background work using Prefect flows and in-process task spawning.

49|11|Updated Jul 31, 2026
One-click install
npx skills add https://github.com/vstorm-co/agenticos --skill background-task-vstorm-co
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: background-task
Source: https://github.com/vstorm-co/agenticos/tree/main/.claude/skills/background-task
Command: npx skills add https://github.com/vstorm-co/agenticos --skill background-task-vstorm-co

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires prefect.

What problem does it solve? Work that runs outside the request/response cycle — document ingestion, MCP refresh, reports, scheduled jobs — often fails silently: tasks get garbage-collected, flows cannot see uncommitted rows, and exceptions vanish with nothing in the logs. This Skill guides you to the right execution mechanism and prevents those exact failure modes. ## Core Features & Use Cases - Mechanism Selection: A decision table for choosing between Prefect flows (durable, retryable, scheduled), spawn() / spawn_after_commit() from app/core/background.py (in-process handoffs), and inline execution. - Failure-Mode Prevention: Explains why bare asyncio.create_task drops work silently, why flows reading their own row need spawn_after_commit, and why CLI commands must call drain() before returning. - Flow Authoring Rules: Conventions for defining flows in app/worker/tasks/, dispatching from services, registering CronSchedule/IntervalSchedule deployments, and enforcing serializable args, idempotency, and cost recording on failure. - Use Case: An upload endpoint returns {"status": "processing"} but ingestion never runs. This Skill diagnoses it as the spawn-before-commit race and shows the spawn_after_commit fix. ## Quick Start Ask the agent to add a new scheduled background flow for report generation following the background-task conventions.

Frequently Asked Questions about background-task

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add a background task in FastAPI with Prefect?▼

Define an async @flow function in app/worker/tasks/<area>.py, dispatch it from a service at the end of the unit of work, and register a CronSchedule or IntervalSchedule deployment in app/worker/prefect_app.py for periodic runs.

Why does asyncio.create_task silently drop my background work?▼

The event loop holds the task only weakly, so a dropped reference lets it be garbage-collected mid-flight, and exceptions in discarded tasks are never retrieved. Use the spawn helper in app/core/background.py, which holds a strong reference and logs failures.

Prefect flow vs in-process background task: which should I use?▼

Use a Prefect flow for ingestion, syncs, reports, and anything scheduled or retryable that must survive a restart. Use spawn() or spawn_after_commit() only for the in-process handoff between answering a request and finishing the slow part.

Why can't my background flow read the row the request just created?▼

spawn starts the task before the database commit, so the flow's fresh session cannot see the uncommitted row. Use spawn_after_commit, which queues the coroutine on the session and starts it after commit() succeeds.

What arguments can I pass to a Prefect flow?▼

Only serializable arguments such as ids and primitives — never ORM objects or database sessions. Re-fetch entities inside the flow with a fresh session, and pass the organization id explicitly since workers have no ambient tenant.