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.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mini_scheduler/schedule_info.rb', line 9 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.
3 4 5 |
# File 'lib/mini_scheduler/schedule_info.rb', line 3 def current_owner @current_owner end |
#next_run ⇒ Object
Returns the value of attribute next_run.
3 4 5 |
# File 'lib/mini_scheduler/schedule_info.rb', line 3 def next_run @next_run end |
#prev_duration ⇒ Object
Returns the value of attribute prev_duration.
3 4 5 |
# File 'lib/mini_scheduler/schedule_info.rb', line 3 def prev_duration @prev_duration end |
#prev_result ⇒ Object
Returns the value of attribute prev_result.
3 4 5 |
# File 'lib/mini_scheduler/schedule_info.rb', line 3 def prev_result @prev_result end |
#prev_run ⇒ Object
Returns the value of attribute prev_run.
3 4 5 |
# File 'lib/mini_scheduler/schedule_info.rb', line 3 def prev_run @prev_run end |
Instance Method Details
#del! ⇒ Object
106 107 108 109 |
# File 'lib/mini_scheduler/schedule_info.rb', line 106 def del! clear! @next_run = @prev_run = @prev_result = @prev_duration = @current_owner = nil end |
#key ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/mini_scheduler/schedule_info.rb', line 111 def key if @klass.is_per_host Manager.schedule_key(@klass, @manager.hostname) else Manager.schedule_key(@klass) end end |
#queue_key ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/mini_scheduler/schedule_info.rb', line 119 def queue_key if @klass.is_per_host Manager.queue_key(@manager.hostname) else Manager.queue_key end end |
#redis ⇒ Object
127 128 129 |
# File 'lib/mini_scheduler/schedule_info.rb', line 127 def redis @manager.redis end |
#schedule! ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/mini_scheduler/schedule_info.rb', line 82 def schedule! if @klass.every schedule_every! elsif @klass.daily schedule_daily! end write! end |
#schedule_daily! ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mini_scheduler/schedule_info.rb', line 66 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
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mini_scheduler/schedule_info.rb', line 54 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 if !valid? @next_run = Time.now.to_i + 5.minutes * 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.
34 35 36 37 |
# File 'lib/mini_scheduler/schedule_info.rb', line 34 def valid? return false unless @next_run (!@prev_run && @next_run < Time.now.to_i + 5.minutes) || valid_every? || valid_daily? end |
#valid_daily? ⇒ Boolean
46 47 48 49 50 51 52 |
# File 'lib/mini_scheduler/schedule_info.rb', line 46 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
39 40 41 42 43 44 |
# File 'lib/mini_scheduler/schedule_info.rb', line 39 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
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mini_scheduler/schedule_info.rb', line 92 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 |