Module: Async::Service::HealthChecker

Included in:
ManagedService
Defined in:
lib/async/service/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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/async/service/health_checker.rb', line 20

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
        
        # We deliberately create a fiber here, to confirm that fiber creation is working.
        # If something has gone wrong with fiber allocation, we will crash here, and that's okay.
        Fiber.new do
          instance.ready!
        end.resume
        
        sleep(timeout / 2)
      end
    end
  else
    if block_given?
      yield(instance)
    end
    
    instance.ready!
  end
end