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.

install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
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"
manifest: SkillBank/ConvSkill/english_gpt4_8_GLM4.7/fastapi-oop-background-task-management-system/SKILL.md
source content

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

  1. Architecture: Use an OOP design with a Base Task class and a TaskManager.
  2. Database: Use SQLAlchemy with SQLite for persistence. Define a
    BackgroundTaskModel
    to store task ID, name, status, start time, end time, and results.
  3. Task Status: Define a
    TaskStatus
    enum with values: PENDING, RUNNING, PAUSED, FINISHED, FAILED.
  4. 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.
  5. 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.
  6. CRUD Functions: Implement specific CRUD functions to create tasks, update status/results, and fetch tasks from the database.
  7. Simplicity: Avoid external message brokers (Celery/RQ) unless explicitly requested. Use FastAPI's native capabilities or threading for local execution.
  8. 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

  1. Define the SQLAlchemy model and Pydantic schemas.
  2. Implement the CRUD operations.
  3. Define the abstract Base Task class.
  4. Implement the TaskManager with startup loading logic.
  5. 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