fastrag.serve.cleanup#

Expired-row cleanup, callable from an external scheduler.

Functions

cleanup_expired(engine, *[, target, batch_size])

Delete expired sessions and chats.

Classes

CleanupReport(sessions, chats)

How many expired rows were deleted.

class fastrag.serve.cleanup.CleanupReport(sessions, chats)[source]#

Bases: object

How many expired rows were deleted.

Parameters:
sessions: int#
chats: int#
async fastrag.serve.cleanup.cleanup_expired(engine, *, target='all', batch_size=1000)[source]#

Delete expired sessions and chats.

Deleting a session cascades to its chats and messages; chats are swept separately because a chat can go idle while its session stays active.

Deletes in bounded batches — one transaction per batch — for the same reason a single unbounded DELETE would be wrong: cascading through chats and messages in one very large transaction would hold locks and bloat WAL for as long as it ran. Sessions are swept before chats, mirroring the cascade.

This function carries the deletion logic but never runs it itself — nothing in the request path calls it. It is meant to be invoked on a daily cadence by an external scheduler (cron, a Kubernetes CronJob, …); a short script under cron calls it.

Parameters:
  • engine (AsyncEngine) – The async engine to run the deletes on. The caller typically passes a PostgresChatStore’s engine. The SQL targets the plain table names (sessions/chats) on the engine’s search path — a store pointed at a non-default schema requires a schema-qualified variant (see this task’s result file).

  • target (str) – "sessions", "chats", or "all".

  • batch_size (int) – Rows deleted per transaction.

Returns:

A CleanupReport of rows deleted per table.

Return type:

CleanupReport