feat: add cron cleanup and worker-check scripts
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import 'dotenv/config'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { config } from '../config/app.config'
|
||||
import { unlinkSync, existsSync } from 'fs'
|
||||
|
||||
async function cleanup() {
|
||||
const cutoff = new Date(
|
||||
Date.now() - config.DOWNLOAD_LINK_TTL_HOURS * 60 * 60 * 1000
|
||||
)
|
||||
|
||||
const expired = await prisma.download.findMany({
|
||||
where: { status: 'DONE', completedAt: { lt: cutoff } },
|
||||
select: { id: true, filePath: true },
|
||||
})
|
||||
|
||||
for (const dl of expired) {
|
||||
if (dl.filePath && existsSync(dl.filePath)) {
|
||||
unlinkSync(dl.filePath)
|
||||
}
|
||||
await prisma.download.update({
|
||||
where: { id: dl.id },
|
||||
data: { status: 'FILE_DELETED', deletedAt: new Date() },
|
||||
})
|
||||
}
|
||||
|
||||
console.log(`[cron-cleanup] removed ${expired.length} expired file(s)`)
|
||||
}
|
||||
|
||||
cleanup().catch(console.error).finally(() => process.exit(0))
|
||||
Reference in New Issue
Block a user