Hey @crpatel 1)So Understand the real problem
Shopify is working correctly.
What happens is:
Shopify refreshes the offline token
But your background job is still using an old in-memory session/client
So your job is “out of sync” with the latest token.
- Pass ONLY shop into jobs
When you enqueue a job send ONLY:
shop
Do not pass:
session
admin client
authenticated context
Because these can expire or change after token refresh.
- Re-create Admin client inside every job
Inside your worker always do:
await unauthenticated.admin(shop)
This is important Because, This pulls the latest token from storage
Ensures you never use stale credentials
Keeps every job fresh and safe
- Never cache Admin client
Avoid:
global admin client
singleton admin instance
storing admin in module variables
keeping admin in closures
Because cached admin is equal to old token in memory
Even if Shopify already refreshed it.
5)Treat every job as “stateless”
Each background job should behave like:
I know nothing I will rebuild everything fresh
So every job:
gets shop
rebuilds admin
runs API call
6) Add 401 retry handling it’s safety layer
Sometimes race conditions still happen.
So if request fails:
So You can Do this:
Catch 401 Unauthorized
Re-run:
unauthenticated.admin(shop)
Retry request once
7)For long-running workers
If your worker runs continuously:
You must:
Not reuse admin between jobs
Not store session globally
Re-create admin for every task
Don’t pass things like:
session
admin client
access token
authenticated objects
Because the shop stays the same but sessions and tokens can expire or change.
If you pass them into jobs they can become outdated and cause errors.
I hope it’s help You
Thank You !