Implement cron execution logging
epic-assignment-follow-up-reminders-cron-infrastructure-task-002 — Add structured execution logging to the cron trigger that captures start time, completion time, total duration, number of assignments evaluated, number of reminders dispatched, and any errors encountered. Store logs in a dedicated cron_execution_logs table in Supabase to support monitoring, auditing, and debugging of the reminder pipeline.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 19 - 2 tasks
Can start after Tier 18 completes
Implementation Notes
Create the table via a Supabase migration file (not ad-hoc SQL) so it is version-controlled. Use a two-step write pattern: INSERT at start with status='running', then UPDATE by the returned row ID at the end. Wrap the UPDATE in a finally block in the Edge Function to guarantee it runs even if ReminderSchedulerService throws. Store the inserted row ID in a local variable at function scope so the finally block can reference it.
Use `Date.now()` timestamps in the Edge Function and store as ISO-8601 strings mapped to timestamptz. Avoid using Supabase Realtime on this table — it is a write-heavy audit log and does not need broadcast.
Testing Requirements
Unit tests: verify the log insert/update SQL statements produce correct column values for success, failure, and skipped scenarios. Integration tests: invoke the cron Edge Function in a test Supabase environment and assert that cron_execution_logs contains exactly one row per invocation with correct status transitions. Test that a simulated ReminderSchedulerService error results in status='failed' and error_message is non-null. Validate the consecutive_failure_count increments on repeated failures and resets to 0 on a success.
Use flutter_test with Supabase test utilities for database state assertions.
If the daily cron job takes longer than 24 hours to complete (due to a large dataset or a slow query), a second instance will start while the first is still running, causing duplicate reminder dispatch for assignments processed twice.
Mitigation & Contingency
Mitigation: Implement an advisory lock that prevents a second run from starting if the first is still active. Monitor run duration via the execution log table and alert if any run exceeds 30 minutes. The 10,000-assignment load test should verify the run completes in under 5 minutes.
Contingency: If a double-run occurs, the idempotency guard in ReminderDispatchService prevents duplicate notifications from being sent. The execution log identifies the overlap and allows the ops team to investigate the root cause.
If the activity registration hook that resets last_contact_date is implemented incorrectly or not triggered for all activity types (e.g., proxy registrations, bulk registrations), peer mentors will continue receiving reminders even after logging contact, damaging user trust.
Mitigation & Contingency
Mitigation: Audit all code paths that create activity records (direct registration, proxy registration, bulk registration, coordinator proxy) and ensure each path calls the assignment contact update. Write integration tests for each registration path asserting that last_contact_date is updated.
Contingency: Provide an authenticated admin endpoint that allows manual correction of last_contact_date for a specific assignment, enabling ops to resolve individual cases while the bug is fixed and deployed.