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
31
32
33
34
35
36
37
# 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]
    on_error_release_lock = job[:on_error_release_lock]
    logger = job[:logger]
    logger_name = job[:logger_name]

    mutex = @mutexes[task_id]

    logger.error "#{logger_name}: #{error}"
    logger.debug "#{logger_name}: #{error.backtrace.join("\n")}"
    logger.debug "#{logger_name}: Suspending #{logger_name} for #{error_sleep_time} seconds"

    # Unlock the job mutex if the job owns it and on_error_release_lock is true
    mutex.unlock if on_error_release_lock && mutex.owned?

    sleep error_sleep_time

    logger.debug "#{logger_name}: Resuming #{logger_name}"
  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



89
90
91
92
93
94
95
96
# File 'lib/daemon_runner/client.rb', line 89

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.



79
80
81
82
83
84
85
86
# File 'lib/daemon_runner/client.rb', line 79

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

#on_error_release_lockBoolean

Returns Whether to release a mutex lock if an error occurs in a task.

Returns:

  • (Boolean)

    Whether to release a mutex lock if an error occurs in a task.



69
70
71
72
73
74
75
76
# File 'lib/daemon_runner/client.rb', line 69

def on_error_release_lock
  return @on_error_release_lock unless @on_error_release_lock.nil?
  @on_error_release_lock = if options[:on_error_release_lock].nil?
                    true
                  else
                    options[:on_error_release_lock]
                  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



99
100
101
102
103
104
105
106
# File 'lib/daemon_runner/client.rb', line 99

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.



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

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)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/daemon_runner/client.rb', line 110

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)


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

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.



42
43
# File 'lib/daemon_runner/client.rb', line 42

def wait
end