Class: DaemonRunner::Client
- Inherits:
-
Object
- Object
- DaemonRunner::Client
- Includes:
- Logger
- Defined in:
- lib/daemon_runner/client.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Options hash.
Instance Method Summary collapse
-
#error_sleep_time ⇒ Fixnum
Number of seconds to sleep before retrying an error.
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
-
#loop_sleep_time ⇒ Fixnum
Number of seconds to sleep between loop interactions.
-
#on_error_release_lock ⇒ Boolean
Whether to release a mutex lock if an error occurs in a task.
-
#post_task_sleep_time ⇒ Fixnum
Number of seconds to sleep after each task.
-
#schedule ⇒ Array<Symbol, String/Fixnum>
Schedule tuple-like with the type of schedule and its timing.
-
#start! ⇒ nil
Start the service.
-
#tasks ⇒ Array<Array>
abstract
List of tasks that get executed in #start!.
-
#wait ⇒ void
abstract
Hook to allow initial setup tasks before running tasks.
Methods included from Logger
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 = # 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
#options ⇒ Object (readonly)
Options hash
9 10 11 |
# File 'lib/daemon_runner/client.rb', line 9 def @options end |
Instance Method Details
#error_sleep_time ⇒ Fixnum
Returns 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 [:error_sleep_time].nil? 5 else [:error_sleep_time] end end |
#loop_sleep_time ⇒ Fixnum
Returns 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 [:loop_sleep_time].nil? 5 else [:loop_sleep_time] end end |
#on_error_release_lock ⇒ Boolean
Returns 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 [:on_error_release_lock].nil? true else [:on_error_release_lock] end end |
#post_task_sleep_time ⇒ Fixnum
Returns 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 [:post_task_sleep_time].nil? 1 else [:post_task_sleep_time] end end |
#schedule ⇒ Array<Symbol, String/Fixnum>
Returns 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
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 |
#tasks ⇒ Array<Array>
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 |
#wait ⇒ void
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 |