Class: MiniScheduler::ScheduleInfo
- Inherits:
-
Object
- Object
- MiniScheduler::ScheduleInfo
- Defined in:
- lib/mini_scheduler/schedule_info.rb
Instance Attribute Summary collapse
-
#current_owner ⇒ Object
Returns the value of attribute current_owner.
-
#next_run ⇒ Object
Returns the value of attribute next_run.
-
#prev_duration ⇒ Object
Returns the value of attribute prev_duration.
-
#prev_result ⇒ Object
Returns the value of attribute prev_result.
-
#prev_run ⇒ Object
Returns the value of attribute prev_run.
Instance Method Summary collapse
- #del! ⇒ Object
-
#initialize(klass, manager) ⇒ ScheduleInfo
constructor
A new instance of ScheduleInfo.
- #key ⇒ Object
- #queue_key ⇒ Object
- #redis ⇒ Object
- #schedule! ⇒ Object
- #schedule_daily! ⇒ Object
- #schedule_every! ⇒ Object
-
#valid? ⇒ Boolean
this means the schedule is going to fire, it is setup correctly.
- #valid_daily? ⇒ Boolean
- #valid_every? ⇒ Boolean
- #write! ⇒ Object
Constructor Details
#initialize(klass, manager) ⇒ ScheduleInfo
Returns a new instance of ScheduleInfo.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mini_scheduler/schedule_info.rb', line 7 def initialize(klass, manager) @klass = klass @manager = manager data = nil if data = @manager.redis.get(key) data = JSON.parse(data) end if data @next_run = data["next_run"] @prev_run = data["prev_run"] @prev_result = data["prev_result"] @prev_duration = data["prev_duration"] @current_owner = data["current_owner"] end rescue StandardError # corrupt redis @next_run = @prev_run = @prev_result = @prev_duration = @current_owner = nil end |
Instance Attribute Details
#current_owner ⇒ Object
Returns the value of attribute current_owner.
5 6 7 |
# File 'lib/mini_scheduler/schedule_info.rb', line 5 def current_owner @current_owner end |
#next_run ⇒ Object
Returns the value of attribute next_run.
5 6 7 |
# File 'lib/mini_scheduler/schedule_info.rb', line 5 def next_run @next_run end |
#prev_duration ⇒ Object
Returns the value of attribute prev_duration.
5 6 7 |
# File 'lib/mini_scheduler/schedule_info.rb', line 5 def prev_duration @prev_duration end |
#prev_result ⇒ Object
Returns the value of attribute prev_result.
5 6 7 |
# File 'lib/mini_scheduler/schedule_info.rb', line 5 def prev_result @prev_result end |
#prev_run ⇒ Object
Returns the value of attribute prev_run.
5 6 7 |
# File 'lib/mini_scheduler/schedule_info.rb', line 5 def prev_run @prev_run end |
Instance Method Details
#del! ⇒ Object
102 103 104 105 |
# File 'lib/mini_scheduler/schedule_info.rb', line 102 def del! clear! @next_run = @prev_run = @prev_result = @prev_duration = @current_owner = nil end |
#key ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/mini_scheduler/schedule_info.rb', line 107 def key if @klass.is_per_host Manager.schedule_key(@klass, @manager.hostname) else Manager.schedule_key(@klass) end end |
#queue_key ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/mini_scheduler/schedule_info.rb', line 115 def queue_key if @klass.is_per_host Manager.queue_key(@manager.queue, @manager.hostname) else Manager.queue_key(@manager.queue) end end |
#redis ⇒ Object
123 124 125 |
# File 'lib/mini_scheduler/schedule_info.rb', line 123 def redis @manager.redis end |
#schedule! ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/mini_scheduler/schedule_info.rb', line 78 def schedule! if @klass.every schedule_every! elsif @klass.daily schedule_daily! end write! end |
#schedule_daily! ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/mini_scheduler/schedule_info.rb', line 62 def schedule_daily! return if valid? at = @klass.daily[:at] || 0 today_begin = Time.now.midnight.to_i today_offset = DateTime.now.seconds_since_midnight # If it's later today if at > today_offset @next_run = today_begin + at else # Otherwise do it tomorrow @next_run = today_begin + 1.day + at end end |
#schedule_every! ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mini_scheduler/schedule_info.rb', line 49 def schedule_every! if !valid? && @prev_run mixup = @klass.every * @manager.random_ratio mixup = (mixup * Random.rand - mixup / 2).to_i @next_run = @prev_run + mixup + @klass.every end # this can look a bit confusing, but @next_run above could be off # if prev_run is off, so this ensures it ends up correct and in the # future @next_run = Time.now.to_i + 300 * Random.rand if !valid? end |
#valid? ⇒ Boolean
this means the schedule is going to fire, it is setup correctly. invalid schedules are fixed by running “schedule!” this happens automatically after if fire by the manager.
32 33 34 35 |
# File 'lib/mini_scheduler/schedule_info.rb', line 32 def valid? return false unless @next_run (!@prev_run && @next_run < Time.now.to_i + 300) || valid_every? || valid_daily? end |
#valid_daily? ⇒ Boolean
43 44 45 46 47 |
# File 'lib/mini_scheduler/schedule_info.rb', line 43 def valid_daily? return false unless @klass.daily return true if !@prev_run && @next_run && @next_run <= (Time.now + 1.day).to_i !!@prev_run && @prev_run <= Time.now.to_i && @next_run < @prev_run + 1.day end |
#valid_every? ⇒ Boolean
37 38 39 40 41 |
# File 'lib/mini_scheduler/schedule_info.rb', line 37 def valid_every? return false unless @klass.every !!@prev_run && @prev_run <= Time.now.to_i && @next_run < @prev_run + @klass.every * (1 + @manager.random_ratio) end |
#write! ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mini_scheduler/schedule_info.rb', line 88 def write! clear! redis.set key, { next_run: @next_run, prev_run: @prev_run, prev_duration: @prev_duration, prev_result: @prev_result, current_owner: @current_owner, }.to_json redis.zadd queue_key, @next_run.to_s, @klass.to_s if @next_run end |