I have a piece of code that needs to remove (rm -rf) a directory, but sometimes FileUtils#rm_rf will fail and return an Errno::ENOENT with the message “Directory not empty.” Maybe a file in that directory is locked by the OS, but for whatever reason, it can fail. So in such a case, I retry it a few times and exit if it fails to remove that directory after those attempts.
The code might be familiar, it looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 | |
The retry counting and incrementing bit can be verbose and if you need similar functionality elsewhere in your code, it can get kind of messy. I have another piece of code that uses Grit to clone a git repo and that can time out so I retry that code a few times too. This type of retry code can be useful when accessing external apis or anything that can possibly time out that you might want to retry.