feat: add worker main poll loop

This commit is contained in:
2026-08-10 14:46:30 +02:00
parent 443657ceef
commit e6d937fd9b
2 changed files with 92 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
import { prisma } from '@/lib/prisma'
import { config } from '../config/app.config'
import { processDownload } from './processor'
export async function pollOnce(): Promise<void> {
const pending = await prisma.download.findMany({
where: { status: 'PENDING' },
orderBy: { submittedAt: 'asc' },
take: config.WORKER_CONCURRENCY,
select: { id: true },
})
if (pending.length === 0) return
const ids = pending.map((d) => d.id)
await prisma.download.updateMany({
where: { id: { in: ids }, status: 'PENDING' },
data: { status: 'PROCESSING', startedAt: new Date() },
})
await Promise.all(ids.map((id) => processDownload(id)))
}
export async function runWorker(): Promise<never> {
console.log('[worker] started')
while (true) {
try {
await pollOnce()
} catch (err) {
console.error('[worker] poll error:', err)
}
await new Promise((resolve) =>
setTimeout(resolve, config.WORKER_POLL_INTERVAL_MS)
)
}
}
if (require.main === module) {
runWorker().catch(console.error)
}