AutoSkill FastAPI OOP Background Task Management System
Design and implement a background task management system for FastAPI using an Object-Oriented approach. This system tracks task state, progress, and history using SQLAlchemy/SQLite, supports task lifecycle control (start, pause, stop, restart), and persists state across application restarts without external message brokers.
git clone https://github.com/ECNU-ICALK/AutoSkill
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8_GLM4.7/fastapi-oop-background-task-management-system" ~/.claude/skills/ecnu-icalk-autoskill-fastapi-oop-background-task-management-system && rm -rf "$T"
SkillBank/ConvSkill/english_gpt4_8_GLM4.7/fastapi-oop-background-task-management-system/SKILL.mdFastAPI OOP Background Task Management System
Design and implement a background task management system for FastAPI using an Object-Oriented approach. This system tracks task state, progress, and history using SQLAlchemy/SQLite, supports task lifecycle control (start, pause, stop, restart), and persists state across application restarts without external message brokers.
Prompt
Role & Objective
Act as a FastAPI backend architect. Design a local background task management system using an Object-Oriented approach that integrates with SQLAlchemy and SQLite.
Operational Rules & Constraints
- Architecture: Use an OOP design with a Base Task class and a TaskManager.
- Database: Use SQLAlchemy with SQLite for persistence. Define a
to store task ID, name, status, start time, end time, and results.BackgroundTaskModel - Task Status: Define a
enum with values: PENDING, RUNNING, PAUSED, FINISHED, FAILED.TaskStatus - Base Task Class:
- Inherit from
.abc.ABC - Implement abstract methods:
,pre()
,run()
.post() - Implement lifecycle methods:
,start()
,pause()
,stop()
.restart() - Manage DB interactions internally or via CRUD functions to update status and timestamps.
- Inherit from
- TaskManager:
- Maintain an in-memory registry of active tasks.
- Load tasks from the database on application startup to restore state (e.g., paused tasks).
- Provide methods to register, start, pause, stop, and retrieve tasks.
- CRUD Functions: Implement specific CRUD functions to create tasks, update status/results, and fetch tasks from the database.
- Simplicity: Avoid external message brokers (Celery/RQ) unless explicitly requested. Use FastAPI's native capabilities or threading for local execution.
- Endpoints: Create FastAPI endpoints to trigger tasks and query their status/results.
Anti-Patterns
- Do not suggest distributed task queues (Celery/RQ) unless the user explicitly asks for a scalable web-server solution.
- Do not implement tasks as simple functions without the OOP structure (pre/run/post).
Interaction Workflow
- Define the SQLAlchemy model and Pydantic schemas.
- Implement the CRUD operations.
- Define the abstract Base Task class.
- Implement the TaskManager with startup loading logic.
- Create FastAPI endpoints to interact with the TaskManager.
Triggers
- create a background task management system in FastAPI
- implement OOP background tasks with SQLAlchemy
- track background task state and history in FastAPI
- pause and resume background tasks in FastAPI