Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ async def lifespan(app: FastAPI):
db = await init_db(db_path)
app.state.db = db

# Startup recovery: reset any images stuck in 'processing' from a previous
# unclean shutdown. Without this, a container restart leaves those images
# permanently blocked and the worker queue stalls indefinitely.
async with db.execute(
"UPDATE images SET status = 'pending', in_queue = 0 WHERE status = 'processing'"
) as cursor:
recovered = cursor.rowcount
if recovered:
await db.commit()
logger.info(
"Startup recovery: reset %d stuck 'processing' image(s) to 'pending'",
recovered,
)

# Prompt library — ensure defaults exist, load active templates
await ensure_defaults(db)
vision_prompt = await get_active_prompt(db, STAGE_VISION)
Expand All @@ -79,6 +93,20 @@ async def lifespan(app: FastAPI):
app.state.worker = worker
await worker.start()

# Startup recovery: re-enqueue any images that were pending when the
# container last stopped. The worker queue is in-memory only, so pending
# items from a previous session are never picked up without this.
async with db.execute(
"SELECT id FROM images WHERE status = 'pending'"
) as cursor:
pending_ids = [row[0] for row in await cursor.fetchall()]
if pending_ids:
enqueued = await worker.enqueue(pending_ids)
logger.info(
"Startup recovery: enqueued %d pending image(s) for processing",
enqueued,
)

# File watcher
watcher = FileWatcher(db=db, settings=settings, worker=worker)
app.state.watcher = watcher
Expand Down