()
| 45 | } |
| 46 | |
| 47 | async function processOneJob(): Promise<void> { |
| 48 | if (processing) return; |
| 49 | processing = true; |
| 50 | |
| 51 | try { |
| 52 | // Check daily budget |
| 53 | const spent = await getTodaySpend(); |
| 54 | if (spent >= DAILY_BUDGET_CENTS) return; |
| 55 | |
| 56 | // Pick the oldest pending job |
| 57 | const [job] = await db |
| 58 | .select() |
| 59 | .from(pipelineJobs) |
| 60 | .where( |
| 61 | and( |
| 62 | eq(pipelineJobs.jobType, "chat-triggered-analysis"), |
| 63 | eq(pipelineJobs.status, "pending"), |
| 64 | ), |
| 65 | ) |
| 66 | .orderBy(asc(pipelineJobs.createdAt)) |
| 67 | .limit(1); |
| 68 | |
| 69 | if (!job) return; |
| 70 | |
| 71 | const documentId = job.documentId; |
| 72 | if (!documentId) { |
| 73 | await db.update(pipelineJobs).set({ status: "failed", errorMessage: "No document ID" }).where(eq(pipelineJobs.id, job.id)); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | // Mark as running |
| 78 | await db.update(pipelineJobs).set({ |
| 79 | status: "running", |
| 80 | startedAt: new Date(), |
| 81 | attempts: job.attempts + 1, |
| 82 | }).where(eq(pipelineJobs.id, job.id)); |
| 83 | |
| 84 | // Get document info |
| 85 | const [doc] = await db.select().from(documents).where(eq(documents.id, documentId)); |
| 86 | if (!doc) { |
| 87 | await db.update(pipelineJobs).set({ status: "failed", errorMessage: "Document not found" }).where(eq(pipelineJobs.id, job.id)); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | // Read all pages for this document |
| 92 | const pages = await db |
| 93 | .select({ content: documentPages.content, pageNumber: documentPages.pageNumber }) |
| 94 | .from(documentPages) |
| 95 | .where(eq(documentPages.documentId, documentId)) |
| 96 | .orderBy(asc(documentPages.pageNumber)); |
| 97 | |
| 98 | const fullText = pages.map((p) => `Page ${p.pageNumber}\n${p.content}`).join("\n\n"); |
| 99 | |
| 100 | if (fullText.length < MIN_TEXT_LENGTH) { |
| 101 | await db.update(pipelineJobs).set({ status: "completed", completedAt: new Date(), errorMessage: "Text too short" }).where(eq(pipelineJobs.id, job.id)); |
| 102 | await db.update(documents).set({ aiAnalysisStatus: "skipped" }).where(eq(documents.id, documentId)); |
| 103 | return; |
| 104 | } |
nothing calls this directly
no test coverage detected