Class: Ant::DRY::Daemon

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/ant/dry/daemon.rb

Overview

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

  • Add a maechanism for killing the process

  • Add a function to test how many threads are alive

  • Make it testable

  • Add unit tests

Instance Method Summary collapse

Constructor Details

#initialize(wait_time, attach, retry_on_failure = false) ⇒ Daemon

Returns a new instance of Daemon.



16
17
18
19
20
21
22
# 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

Instance Method Details

#awaitObject

:nocov: #



51
52
53
# File 'lib/ant/dry/daemon.rb', line 51

def await
  @thread&.join
end

#runObject



40
41
42
43
44
45
46
47
48
# File 'lib/ant/dry/daemon.rb', line 40

def run
  if @attach
    task
  else
    # :nocov: #
    @thread = Thread.new { task }
    # :nocov: #
  end
end

#taskObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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