Class: Stackify::Scheduler

Inherits:
Object show all
Defined in:
lib/stackify/scheduler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScheduler

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

#iterationsObject (readonly)

Returns the value of attribute iterations.



7
8
9
# File 'lib/stackify/scheduler.rb', line 7

def iterations
  @iterations
end

#periodObject

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

Returns:

  • (Boolean)


56
57
58
# File 'lib/stackify/scheduler.rb', line 56

def attemps_are_not_over?
  @attempts.nil? || @attempts > 0
end

#keep_running?Boolean

Returns:

  • (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

Returns:

  • (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_taskObject



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.message + '' + e.backtrace.to_s
  end
end

#schedule_next_invocationObject



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

#stopObject



64
65
66
# File 'lib/stackify/scheduler.rb', line 64

def stop
  @should_run = false
end

#task_resultObject



68
69
70
# File 'lib/stackify/scheduler.rb', line 68

def task_result
  @task_result
end