Teacher work Reminder delivery
The D1 worker is a long-lived Bun process, apps/api/src/teacher-work-worker.ts, bundled as
teacher-work-worker.js in functions-data. The Helm service is teacher-work-worker, port 3016,
with /health and /ready. Enable it only after the additive D1 migrations are applied through the
normal migration workflow. It creates in-app Notifications and optionally submits email; it never
executes a Task action or flips Survey status on a schedule.
DATABASE_URL is required and injected by the chart. Email uses the existing sender configuration:
EMAIL_SENDER_TYPE=mailgun, MAILGUN_API_KEY, MAILGUN_DOMAIN, MAILGUN_FROM, and optional
MAILGUN_REGION / MAILGUN_BASE_URL. Inject credentials through Kubernetes secretKeyRef; do not
put them in Task actions, Notifications, logs, or committed values. Missing email configuration leaves
in-app delivery running and records email as unavailable. The sender resolves at boot, so restart
the worker after changing its configuration. Only a current active Supabase identity with a verified,
nondeleted, nonbanned mailbox qualifies; roster contact addresses are not email destinations.
The defaults are an active cadence of 2 seconds, idle cadence of 15 seconds, 25 due prompts per pass,
5 email attempts per pass, 3 maximum email attempts, and a 5-minute interrupted-attempt window.
Override with TEACHER_WORK_ACTIVE_INTERVAL_MS, TEACHER_WORK_IDLE_INTERVAL_MS,
TEACHER_WORK_BATCH_SIZE, TEACHER_WORK_EMAIL_BATCH_SIZE, TEACHER_WORK_EMAIL_MAX_ATTEMPTS,
and TEACHER_WORK_EMAIL_PROCESSING_STALE_MS. Mailgun submission has a 30-second timeout. Multiple
workers use bounded atomic row claims and one Notification per Reminder; Recreate keeps ordinary
rollouts simple. SIGTERM/SIGINT stops the loop after its current bounded pass.
The email state is intentionally separate from in-app delivery:
| State | Meaning |
|---|---|
pending |
In-app prompt committed; email has not been attempted. |
processing |
A durable attempt identity and counter committed before remote submission. |
sent |
Mailgun accepted submission; this does not assert inbox delivery. |
failed |
A known provider refusal. A nonnull emailNextAttemptAt schedules a bounded safe retry; null means terminal. |
uncertain |
Acceptance could not be determined. Never automatically resubmitted. |
unavailable |
Email was not requested, sender configuration is absent, or current recipient/target/mailbox eligibility failed. |
Known HTTP 429 refusals use bounded exponential backoff and the provider reset time. HTTP 5xx,
network failures, timeouts, untyped transport exceptions and stale processing attempts become
uncertain. An interrupted process can have submitted mail before recording acceptance: investigate
provider logs before any deliberate new prompt, and use a new stable Reminder key if another prompt
is required. There is no exactly-once delivery guarantee or blind stale-attempt resend. This is a
conservative adapter choice: Mailgun’s API overview
documents rate-limit retry, and the send API
describes a successful submission as queued.
/health reports only progress counts, capability and in-flight duration. Database/provider faults
remain visible as pass errors while the loop continues. Liveness is suspended during a pass so a
restart cannot repeatedly interrupt submissions; Mailgun’s own timeout bounds remote I/O. A database
call that remains in flight requires investigation. No raw provider error bodies or mailbox addresses
are included in health or durable attempt errors.
Task completion/cancellation suppresses unfired prompts. Explicit reopen reconciles current source timing (generated work must still have its exact live declaration/occurrence), and delivered identities remain final. Direct Meeting editing reads and replaces exact pending schedules, grouped by key, anchor, offset and channels with their own recipient sets; it does not read another person’s delivered inbox history. An unchanged save preserves those assignments after partial delivery.