Skip to content

Commit 99c86c2

Browse files
committed
Introduce health threshold
1 parent dbb57ad commit 99c86c2

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

bin/docker-entrypoint

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then
66
bundle exec rake \
77
orchestrator:start[postgresql] \
88
orchestrator:start[redis] \
9-
orchestrator:await_healthy[postgresql] \
10-
orchestrator:await_healthy[redis] \
9+
orchestrator:await_healthy[postgresql,5] \
10+
orchestrator:await_healthy[redis,5] \
1111
orchestrator:connect_self
1212

1313
# create connection environment variables

lib/tasks/orchestrator.rake

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,34 @@ namespace :orchestrator do
4545
abort("#{args[:container]} is not healthy") unless Sagittarius::Orchestrator::State[args[:container]].healthy?
4646
end
4747

48-
auto_reenable_task :await_healthy, %i[container timeout] => :build_state do |_, args|
48+
auto_reenable_task :await_healthy, %i[container timeout min_attempts] => :build_state do |_, args|
4949
timeout = args.fetch(:timeout, 30).to_i
50+
min_attempts = args.fetch(:min_attempts, 2).to_i
5051
container = Sagittarius::Orchestrator::State[args[:container]]
5152

52-
(0..timeout).each do |attempt|
53+
total_attempts = 0
54+
successful_attempts = 0
55+
(0..timeout).each do
56+
total_attempts += 1
5357
if container.healthy?
54-
puts "#{container.name} is healthy after #{attempt} seconds"
55-
break
58+
successful_attempts += 1
59+
else
60+
successful_attempts = 0
5661
end
62+
63+
break if successful_attempts >= min_attempts
64+
5765
sleep 1
5866
end
5967

60-
abort("#{container.name} did not get healthy within #{timeout} seconds") unless container.healthy?
68+
if successful_attempts >= min_attempts
69+
puts "#{container.name} is healthy after #{total_attempts} seconds"
70+
elsif container.healthy?
71+
abort("#{container.name} did get healthy, but did not meet health threshold of " \
72+
"#{min_attempts} within #{timeout} seconds")
73+
else
74+
abort("#{container.name} did not get healthy within #{timeout} seconds")
75+
end
6176
end
6277

6378
task :create_connection_environment, [] => :build_state do |_, args|

0 commit comments

Comments
 (0)