diff options
| -rw-r--r-- | makima/src/db/repository.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/makima/src/db/repository.rs b/makima/src/db/repository.rs index 33e48a4..7387735 100644 --- a/makima/src/db/repository.rs +++ b/makima/src/db/repository.rs @@ -3684,3 +3684,31 @@ pub async fn get_supervisor_conversation_full( ) -> Result<Option<SupervisorState>, sqlx::Error> { get_supervisor_state(pool, contract_id).await } + +// ============================================================================= +// Anonymous Task Cleanup Functions +// ============================================================================= + +/// Delete stale anonymous tasks (tasks with contract_id = NULL) that: +/// - Are in a terminal state (done, failed, merged) +/// - Are older than the specified number of days +/// +/// Returns the number of deleted tasks. +pub async fn cleanup_stale_anonymous_tasks( + pool: &PgPool, + max_age_days: i32, +) -> Result<i64, sqlx::Error> { + let result = sqlx::query( + r#" + DELETE FROM tasks + WHERE contract_id IS NULL + AND status IN ('done', 'failed', 'merged') + AND created_at < NOW() - INTERVAL '1 day' * $1 + "#, + ) + .bind(max_age_days) + .execute(pool) + .await?; + + Ok(result.rows_affected() as i64) +} |
