Guest User

Untitled

a guest
Jan 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. # encoding: utf-8
  2.  
  3. ## pulled from mongoid
  4.  
  5. # Provides behaviour for retrying commands on connection failure.
  6. module Mongo::Retry
  7.  
  8. # Retries command on connection failures.
  9. #
  10. # This is useful when using replica sets. When a primary server wents
  11. # down and a command is issued, the driver will raise a
  12. # Mongo::ConnectionFailure. We wait a little bit, because nodes are
  13. # electing themselves, and then retry the given command.
  14. #
  15. # By setting Mongoid.max_retries_on_connection_failure to a value of 0,
  16. # no attempt will be made, immediately raising connection failure.
  17. # Otherwise it will attempt to make the specified number of retries
  18. # and then raising the exception to clients.
  19. #
  20. # @example Retry the command.
  21. # retry_on_connection_failure do
  22. # collection.send(name, *args)
  23. # end
  24. #
  25. # @since 2.0.0
  26. def retry_on_connection_failure
  27. retries = 0
  28. begin
  29. yield
  30. rescue Mongo::ConnectionFailure => ex
  31. retries += 1
  32. raise ex if retries > 30 # Config.max_retries_on_connection_failure
  33. Kernel.sleep(0.5) ## Todo.. modify for event machine
  34. log_retry retries
  35. retry
  36. end
  37. end
  38.  
  39. private
  40.  
  41. def log_retry(retry_number)
  42. puts "A Mongo::ConnectionFailure was raised. Retry attempt ##{retry_number}."
  43. end
  44. end
Add Comment
Please, Sign In to add comment