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.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mini_scheduler/schedule_info.rb', line 11 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 # 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
110 111 112 113 |
# File 'lib/mini_scheduler/schedule_info.rb', line 110 def del! clear! @next_run = @prev_run = @prev_result = @prev_duration = @current_owner = nil end |
#key ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/mini_scheduler/schedule_info.rb', line 115 def key if @klass.is_per_host Manager.schedule_key(@klass, @manager.hostname) else Manager.schedule_key(@klass) end end |
#queue_key ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/mini_scheduler/schedule_info.rb', line 123 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
131 132 133 |
# File 'lib/mini_scheduler/schedule_info.rb', line 131 def redis @manager.redis end |
#schedule! ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/mini_scheduler/schedule_info.rb', line 87 def schedule! if @klass.every schedule_every! elsif @klass.daily schedule_daily! end write! end |
#schedule_daily! ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/mini_scheduler/schedule_info.rb', line 71 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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mini_scheduler/schedule_info.rb', line 56 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 if !valid? @next_run = Time.now.to_i + 300 * Random.rand end 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.
36 37 38 39 |
# File 'lib/mini_scheduler/schedule_info.rb', line 36 def valid? return false unless @next_run (!@prev_run && @next_run < Time.now.to_i + 300) || valid_every? || valid_daily? end |
#valid_daily? ⇒ Boolean
48 49 50 51 52 53 54 |
# File 'lib/mini_scheduler/schedule_info.rb', line 48 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
41 42 43 44 45 46 |
# File 'lib/mini_scheduler/schedule_info.rb', line 41 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
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mini_scheduler/schedule_info.rb', line 97 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, @klass if @next_run end |