Skip to content

fix(opportunity): retry add_connect_users on transient ConnectID outages - #1854

Merged
snopoke merged 3 commits into
mainfrom
seer/retry-add-connect-users-outage
Nov 27, 2026
Merged

snopoke merged 3 commits into
mainfrom
seer/retry-add-connect-users-outage

Conversation

@sentry

@sentry sentry Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Product Description

Opportunity invites are no longer dropped when ConnectID is temporarily unavailable, such as during a proxy cutover or deployment.

Technical Summary

What was changed and why

When ConnectID is temporarily unavailable (for instance, during a proxy cutover or deployment where kamal-proxy returns HTTP 404/5xx before the app container becomes healthy), add_connect_users failed with an uncaught httpx.HTTPStatusError and dropped opportunity invites.

This PR updates add_connect_users to:

  • Treat transient ConnectID failures (httpx.TransportError and HTTP status codes 404, 502, 503, 504) as retryable errors via a dedicated _fetch_users_or_retry helper.
  • Retry up to 5 times using exponential backoff starting at 30 seconds up to a maximum of 300 seconds (ADD_CONNECT_USERS_RETRY_BASE_SECONDS and ADD_CONNECT_USERS_RETRY_MAX_SECONDS).
  • Preserve Sentry reporting for non-transient errors (e.g. 401 Unauthorized) and re-raise the original exception when retry attempts are exhausted.
  • Check task.request.is_eager before calling self.retry to prevent Celery's Retry exception from leaking when run synchronously (such as during tests or in development under CELERY_TASK_ALWAYS_EAGER).
  • Keep helper methods compact to maintain CodeScene complexity thresholds under limits.

Documentation

None

Deployment

  • This PR can be deployed: any required configuration, commands, or other manual steps required before this PR can be deployed are done.

Fixes COMMCARE-CONNECT-14K

@sentry <feedback>: Autofix iterates on these changes
@sentry stop iterating: Autofix stops iterating on this run

Co-authored-by: sentry[bot] <39604003+sentry[bot]@users.noreply.github.com>
@sentry sentry Bot assigned snopoke Nov 27, 2026
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

codescene-access[bot]

This comment was marked as outdated.

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 2 selected item(s).

Comment thread commcare_connect/opportunity/tasks.py Outdated
Comment on lines +147 to +154
logger.warning(
"ConnectID unavailable while inviting %d users to opportunity %s (attempt %d): %s",
len(user_list),
opportunity_id,
self.request.retries + 1,
exc,
)
raise self.retry(exc=exc, countdown=min(300, 30 * 2**self.request.retries))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bug · medium
add_connect_users is invoked via .delay() from request-handling views (e.g. OpportunityEdit.form_valid), with no try/except around the call. With CELERY_TASK_ALWAYS_EAGER=True (the default in local/dev settings and several test modules), this task executes synchronously inside the request. Calling self.retry(..., countdown=...) in eager mode does not actually wait; it raises celery.exceptions.Retry immediately, which will propagate up into the view instead of being handled as a normal task failure. A similar case in this same file (_retry_or_fail_finance_request) explicitly guards against this with not task.request.is_eager before retrying - the same guard seems to be missing here.

Suggestion:

Suggested change
logger.warning(
"ConnectID unavailable while inviting %d users to opportunity %s (attempt %d): %s",
len(user_list),
opportunity_id,
self.request.retries + 1,
exc,
)
raise self.retry(exc=exc, countdown=min(300, 30 * 2**self.request.retries))
logger.warning(
"ConnectID unavailable while inviting %d users to opportunity %s (attempt %d): %s",
len(user_list),
opportunity_id,
self.request.retries + 1,
exc,
)
if self.request.is_eager:
raise
raise self.retry(exc=exc, countdown=min(300, 30 * 2**self.request.retries))

Comment thread commcare_connect/opportunity/tasks.py Outdated
self.request.retries + 1,
exc,
)
raise self.retry(exc=exc, countdown=min(300, 30 * 2**self.request.retries))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maintainability · low
The backoff calculation uses inline magic numbers (30 and 300) that aren't named constants, unlike CONNECTID_TRANSIENT_STATUS_CODES and ADD_CONNECT_USERS_MAX_RETRIES defined above. Extracting them (e.g. ADD_CONNECT_USERS_RETRY_BASE_SECONDS / ADD_CONNECT_USERS_RETRY_MAX_SECONDS) would make the backoff schedule easier to tune and reason about.

Suggestion:

Suggested change
raise self.retry(exc=exc, countdown=min(300, 30 * 2**self.request.retries))
countdown = min(ADD_CONNECT_USERS_RETRY_MAX_SECONDS, ADD_CONNECT_USERS_RETRY_BASE_SECONDS * 2**self.request.retries)
raise self.retry(exc=exc, countdown=countdown)

codescene-access[bot]

This comment was marked as outdated.

@snopoke
snopoke merged commit 6c86417 into main Nov 27, 2026
14 checks passed
@snopoke
snopoke deleted the seer/retry-add-connect-users-outage branch November 27, 2026 11:04
@github-actions github-actions Bot added the deployed: production Merged PR included in a production deploy label Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deployed: production Merged PR included in a production deploy

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant