Skip to content

Commit 884037b

Browse files
committed
feat: configurable cleanup job crons
1 parent 56d9477 commit 884037b

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/env.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import { tmpdir } from 'node:os'
12
import { z } from 'zod'
23

34
const booleanSchema = z.string().transform((v) => v.toLowerCase() === 'true')
45
const portSchema = z.coerce.number().int().min(1).max(65_535)
56

67
const envSchema = z.object({
78
ENABLE_DIRECT_DOWNLOADS: booleanSchema.default('false'),
8-
CLEANUP_OLDER_THAN_DAYS: z.coerce.number().int().min(0).default(90),
9+
CACHE_CLEANUP_OLDER_THAN_DAYS: z.coerce.number().int().min(0).default(90),
10+
CACHE_CLEANUP_CRON: z.string().default('0 0 * * *'),
11+
UPLOAD_CLEANUP_CRON: z.string().default('*/10 * * * *'),
912
API_BASE_URL: z.string().url(),
1013
STORAGE_DRIVER: z.string().toLowerCase().default('filesystem'),
1114
DB_DRIVER: z.string().toLowerCase().default('sqlite'),

plugins/cleanup.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,27 @@ import { Cron } from 'croner'
77
import { useStorageAdapter } from '~/lib/storage'
88

99
export default defineNitroPlugin(() => {
10-
if (ENV.CLEANUP_OLDER_THAN_DAYS === 0) return
10+
// cache cleanup
11+
if (ENV.CACHE_CLEANUP_OLDER_THAN_DAYS > 0) {
12+
const job = new Cron(ENV.CACHE_CLEANUP_CRON)
13+
const nextRun = job.nextRun()
14+
logger.info(
15+
`Cleaning up cache entries older than ${colorize('blue', `${ENV.CACHE_CLEANUP_OLDER_THAN_DAYS}d`)} with schedule ${colorize('blue', job.getPattern() ?? '')}${nextRun ? ` (next run: ${nextRun.toLocaleString()})` : ''}`,
16+
)
17+
job.schedule(async () => {
18+
const adapter = await useStorageAdapter()
19+
await adapter.pruneCaches(ENV.CACHE_CLEANUP_OLDER_THAN_DAYS)
20+
})
21+
}
1122

12-
const job = new Cron('0 0 * * *') // daily
23+
// upload cleanup
24+
const job = new Cron(ENV.UPLOAD_CLEANUP_CRON)
1325
const nextRun = job.nextRun()
1426
logger.info(
15-
`Cleaning up cache entries older than ${colorize('blue', `${ENV.CLEANUP_OLDER_THAN_DAYS}d`)} with schedule ${colorize('blue', job.getPattern() ?? '')}${nextRun ? ` (next run: ${nextRun.toLocaleString()})` : ''}`,
27+
`Cleaning up dangling uploads with schedule ${colorize('blue', job.getPattern() ?? '')}${nextRun ? ` (next run: ${nextRun.toLocaleString()})` : ''}`,
1628
)
1729
job.schedule(async () => {
1830
const adapter = await useStorageAdapter()
19-
await adapter.pruneCaches(ENV.CLEANUP_OLDER_THAN_DAYS)
2031
await adapter.pruneUploads()
2132
})
2233
})

0 commit comments

Comments
 (0)