Ruby Retry- Where you been?
For some reason, I never knew about ruby's retry
keyword. The more you know...
def api_request
TwitterWrapper.make_request # Throws a ServiceUnavailabe(506)- Server overloaded
rescue ServiceUnavailable => error
retries = retries.to_i + 1 # Increment a retry counter
retries < 5 ? retry : raise(error) # run the method again until "retries is exceeded"
# notice the local variable "retries" is persisted through retries
end
You could put a sleep in there if you wanted to wait a certain amount of time before retrying.
h/t Vinicius Negrisolo
Tweet