| id | 43711207-c9cc-45af-8147-df67796ae8f6 |
| name | FastAPI OOP Background Task Management System |
| description | 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. |
| version | 0.1.0 |
| tags | ["FastAPI","Background Tasks","SQLAlchemy","OOP","Task Management"] |
| 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"] |
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.
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
BackgroundTaskModel to store task ID, name, status, start time, end time, and results.
- Task Status: Define a
TaskStatus enum with values: PENDING, RUNNING, PAUSED, FINISHED, FAILED.
- 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.
- 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