Skip to content

Commit

Permalink
Merge pull request #2 from fernandoalmeida/support-concurrent-instances
Browse files Browse the repository at this point in the history
Support concurrent application instances/checks
  • Loading branch information
fabioperrella authored Sep 28, 2017
2 parents 74785df + ac7a0f8 commit 9c24b13
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/heartcheck/cache.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require 'securerandom'
require 'heartcheck'
require 'heartcheck/checks/cache'
15 changes: 12 additions & 3 deletions lib/heartcheck/checks/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def validate
#
# @return [Bollean]
def set?(con)
con.set('check_test', 'heartcheck')
con.set(unique_check_key, 'heartcheck')
rescue
false
end
Expand All @@ -35,7 +35,7 @@ def set?(con)
#
# @return [Bollean]
def get?(con)
con.get('check_test') == 'heartcheck'
con.get(unique_check_key) == 'heartcheck'
rescue
false
end
Expand All @@ -46,7 +46,7 @@ def get?(con)
#
# @return [Bollean]
def delete?(con)
con.delete('check_test')
con.delete(unique_check_key)
rescue
false
end
Expand All @@ -61,6 +61,15 @@ def delete?(con)
def custom_error(name, key_error)
@errors << "#{name} fails to #{key_error}"
end

# generate an unique redis key
# It's necessary to run concurrent application instances/checks using a
# shared memcache
#
# @return [String]
def unique_check_key
@unique_check_key ||= SecureRandom.hex
end
end
end
end
18 changes: 18 additions & 0 deletions spec/heartcheck/checks/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,23 @@
end
end
end

it 'supports concurrent application instances/checks' do
total_concurrent_instances = 30

concurrent_checks = 0.upto(total_concurrent_instances).map do |n|
Thread.new do
checker_instance = described_class.new.tap do |c|
c.add_service(name: "check #{n}", connection: connection)
end

checker_instance.check
end
end

check_results = concurrent_checks.map(&:value)

expect(check_results.inspect).not_to include('error')
end
end
end

0 comments on commit 9c24b13

Please sign in to comment.