/** * THE JOBS QUEUE'S CONSUMER (JOBS OUT 1, assets/design/shared/JOBS-OUT.md): each message is one run of one job, * handed a narrowed environment: the account object's door (the same front a phone's request goes through), a TCP * socket, and nothing else. That is a convention inside one isolate (the closure holding the front holds the Worker's * env), kept by review and a test; a real boundary is a separate Worker (assets/design/shared/JOBS-OUT.md, L4). * * One message a batch (wrangler.jsonc `max_batch_size: 1`), so each run has the consumer's 15 minutes to itself. * A run the object was told the end of is acknowledged; one that could not tell it is retried a minute later, at * most `max_retries` times, then dead-lettered; the account object's watchdog asks for a new run either way. */ import { dial, type Dial } from "../mail/imap.ts"; import { isMailJob, runMailJob, type MailJobDeps, type MailJobMessage } from "./mail_job.ts"; import { isStepJob, runStepJob } from "./step_job.ts"; /** The queues whose messages are jobs (the live Worker's and the dev Worker's). */ export const JOB_QUEUES = new Set(["cotton-jobs", "cotton-dev-jobs"]); /** The door, as a phone reaches it: a request on `/b//…` through the front Worker. */ export type Front = (request: Request) => Promise; /** `socket` is the Worker's TCP socket; a test hands its fake mail server. */ export async function consume(batch: MessageBatch, front: Front, label: string | undefined, socket: Dial = dial, tune: Pick = {}): Promise { for (const m of batch.messages) { // A step job (JOBS OUT M1): bounded steps through the door, no socket. The test Worker's tests run each run // themselves (runStepJob), so a delivery there is only acknowledged. if (isStepJob(m.body)) { const job = m.body; if (label === "test") { m.ack(); continue; } const started = Date.now(); try { const outcome = await runStepJob({ door: (slug, path, init) => front(new Request(`https://jobs.internal/b/${slug}${path}`, init)) }, job); console.log(`cottondb: job ${job.job} ${job.slug}: ${outcome} in ${Date.now() - started} ms`); m.ack(); } catch (error) { console.log(`cottondb: job ${job.job} ${job.slug}: retry (${error instanceof Error ? error.message : String(error)})`); m.retry({ delaySeconds: 60 }); } continue; } if (!isMailJob(m.body)) { console.log(`cottondb: job dropped: not a job (${batch.queue})`); m.ack(); continue; } // The test Worker reaches no mail server: its tests run each job against their own fake one (runMailJob). if (label === "test") { m.ack(); continue; } const job: MailJobMessage = m.body; const started = Date.now(); try { const outcome = await runMailJob({ door: (slug, path, init) => front(new Request(`https://jobs.internal/b/${slug}${path}`, init)), dial: socket, ...tune }, job); console.log(`cottondb: job mail_fetch ${job.slug}: ${outcome} in ${Date.now() - started} ms`); m.ack(); } catch (error) { console.log(`cottondb: job mail_fetch ${job.slug}: retry (${error instanceof Error ? error.message : String(error)})`); m.retry({ delaySeconds: 60 }); } } }