class Stackify::Scheduler

Attributes

iterations[R]
period[RW]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

attemps_are_not_over?() click to toggle source
# File lib/stackify/scheduler.rb, line 56
def attemps_are_not_over?
  @attempts.nil? || @attempts > 0
end
keep_running?() click to toggle source
# 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?() click to toggle source
# File lib/stackify/scheduler.rb, line 60
def limit_is_not_reached?
  @task.limit.nil? || @iterations < @task.limit
end
run(period=nil, task) click to toggle source
# 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() click to toggle source
# File lib/stackify/scheduler.rb, line 72
def run_task
  begin
    @task.execute!
  rescue Exception => e
    ::Stackify.log_internal_error 'Scheduler: ' + e.message + '' + e.backtrace.to_s
  end
end
schedule_next_invocation() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/stackify/scheduler.rb, line 64
def stop
  @should_run = false
end
task_result() click to toggle source
# File lib/stackify/scheduler.rb, line 68
def task_result
  @task_result
end