mirror of
https://github.com/beilunyang/moemail.git
synced 2026-06-03 14:40:21 +08:00
fix: cleanup worker
This commit is contained in:
@@ -11,7 +11,7 @@ const CLEANUP_CONFIG = {
|
|||||||
} as const
|
} as const
|
||||||
|
|
||||||
const main = {
|
const main = {
|
||||||
async scheduled(event: ScheduledEvent, env: Env) {
|
async scheduled(_: ScheduledEvent, env: Env) {
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -20,20 +20,20 @@ const main = {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Directly delete expired emails (messages will be cascade-deleted via foreign key constraint)
|
|
||||||
const result = await env.DB
|
const result = await env.DB
|
||||||
.prepare(`
|
.prepare(`
|
||||||
DELETE FROM email
|
DELETE FROM email
|
||||||
WHERE expires_at < ?
|
WHERE expires_at < ?
|
||||||
LIMIT ?
|
LIMIT ?
|
||||||
RETURNING id
|
|
||||||
`)
|
`)
|
||||||
.bind(now, CLEANUP_CONFIG.BATCH_SIZE)
|
.bind(now, CLEANUP_CONFIG.BATCH_SIZE)
|
||||||
.run()
|
.run()
|
||||||
|
|
||||||
const deletedCount = result?.meta?.changes || 0
|
if (result.success) {
|
||||||
console.log(`Deleted ${deletedCount} expired emails and their associated messages`)
|
console.log(`Deleted ${result?.meta?.changes ?? 0} expired emails and their associated messages`)
|
||||||
|
} else {
|
||||||
|
console.error('Failed to delete expired emails')
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to cleanup:', error)
|
console.error('Failed to cleanup:', error)
|
||||||
throw error
|
throw error
|
||||||
|
|||||||
Reference in New Issue
Block a user