Effective Ruby: 48 Specific Ways to Write Better Ruby by Peter J. Jones
Author:Peter J. Jones [Jones, Peter J.]
Language: eng
Format: epub
Tags: Programming Languages, Computers, Ruby, General
ISBN: 9780133846973
Google: pzd_BAAAQBAJ
Publisher: Pearson Education
Published: 2014-09-01T21:14:37+00:00
Item 26: Bound retry Attempts, Vary Their Frequency, and Keep an Audit Trail
Item 23 explains why it’s important to rescue only those specific exceptions which you know how to handle and how things can go wrong when using rescue. I recommend you familiarize yourself with the ramifications involved when using rescue because this item expands on that idea by demonstrating how to resolve an error condition by rerunning the code which caused it.
Although not very common, certain errors can be safely handled by rescuing the resulting exception and restarting the enclosing scope with the retry keyword. Suppose you’ve written a program which communicates with a third party vendor using a fancy web API. When specific records in your database change your code kicks off and attempts to transmit the changes to the vendor. The only problem is, the vendor is running some very buggy software. 1 out of every 30 requests you make result in a response informing you that your data transfer failed due to a database deadlock error. Good news though, the vendor is aware of the problem and should have it fixed in the next three months.
After some experimenting you discover a simple workaround, retrying the request after a short delay results in a successful transaction. With hundreds of these failures an hour, this task clearly needs to be automated. A natural first attempt might look something like this:
begin
service.update(record)
rescue VendorDeadlockError
sleep(15)
retry
end
There’s a major problem with this code that isn’t immediately obvious. Testing this code—even putting it into production—might work for a bit of time until an unexpected edge case is encountered. The problem lies with how we’re using retry and the fact that it creates a hidden loop in the code. As long as the update method continues to raise a VendorDeadlockError the block will continue to get restarted. Writing an unconditional retry is akin to writing an infinite loop. To make this clearer consider this use of while which emulates our first attempt at using retry:
while true
begin
service.update(record)
rescue VendorDeadlockError
sleep(15)
# Drop exception
else
break # Success, exit loop.
end
end
This is quite a bit uglier but it does make the loop explicit. If we’re going to use retry then we need to take this implicit loop into consideration. One correct solution is to place an upper bound on the number of retry attempts. This is often done with a bounding variable. The tricky part is ensuring that this variable is correctly scoped:
retries = 0
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9912)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8295)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7762)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6816)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6547)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6413)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4037)
Solidity Programming Essentials by Ritesh Modi(3991)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3781)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3723)
The Ultimate iOS Interview Playbook by Avi Tsadok(3699)
