class Ant::DRY::Daemon

Use this for running tasks that will be looping by only sending a lambda TODO: Improve this class.

Public Class Methods

new(wait_time, attach, retry_on_failure = false) { || ... } click to toggle source
# File lib/ant/dry/daemon.rb, line 16
def initialize(wait_time, attach, retry_on_failure = false)
  @proc = -> { yield }
  @wait_time = wait_time
  @attach = attach
  @retry_on_failure = retry_on_failure
  @finish = false
end

Public Instance Methods

await() click to toggle source

:nocov: #

# File lib/ant/dry/daemon.rb, line 51
def await
  @thread&.join
end
run() click to toggle source
# File lib/ant/dry/daemon.rb, line 40
def run
  if @attach
    task
  else
    # :nocov: #
    @thread = Thread.new { task }
    # :nocov: #
  end
end
task() click to toggle source
# File lib/ant/dry/daemon.rb, line 24
def task
  log_info 'starting daemon'
  loop do
    begin
      @proc.call
    rescue StandardError => ex
      raise unless @retry_on_failure

      # :nocov: #
      log_error('Unexpected error', error: ex)
      # :nocov: #
    end
    sleep(@wait_time)
  end
end