Skip to content

fix(backend): harden cron stat app follow-up#1931

Merged
riderx merged 1 commit into
mainfrom
codex/cron-stat-app-followup
Apr 22, 2026
Merged

fix(backend): harden cron stat app follow-up#1931
riderx merged 1 commit into
mainfrom
codex/cron-stat-app-followup

Conversation

@riderx
Copy link
Copy Markdown
Member

@riderx riderx commented Apr 21, 2026

Summary (AI generated)

  • retry cron_stat_app plan-refresh queueing when the downstream RPC returns transient failures
  • stop converting a follow-up queue miss into a full trigger failure after stats rows already persisted
  • cover exhausted-retry and eventual-success behavior in cron_stat_app follow-up unit tests

Motivation (AI generated)

cron_stat_app already saves the daily stats before it queues the downstream org-plan refresh. When that follow-up RPC returned a transient 502, the trigger still ended as a 500, which caused noisy Discord alerts and unnecessary queue retries even though the primary work had succeeded.

Business Impact (AI generated)

This reduces false-positive backend alert noise and avoids reprocessing already-saved stats for transient downstream failures. The result is cleaner operations around billing/statistics jobs without changing customer-visible stats behavior.

Test Plan (AI generated)

  • bunx vitest run tests/cron_stat_app_followup.unit.test.ts
  • bunx eslint supabase/functions/_backend/triggers/cron_stat_app.ts tests/cron_stat_app_followup.unit.test.ts
  • bun typecheck
  • bunx vitest run tests/cron_stat_app.test.ts (blocked locally: Supabase on 127.0.0.1:54321 was not running)

Generated with AI

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of organization plan refresh with automatic retry logic and exponential backoff on failures.
    • Enhanced error logging to capture retry attempts and provide better diagnostics.
  • Tests

    • Added test coverage for plan refresh retry scenarios and failure recovery.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 21, 2026

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

The changes introduce a retry-with-backoff wrapper for org plan refresh queueing in a cron job trigger. A new function encapsulates the existing RPC call, automatically retrying on failures with configurable delays. Error and success states are logged with attempt information, and corresponding tests validate both failure recovery and multi-attempt retry behavior.

Changes

Cohort / File(s) Summary
Cron Job Retry Wrapper
supabase/functions/_backend/triggers/cron_stat_app.ts
Adds retry-with-backoff wrapper function queueOrgPlanRefreshWithRetry with configurable retry attempts and delay constants. Updates request handler to invoke the new wrapper instead of direct RPC call, with enhanced logging via cloudlog/cloudlogErr capturing attempt counts and errors.
Test Updates
tests/cron_stat_app_followup.unit.test.ts
Updates existing test to expect 200 response and validates retry behavior; adjusts expected call count to 3 to reflect retry attempts. Adds new test case validating retry recovery after 2 initial failures, verifying handler succeeds after queueing is retried 3 times total.

Sequence Diagram

sequenceDiagram
    participant Handler as Request Handler
    participant Wrapper as queueOrgPlanRefreshWithRetry
    participant Retry as retryWithBackoff
    participant Queue as queueOrgPlanRefresh RPC
    participant Log as Logger (cloudlog/cloudlogErr)

    Handler->>Wrapper: Call with orgStatsRefreshTarget
    activate Wrapper
    Wrapper->>Retry: Execute with shouldRetry condition
    activate Retry
    loop Retry Loop (up to N attempts)
        Retry->>Queue: Attempt RPC call
        activate Queue
        Queue-->>Retry: Result (ok or error)
        deactivate Queue
        alt Success
            Retry-->>Wrapper: Return successful result
        else Failure & shouldRetry
            Note over Retry: Wait (backoff delay)
            Note over Retry: Increment attempt counter
        end
    end
    deactivate Retry
    
    alt Final Success
        Wrapper->>Log: cloudlog (success message with retry info)
    else Final Failure
        Wrapper->>Log: cloudlogErr (error with attempt count)
    end
    deactivate Wrapper
    Wrapper-->>Handler: Return result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 With backoff and patience, we hop and retry,
When queues delay us, we don't say goodbye,
Three tries, then success—our stats shine so bright,
Exponential grace in the cron job's night! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: hardening the cron stat app follow-up with retry logic for transient failures.
Description check ✅ Passed The description includes a comprehensive summary, motivation, business impact, and test plan. However, the standard template sections are present but organized differently with AI-generated labels.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/cron-stat-app-followup

Comment @coderabbitai help to get the list of available commands and usage tips.

@codspeed-hq
Copy link
Copy Markdown
Contributor

codspeed-hq Bot commented Apr 21, 2026

Merging this PR will not alter performance

✅ 28 untouched benchmarks


Comparing codex/cron-stat-app-followup (32cb99c) with main (a221796)

Open in CodSpeed

@sonarqubecloud
Copy link
Copy Markdown

@riderx riderx marked this pull request as ready for review April 22, 2026 09:27
@riderx riderx merged commit b34babb into main Apr 22, 2026
14 of 15 checks passed
@riderx riderx deleted the codex/cron-stat-app-followup branch April 22, 2026 09:28
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 32cb99cc8a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +97 to +100
attempts,
error: result?.error,
})
return
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Fail cron_stat_app when follow-up queueing exhausts retries

If all retries to queue_cron_stat_org_for_org fail, this branch only logs and returns, so the handler still sends a 200 response. In the current queue flow, queue_consumer deletes messages on any 2xx, which means persistent follow-up failures (for example an RPC permission/function regression, not just a transient 502) will silently drop org plan refresh work instead of being retried from the queue. Please propagate failure after retry exhaustion for non-transient errors (or otherwise preserve retryable queue semantics).

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant