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.
-
#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 |
# 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] 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
#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.
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 [:error_sleep_time].nil? 5 else [:error_sleep_time] end end |
#loop_sleep_time ⇒ Fixnum
Returns 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 [:loop_sleep_time].nil? 5 else [:loop_sleep_time] end end |
#post_task_sleep_time ⇒ Fixnum
Returns 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 [: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.
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
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 |
#tasks ⇒ Array<Array>
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 |
#wait ⇒ void
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 |