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
+27
View File
@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from 'next/server'
import { ArchPackageFetcher } from '@/services/archPackageFetcher'
import { query } from '@/lib/database/config'
import { SyncAuth } from '@/lib/sync/auth'
export const dynamic = 'force-dynamic'
@@ -51,6 +52,7 @@ export async function POST(request: NextRequest) {
// Start sync in background
;(async () => {
try {
const runStartedAt = new Date()
const fetcher = new ArchPackageFetcher()
// Fetch packages
@@ -76,6 +78,31 @@ export async function POST(request: NextRequest) {
syncStatus.storeTotal = total
}
)
// Prune packages not seen in this run (Arch official scope)
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 repository = $2
AND is_active = true
AND (last_seen_at IS NULL OR last_seen_at < $3)`,
['arch', 'official', cutoff]
)
// Optional hard delete of long-inactive packages
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 repository = $2
AND is_active = false
AND last_seen_at IS NOT NULL AND last_seen_at < $3`,
['arch', 'official', deleteCutoff]
)
}
syncStatus.status = 'complete'
console.log('✅ Arch Linux sync completed successfully')