| 211 | // --- Batch Processing --- |
| 212 | |
| 213 | async function getNextBatch(batchSize: number): Promise<Array<{ |
| 214 | jobId: number; |
| 215 | documentId: number; |
| 216 | priority: number; |
| 217 | attempts: number; |
| 218 | maxAttempts: number; |
| 219 | metadata: any; |
| 220 | }>> { |
| 221 | const jobs = await db |
| 222 | .select({ |
| 223 | jobId: pipelineJobs.id, |
| 224 | documentId: pipelineJobs.documentId, |
| 225 | priority: pipelineJobs.priority, |
| 226 | attempts: pipelineJobs.attempts, |
| 227 | maxAttempts: pipelineJobs.maxAttempts, |
| 228 | metadata: pipelineJobs.metadata, |
| 229 | }) |
| 230 | .from(pipelineJobs) |
| 231 | .where( |
| 232 | and( |
| 233 | eq(pipelineJobs.jobType, "ai_analysis"), |
| 234 | eq(pipelineJobs.status, "pending"), |
| 235 | ) |
| 236 | ) |
| 237 | .orderBy(desc(pipelineJobs.priority)) |
| 238 | .limit(batchSize); |
| 239 | |
| 240 | return jobs.filter(j => j.documentId !== null) as Array<{ |
| 241 | jobId: number; |
| 242 | documentId: number; |
| 243 | priority: number; |
| 244 | attempts: number; |
| 245 | maxAttempts: number; |
| 246 | metadata: any; |
| 247 | }>; |
| 248 | } |
| 249 | |
| 250 | async function markJobProcessing(jobId: number): Promise<void> { |
| 251 | await db |