Class: Stackify::Scheduler
Instance Attribute Summary collapse
-
#iterations ⇒ Object
readonly
Returns the value of attribute iterations.
-
#period ⇒ Object
Returns the value of attribute period.
Instance Method Summary collapse
- #attemps_are_not_over? ⇒ Boolean
-
#initialize ⇒ Scheduler
constructor
A new instance of Scheduler.
- #keep_running? ⇒ Boolean
- #limit_is_not_reached? ⇒ Boolean
- #run(period = nil, task) ⇒ Object
- #run_task ⇒ Object
- #schedule_next_invocation ⇒ Object
- #setup(period, task) ⇒ Object
- #stop ⇒ Object
- #task_result ⇒ Object
Constructor Details
#initialize ⇒ Scheduler
Returns a new instance of Scheduler.
9 10 11 12 13 14 15 |
# File 'lib/stackify/scheduler.rb', line 9 def initialize @should_run = true @next_invocation_time = Time.now @period = ScheduleDelay.new @iterations = 0 @attempts = 3 end |
Instance Attribute Details
#iterations ⇒ Object (readonly)
Returns the value of attribute iterations.
7 8 9 |
# File 'lib/stackify/scheduler.rb', line 7 def iterations @iterations end |
#period ⇒ Object
Returns the value of attribute period.
6 7 8 |
# File 'lib/stackify/scheduler.rb', line 6 def period @period end |
Instance Method Details
#attemps_are_not_over? ⇒ Boolean
56 57 58 |
# File 'lib/stackify/scheduler.rb', line 56 def attemps_are_not_over? @attempts.nil? || @attempts > 0 end |
#keep_running? ⇒ Boolean
52 53 54 |
# File 'lib/stackify/scheduler.rb', line 52 def keep_running? @should_run && limit_is_not_reached? && attemps_are_not_over? end |
#limit_is_not_reached? ⇒ Boolean
60 61 62 |
# File 'lib/stackify/scheduler.rb', line 60 def limit_is_not_reached? @task.limit.nil? || @iterations < @task.limit end |
#run(period = nil, task) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/stackify/scheduler.rb', line 27 def run(period=nil, task) setup(period, task) while keep_running? do sleep_time = schedule_next_invocation sleep(sleep_time) if sleep_time > 0 && @iterations != 0 @task_result = run_task if keep_running? if @task.success? @task_result @iterations += 1 @attempts = @task.attempts || @attempts else @period.update_by_exeption!(@task_result) @attempts -= 1 end end @task.success? @task_result end |
#run_task ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/stackify/scheduler.rb', line 72 def run_task begin @task.execute! rescue Exception => e ::Stackify.log_internal_error 'Scheduler: ' + e. + '' + e.backtrace.to_s end end |
#schedule_next_invocation ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/stackify/scheduler.rb', line 44 def schedule_next_invocation now = Time.now while @next_invocation_time <= now && @period.to_sec > 0 @next_invocation_time += @period.to_sec end @next_invocation_time - Time.now end |
#setup(period, task) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/stackify/scheduler.rb', line 17 def setup(period, task) @task = task @period = period if period @should_run = true @iterations = 0 @attempts = @task.attempts if @task.attempts now = Time.now @next_invocation_time = (now + @period.to_sec) end |
#stop ⇒ Object
64 65 66 |
# File 'lib/stackify/scheduler.rb', line 64 def stop @should_run = false end |
#task_result ⇒ Object
68 69 70 |
# File 'lib/stackify/scheduler.rb', line 68 def task_result @task_result end |