-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
52 lines (41 loc) · 1.46 KB
/
main.py
File metadata and controls
52 lines (41 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import threading
from contextlib import asynccontextmanager
from fastapi import FastAPI
from app.api.jobs import router as jobs_router, queue
from app.core.worker import run_worker
from app.core.database import create_db_and_tables
# Global event to signal the worker thread to stop gracefully
stop_event = threading.Event()
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Lifespan context manager for the FastAPI application.
Handles system startup (DB init, Worker start) and shutdown.
"""
# --- Startup Phase ---
# 1. Initialize the database and create tables if they don't exist
create_db_and_tables()
print("📁 Database and tables created!")
# 2. Start the background worker thread
# The worker runs in a separate thread but shares the same memory space (queue)
worker_thread = threading.Thread(
target=run_worker,
args=(queue, stop_event),
daemon=True
)
worker_thread.start()
yield # The application handles requests during this phase
# --- Shutdown Phase ---
print("🛑 Shutting down worker...")
# Signal the worker to stop and wait for it to finish cleanly
stop_event.set()
worker_thread.join()
# Initialize FastAPI app
app = FastAPI(
title="Distributed Task Queue",
description="A robust async task queue system using FastAPI, SQLite, and threading.",
version="1.0.0",
lifespan=lifespan
)
# Register the API router
app.include_router(jobs_router)