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.From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill fastapi-oop-background-task-management-systemAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
3.3 KB, 555 tokens by cl100k_base, as published. Nobody here has run it
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
BackgroundTaskModelto store task ID, name, status, start time, end time, and results. - Task Status: Define a
TaskStatusenum 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.
- 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
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.