feat: add package pruning system with configurable grace periods

- Implemented automatic pruning of stale packages across all platform sync endpoints (Arch, AUR, Debian, Ubuntu, Fedora, Homebrew, Winget)
- Added `last_seen_at` tracking to mark packages as seen during sync runs and identify inactive packages
- Introduced `PRUNE_GRACE_DAYS` and `PRUNE_HARD_DELETE_DAYS` environment variables for configurable soft-delete and hard-delete thresholds
This commit is contained in:
Yusuf İpek
2025-11-13 01:54:23 +03:00
parent 560e78cb96
commit ea792a8a7f
15 changed files with 242 additions and 17 deletions
+24
View File
@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from 'next/server'
import { DebianPackageFetcher } from '@/services/debianPackageFetcher'
import { query } from '@/lib/database/config'
import { SyncAuth } from '@/lib/sync/auth'
export const dynamic = 'force-dynamic'
@@ -36,7 +37,30 @@ export async function POST(request: NextRequest) {
;(async () => {
try {
const runStartedAt = new Date()
await DebianPackageFetcher.fetchUbuntuPackages()
// Prune not-seen Ubuntu packages (platform ubuntu)
const graceDays = parseInt(process.env.PRUNE_GRACE_DAYS || '0', 10)
const cutoff = new Date(runStartedAt.getTime() - (graceDays > 0 ? graceDays : 0) * 24 * 60 * 60 * 1000)
await query(
`UPDATE packages
SET is_active = false
WHERE platform_id = $1
AND is_active = true
AND (last_seen_at IS NULL OR last_seen_at < $2)`,
['ubuntu', cutoff]
)
const hardDays = parseInt(process.env.PRUNE_HARD_DELETE_DAYS || '0', 10)
if (hardDays > 0) {
const deleteCutoff = new Date(runStartedAt.getTime() - hardDays * 24 * 60 * 60 * 1000)
await query(
`DELETE FROM packages
WHERE platform_id = $1
AND is_active = false
AND last_seen_at IS NOT NULL AND last_seen_at < $2`,
['ubuntu', deleteCutoff]
)
}
syncStatus.status = 'complete'
} catch (error) {
syncStatus.status = 'error'