Module: Async::Service::Managed::HealthChecker

Included in:
Service
Defined in:
lib/async/service/managed/health_checker.rb

Overview

A health checker for managed services.

Instance Method Summary collapse

Instance Method Details

#health_checker(instance, timeout = @evaluator.health_check_timeout, parent: Async::Task.current, &block) ⇒ Object

Start the health checker.

If a timeout is specified, a transient child task will be scheduled which will mark the instance as healthy, sleep for half the health check duration (so that we guarantee that the health check runs in time), and then yield the instance if a block is given. This repeats indefinitely.

If a timeout is not specified, the instance is marked as healthy immediately and no task is created. Any block given is ignored.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/async/service/managed/health_checker.rb', line 21

def health_checker(instance, timeout = @evaluator.health_check_timeout, parent: Async::Task.current, &block)
	if timeout
		parent.async(transient: true) do
			while true
				instance.healthy!
				
				sleep(timeout / 2.0)
				
				if block_given?
					yield(instance)
				end
			end
		end
	else
		instance.healthy!
	end
end