Sync Scheduler cron configuration reader
epic-external-system-integration-configuration-backend-infrastructure-task-007 — Implement the cron configuration reader for the Sync Scheduler that queries the organization_integrations table and reads each integration's cron_schedule field (e.g., '0 2 * * *' for 2am nightly). Parse and validate cron expressions, build a schedule map per organization-integration pair, and expose a function that returns which integrations are due to run at a given timestamp.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 0 - 440 tasks
Handles integration between different epics or system components. Requires coordination across multiple development streams.
Implementation Notes
Implement as cron-config-reader.ts with a single exported async function readDueIntegrations(atTime: Date, supabaseClient: SupabaseClient): Promise
Store validated DueIntegration as { orgId: string, integrationId: string, integrationType: 'xledger' | 'dynamics' | 'bufdir' } to give the trigger engine type safety. Log a warning (not error) for any rows with invalid cron expressions so an org misconfiguration does not crash the entire scheduler run.
Testing Requirements
Unit tests: (1) valid cron '0 2 * * *' — fires at 02:00 UTC, not at 02:01; (2) invalid cron '99 * * * *' — returns validation error; (3) is_enabled = false — excluded from results; (4) cron_schedule = null — excluded; (5) month-end '0 2 31 * *' — does not fire on 30 November; (6) multiple orgs with overlapping schedules — all returned correctly. Use mock Supabase client injected at test time to avoid real DB dependency. Integration test: seed organization_integrations table locally and assert getIntegrationsDueAt returns correct subset for a known timestamp.
Supabase Edge Functions have cold start latency that can cause the first sync invocation after idle periods to fail or timeout when the external API has a short connection window, leading to missed scheduled syncs that go undetected.
Mitigation & Contingency
Mitigation: Configure Edge Function memory and implement a warm-up ping mechanism before heavy sync invocations. Set generous timeout values on the external API calls. Log all cold-start incidents for monitoring.
Contingency: If cold starts cause consistent sync failures, migrate the sync scheduler to a persistent Supabase cron job that pre-warms the function 30 seconds before the scheduled sync time.
The sync scheduler must execute jobs at predictable times for financial reporting accuracy. Drift in cron execution timing (due to Supabase infrastructure delays) could cause syncs to run at wrong times, leading to missing data in accounting exports or duplicate exports across reporting periods.
Mitigation & Contingency
Mitigation: Implement idempotency keys based on integration ID + scheduled period, so re-runs of a delayed sync cannot create duplicate exports. Log actual execution timestamps vs scheduled timestamps and alert on drift exceeding 5 minutes.
Contingency: If scheduler reliability is insufficient, integrate with a dedicated cron service (e.g., pg_cron on Supabase) for millisecond-precise scheduling, replacing the application-level scheduler.
Aggressive health monitoring ping frequency could trigger rate limiting on external APIs (especially Xledger and Dynamics), causing legitimate export calls to fail after the monitor exhausts the API's request quota.
Mitigation & Contingency
Mitigation: Use lightweight health check endpoints (HEAD requests or vendor-specific ping/status endpoints) rather than data requests. Set health check frequency to once per 15 minutes minimum. Implement exponential backoff after consecutive failures.
Contingency: If rate limiting occurs, disable active health monitoring for the affected integration type and switch to passive health detection (mark unhealthy only when a scheduled sync fails).