Class: DaemonRunner::Client

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/daemon_runner/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger, #logger_name

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/daemon_runner/client.rb', line 12

def initialize(options)
  @options = options

  # Set error handling
  # @param [Rufus::Scheduler::Job] job job that raised the error
  # @param [RuntimeError] error the body of the error
  def scheduler.on_error(job, error)
    error_sleep_time = job[:error_sleep_time]
    logger = job[:logger]
    task_id = job[:task_id]

    logger.error "#{task_id}: #{error}"
    logger.debug "#{task_id}: Suspending #{task_id} for #{error_sleep_time} seconds"
    job.pause
    sleep error_sleep_time
    logger.debug "#{task_id}: Resuming #{task_id}"
    job.resume
  end
end

Instance Attribute Details

#optionsObject (readonly)

Options hash



9
10
11
# File 'lib/daemon_runner/client.rb', line 9

def options
  @options
end

Instance Method Details

#error_sleep_timeFixnum

Returns Number of seconds to sleep before retrying an error.

Returns:

  • (Fixnum)

    Number of seconds to sleep before retrying an error



72
73
74
75
76
77
78
79
# File 'lib/daemon_runner/client.rb', line 72

def error_sleep_time
  return @error_sleep_time unless @error_sleep_time.nil?
  @error_sleep_time = if options[:error_sleep_time].nil?
                        5
                      else
                        options[:error_sleep_time]
                      end
end

#loop_sleep_timeFixnum

Returns Number of seconds to sleep between loop interactions.

Returns:

  • (Fixnum)

    Number of seconds to sleep between loop interactions.



62
63
64
65
66
67
68
69
# File 'lib/daemon_runner/client.rb', line 62

def loop_sleep_time
  return @loop_sleep_time unless @loop_sleep_time.nil?
  @loop_sleep_time = if options[:loop_sleep_time].nil?
                       5
                     else
                       options[:loop_sleep_time]
                     end
end

#post_task_sleep_timeFixnum

Returns Number of seconds to sleep after each task.

Returns:

  • (Fixnum)

    Number of seconds to sleep after each task



82
83
84
85
86
87
88
89
# File 'lib/daemon_runner/client.rb', line 82

def post_task_sleep_time
  return @post_task_sleep_time unless @post_task_sleep_time.nil?
  @post_task_sleep_time = if options[:post_task_sleep_time].nil?
                            1
                          else
                            options[:post_task_sleep_time]
                          end
end

#scheduleArray<Symbol, String/Fixnum>

Returns Schedule tuple-like with the type of schedule and its timing.

Returns:

  • (Array<Symbol, String/Fixnum>)

    Schedule tuple-like with the type of schedule and its timing.



55
56
57
58
59
# File 'lib/daemon_runner/client.rb', line 55

def schedule
  # The default type is an `interval` which trigger, execute and then trigger again after
  # the interval has elapsed.
  [:interval, loop_sleep_time]
end

#start!nil

Start the service

Returns:

  • (nil)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/daemon_runner/client.rb', line 93

def start!
  wait

  logger.warn 'Tasks list is empty' if tasks.empty?
  tasks.each do |task|
    run_task(task)
    sleep post_task_sleep_time
  end

  scheduler.join
rescue SystemExit, Interrupt
  logger.info 'Shutting down'
  scheduler.shutdown
end

#tasksArray<Array>

This method is abstract.

Override #tasks in a subclass.

List of tasks that get executed in #start!

Examples:

Example tasks method

def tasks
  [
    [::MyService::Tasks::Foo.new, 'run!'],
    [::MyService::Tasks::Bar.new, 'run!', 'bar'],
    [::MyService::Tasks::Baz, 'run!', 'baz', 'because']
  ]
end

Returns:

  • (Array<Array>)

Raises:

  • (NotImplementedError)


49
50
51
52
# File 'lib/daemon_runner/client.rb', line 49

def tasks
  raise NotImplementedError, 'Must implement this in a subclass.  \
  This must be an array of methods for the runner to call'
end

#waitvoid

This method is abstract.

Override #wait to pause before starting.

This method returns an undefined value.

Hook to allow initial setup tasks before running tasks.



35
36
# File 'lib/daemon_runner/client.rb', line 35

def wait
end