Skip to content

Commit

Permalink
Merge pull request #77 from mougams/consul_backup_url
Browse files Browse the repository at this point in the history
Feature to attempt consul get call to backup url if failure
  • Loading branch information
jlundqvist-criteo authored Jan 10, 2025
2 parents 9a1169e + 1ff0d45 commit 359a51b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
16 changes: 16 additions & 0 deletions libraries/consul.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,21 @@ def self.setup_consul(options)
options[:token] = options[:consul_token]
end
end

def self.update_backup_url(options)
require 'diplomat'
return unless options[:consul_backup_url]

Diplomat.configure do |config|
config.url = @options[:consul_backup_url]
end
end

def self.reset_url
require 'diplomat'
Diplomat.configure do |config|
config.url = Diplomat::Configuration.parse_consul_addr
end
end
end
end
7 changes: 6 additions & 1 deletion libraries/primitive_consul_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ def self.get_or_create(path, concurrency:, **kwargs)
dc = kwargs[:dc]
token = kwargs[:token]
require 'diplomat'
retry_left = 5
retry_total_attempts = 5
connection_failed_count = 0
retry_left = retry_total_attempts
value = Mash.new({ version: 1, concurrency: concurrency, holders: {} })
current_lock = begin
Chef::Log.info "Fetch lock state for #{path}"
Expand All @@ -126,12 +128,15 @@ def self.get_or_create(path, concurrency:, **kwargs)
retry_secs = 30
Chef::Log.info "Consul did not respond, wait #{retry_secs} seconds and retry to let it (re)start: #{e}"
sleep retry_secs
connection_failed_count += 1
ConsulCommon.update_backup_url(@options) if connection_failed_count == retry_total_attempts / 2
(retry_left -= 1).positive? ? retry : raise
rescue Diplomat::KeyNotFound
Chef::Log.info "Lock for #{path} did not exist, creating with value #{value}"
Diplomat::Kv.put(path, value.to_json, cas: 0, dc: dc, token: token) # we ignore success/failure of CaS
(retry_left -= 1).positive? ? retry : raise
end.first
ConsulCommon.reset_url
desired_lock = bootstrap_lock(value, current_lock)
new(path, new_lock: desired_lock, dc: dc, token: token)
end
Expand Down

0 comments on commit 359a51b

Please sign in to comment.