Supabase Edge Functions
Authoritative specifications, execution models, and architecture for Attendrix Deno Edge Functions.
Supabase Edge Functions
Attendrix executes serverless logic on the Supabase Edge Functions runtime (powered by Deno and V8 isolates). Edge functions handle asynchronous background queues, Google OAuth 2.0 token negotiations, OneSignal push broadcasts, and scheduled NASA APOD synchronization.
graph TD
Client["Flutter Mobile Client"] -->|HTTPS / OAuth| EF_Auth["google-calendar-auth"]
Client -->|HTTPS / JWT| EF_Disc["google-calendar-disconnect"]
Cron["pg_cron / External Evaluator"] -->|Trigger| EF_Eval["notify-scheduled-evaluator"]
Cron -->|Nightly| EF_Apod["fetch-apod"]
EF_Eval -->|pgmq_send| DB_NQ[("public.notification_queue (PGMQ)")]
DB_Trig["DB Trigger: Statement Coalesce"] -->|pgmq_send| DB_GQ[("public.gcal_sync_queue (PGMQ)")]
DB_NQ -->|pgmq_read| EF_Notif["notify-worker"]
DB_GQ -->|pgmq_read| EF_GCal["google-calendar-sync-worker"]
EF_Notif -->|REST API| OneSignal["OneSignal Push Delivery"]
EF_GCal -->|OAuth v3 REST| GCalAPI["Google Calendar API"]Active Functions Inventory
| Slug | Status | Version | JWT Verify | Trigger Mechanism | Primary Responsibilities |
|---|---|---|---|---|---|
google-calendar-auth | ACTIVE | 16 | true | HTTP POST / GET | PKCE OAuth URL generation and Google token exchange into Supabase Vault. |
google-calendar-sync-worker | ACTIVE | 17 | true | PGMQ / HTTP | Reconciles classes with Google Calendar API using deterministic event IDs. |
google-calendar-disconnect | ACTIVE | 5 | true | HTTP POST | Revokes Google OAuth grant, deletes secondary calendar, purges sync leases. |
notify-worker | ACTIVE | 10 | true | PGMQ / HTTP | Consumes notification_queue, renders multi-tone copy, dispatches via OneSignal. |
notify-scheduled-evaluator | ACTIVE | 6 | true | Cron / HTTP | Evaluates upcoming classes (10m), tasks/exams (1h, 24h), and mess menus. |
fetch-apod | ACTIVE | 9 | false | Cron / Daily | Ingests daily NASA Astronomy Picture of the Day metadata and images into public.apod. |
Deep Dive: google-calendar-sync-worker
The google-calendar-sync-worker function reconciles scheduled classes with the student's Google Calendar account without creating duplicate events or generating unnecessary API calls.
1. Deterministic Event IDs
Google Calendar event IDs must be unique per calendar and conform to ^[a-v0-9]{5,1024}$. Attendrix derives deterministic IDs via SHA-256 and base32 hex encoding:
const raw = `${userId}:${classId}`;
const hex = await sha256Hex(raw);
const base32 = toBase32Hex(hex);
const googleEventId = `attendrix${base32.substring(0, 40)}`;This guarantees idempotency: retrying a sync job for the same class and user always targets the exact same Google Calendar event ID.
2. Distributed Leases & Serialization
To avoid concurrent modifications to a user's Google Calendar:
- The worker calls
acquire_gcal_user_sync_lease(userId, workerId, 45). - If another worker currently holds an unexpired lease, the worker yields immediately.
- Upon completion or failure, the lease is released via
release_gcal_user_sync_lease.
3. PGMQ Visibility & Poison Pills
- Claims batches of up to 10 messages with a 180-second visibility timeout (
vt = 180). - If
read_ct >= 5(max attempts reached), the message is classified as a poison pill and archived to the dead-letter queue viapgmq_archive. - Failed messages receive exponential backoff up to 1 hour:
const vtOffset = Math.min(60 * Math.pow(2, readCt), 3600);
await supabase.rpc("pgmq_set_vt", { p_queue_name: "gcal_sync_queue", p_msg_id: msgId, p_vt_offset: vtOffset });Deep Dive: notify-worker
The notify-worker processes notifications from notification_queue, performing personalization, quiet-hours filtering, and OneSignal batch dispatching.
1. Multi-Tone Personalization Engine
Attendrix personalizes notification copy based on each student's action_tone preference:
- Direct: Neutral, concise, factual.
- Playful: Energetic, emoji-rich, warm.
- Roast: Sarcastic, humorous reminders emphasizing attendance consequences.
- Motivational: Goal-oriented, positive, empowering.
2. Quiet Hours Filtering
Students can configure a quiet hours window (e.g. 22:00:00 to 07:00:00 IST). Non-urgent notifications are suppressed during quiet hours. Only time-sensitive notifications bypass quiet hours:
class_reminder(10 minutes before lecture)class_cancelledclass_rescheduledextra_class_addedmess_reminder
3. Deduplication & Cross-User Privacy Guard
- Deliveries are logged to
public.notification_logwith unique constraint(user_id, dedup_key). - Before dispatching to OneSignal, recipients are grouped by distinct copy and deep link to guarantee personalized tokens (e.g. student first names) never leak across users.
Was this page helpful?
Your feedback directly guides the engineering documentation roadmap.