Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make shopify theme dev more resilient to HTTP errors with the Admin API #4606

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

karreiro
Copy link
Contributor

@karreiro karreiro commented Oct 9, 2024

WHY are these changes introduced?

Related to #4576, #4598, and #4599

Retry requests for some HTTP errors. This PR doesn't propose to refresh the session because we're already refreshing it every 30 minutes, the Admin session token expires in 2 hours, and we're getting a fresh token when we start the session.

WHAT is this pull request doing?

This PR updates the Admin REST API client to retry requests when they encounter certain HTTP errors. The Ruby CLI was more permissive regarding execution abortion, so we're adopting this approach as a middle-term to: fail when errors really happen but also be resilient and retry in some scenarios.

How to test your changes?

  • Run shopify theme push -d
  • Cherry-pick this commit e806941bd7538a0325593da4c02663c599f18e64 in your local CLI (it will trigger an error every time we update a file)
  • Update the layout/theme.liquid file
  • Run shopify theme push -d --verbose
  • Notice the retries happening in the logs

Post-release steps

N/A

Measuring impact

How do we know this change was effective? Please choose one:

  • n/a - this doesn't need measurement, e.g. a linting rule or a bug-fix
  • Existing analytics will cater for this addition
  • PR includes analytics changes to measure impact

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes

@karreiro karreiro requested review from frandiox and a team October 9, 2024 07:42
Copy link
Contributor

github-actions bot commented Oct 9, 2024

Thanks for your contribution!

Depending on what you are working on, you may want to request a review from a Shopify team:

  • Themes: @shopify/advanced-edits
  • UI extensions: @shopify/ui-extensions-cli
    • Checkout UI extensions: @shopify/checkout-ui-extensions-api-stewardship
  • Hydrogen: @shopify/hydrogen
  • Other: @shopify/app-inner-loop

Copy link
Contributor

github-actions bot commented Oct 9, 2024

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
72.7% (+0.03% 🔼)
8550/11760
🟡 Branches
69.67% (+0.03% 🔼)
4200/6028
🟡 Functions
71.77% (+0.05% 🔼)
2212/3082
🟡 Lines
73.04% (+0.03% 🔼)
8094/11082

Test suite run success

1943 tests passing in 876 suites.

Report generated by 🧪jest coverage report action from 707b901

@EvilGenius13
Copy link
Contributor

🚀 Thank you! This looks great. I left a quick question about the retry time


outputDebug(`[${retries}] Retrying '${path}' request...`)

await sleep(0.2)
Copy link
Contributor

Choose a reason for hiding this comment

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

Was there a decision that led you to using 0.2 seconds as our retry "cooldown"?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should use different cooldowns depending on the error type... 0.2 seems sensible for something that is likely to keep failing, like a 401, so that we fail fast.
But perhaps a 503 could recover if we give it more time? Not sure really 🤔

At the same time, we are also adding another type of error handling in #4599 , so maybe it's good to fail fast here and let the consumer handle the situation...

Copy link
Contributor

@frandiox frandiox left a comment

Choose a reason for hiding this comment

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

Great! I think the dev server is going to be much more resilient to random errors with this 🎉

Comment on lines 133 to +135
case status === 401:
throw new AbortError(`[${status}] API request unauthorized error`)
// Retry 401 errors to be resilient to authentication errors.
return handleRetriableError({
Copy link
Contributor

Choose a reason for hiding this comment

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

If we don't refresh the token, I'm not sure if this will do anything? I guess it doesn't hurt much but the token will be the same so I assume the API will keep returning 401?

Perhaps we should explore the option of "refresh on demand" when auth errors happen in addition to refreshing periodically in an interval? For example, something like session.refresh(). Or perhaps a job for AsyncLocalStorage if it's too hard to pass that logic down here 🤔


outputDebug(`[${retries}] Retrying '${path}' request...`)

await sleep(0.2)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should use different cooldowns depending on the error type... 0.2 seems sensible for something that is likely to keep failing, like a 401, so that we fail fast.
But perhaps a 503 could recover if we give it more time? Not sure really 🤔

At the same time, we are also adding another type of error handling in #4599 , so maybe it's good to fail fast here and let the consumer handle the situation...

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.

3 participants