Skip to content

Commit eca7572

Browse files
committed
Implement retries for MongoDB shell commands
Retries for MongoDB shell commands could be useful in some cases: * unstable connectivity * when replica or sharding initialization is in process and you should wait some time till its end
1 parent 6e0351d commit eca7572

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

lib/puppet/provider/mongodb.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,27 @@ def self.get_conn_string
7676

7777
# Mongo Command Wrapper
7878
def self.mongo_eval(cmd, db = 'admin')
79+
retry_count = 10
80+
retry_sleep = 3
7981
if mongorc_file
8082
cmd = mongorc_file + cmd
8183
end
8284

83-
out = mongo([db, '--quiet', '--host', get_conn_string, '--eval', cmd])
85+
out = nil
86+
retry_count.times do |n|
87+
begin
88+
out = mongo([db, '--quiet', '--host', get_conn_string, '--eval', cmd])
89+
rescue => e
90+
debug "Request failed: '#{e.message}' Retry: '#{n}'"
91+
sleep retry_sleep
92+
next
93+
end
94+
break
95+
end
96+
97+
if !out
98+
fail "Could not evalute MongoDB shell command: #{cmd}"
99+
end
84100

85101
out.gsub!(/ObjectId\(([^)]*)\)/, '\1')
86102
out

0 commit comments

Comments
 (0)