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 yield the instance if a block is given, then mark the instance as ready, and finally sleep for half the health check duration (so that we guarantee that the health check runs in time).

If a timeout is not specified, the health checker will yield the instance immediately and then mark the instance as ready.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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
				if block_given?
					yield(instance)
				end
				
				instance.healthy!
				
				sleep(timeout / 2.0)
			end
		end
	else
		if block_given?
			yield(instance)
		end
		
		instance.healthy!
	end
end