That's a broad question and quite non-specific.
There could be many approaches.
Basically you'd have to capture the failure and retry keeping track of how many attempts you've made until you've hit a "reasonable" limit.
You'll likely hit some kind of time out though I imagine which you cannot do much about.