Class: MiniScheduler::Manager::Runner
- Inherits:
-
Object
- Object
- MiniScheduler::Manager::Runner
- Defined in:
- lib/mini_scheduler/manager.rb
Instance Method Summary collapse
- #attempts(max_attempts) ⇒ Object
- #current_worker_thread_id ⇒ Object
- #enq(klass) ⇒ Object
- #ensure_worker_threads ⇒ Object
- #hostname ⇒ Object
-
#initialize(manager) ⇒ Runner
constructor
A new instance of Runner.
- #keep_alive(*ids) ⇒ Object
- #process_queue ⇒ Object
- #repair_queue ⇒ Object
- #reschedule_orphans ⇒ Object
- #set_current_worker_thread_id! ⇒ Object
- #stop! ⇒ Object
- #wait_till_done ⇒ Object
- #worker_loop ⇒ Object
- #worker_thread_ids ⇒ Object
Constructor Details
#initialize(manager) ⇒ Runner
Returns a new instance of Runner.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mini_scheduler/manager.rb', line 8 def initialize(manager) @stopped = false @mutex = Mutex.new @queue = Queue.new @manager = manager @hostname = manager.hostname @recovery_thread = Thread.new do while !@stopped sleep 60 @mutex.synchronize do repair_queue reschedule_orphans ensure_worker_threads end end end @keep_alive_thread = Thread.new do while !@stopped @mutex.synchronize { keep_alive } sleep(@manager.keep_alive_duration / 2) end end ensure_worker_threads end |
Instance Method Details
#attempts(max_attempts) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/mini_scheduler/manager.rb', line 211 def attempts(max_attempts) attempt = 0 begin yield rescue StandardError attempt += 1 raise if attempt >= max_attempts sleep Random.rand retry end end |
#current_worker_thread_id ⇒ Object
93 94 95 |
# File 'lib/mini_scheduler/manager.rb', line 93 def current_worker_thread_id Thread.current[:mini_scheduler_worker_thread_id] end |
#enq(klass) ⇒ Object
199 200 201 |
# File 'lib/mini_scheduler/manager.rb', line 199 def enq(klass) @queue << klass end |
#ensure_worker_threads ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mini_scheduler/manager.rb', line 60 def ensure_worker_threads @threads ||= [] @threads.delete_if { |t| !t.alive? } (@manager.workers - @threads.size).times { @threads << Thread.new { worker_loop } } rescue => ex MiniScheduler.handle_job_exception( ex, message: "Error during MiniScheduler ensure_worker_threads", ) end |
#hostname ⇒ Object
89 90 91 |
# File 'lib/mini_scheduler/manager.rb', line 89 def hostname @hostname end |
#keep_alive(*ids) ⇒ Object
39 40 41 42 43 |
# File 'lib/mini_scheduler/manager.rb', line 39 def keep_alive(*ids) @manager.keep_alive(*ids) rescue => ex MiniScheduler.handle_job_exception(ex, message: "Error during MiniScheduler keep_alive") end |
#process_queue ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/mini_scheduler/manager.rb', line 107 def process_queue klass = @queue.deq # hack alert, I need to both deq and set @running atomically. @running = true return if !klass failed = false start = Time.now.to_f info = @mutex.synchronize { @manager.schedule_info(klass) } stat = nil error = nil begin info.prev_result = "RUNNING" info.current_owner = current_worker_thread_id @mutex.synchronize { info.write! } if @manager.enable_stats stat = MiniScheduler::Stat.create!( name: klass.to_s, hostname: hostname, pid: Process.pid, started_at: Time.now, live_slots_start: GC.stat[:heap_live_slots], ) end klass.new.perform rescue => e MiniScheduler.handle_job_exception( e, message: "Error while running a scheduled job", job: { "class" => klass, }, ) error = "#{e.class}: #{e.} #{e.backtrace.join("\n")}" failed = true end duration = ((Time.now.to_f - start) * 1000).to_i info.prev_duration = duration info.prev_result = failed ? "FAILED" : "OK" info.current_owner = nil if stat stat.update!( duration_ms: duration, live_slots_finish: GC.stat[:heap_live_slots], success: !failed, error: error, ) MiniScheduler.job_ran&.call(stat) end attempts(3) { @mutex.synchronize { info.write! } } ensure @running = false if defined?(ActiveRecord::Base) ActiveRecord::Base.connection_handler.clear_active_connections! end end |
#repair_queue ⇒ Object
45 46 47 48 49 |
# File 'lib/mini_scheduler/manager.rb', line 45 def repair_queue @manager.repair_queue rescue => ex MiniScheduler.handle_job_exception(ex, message: "Error during MiniScheduler repair_queue") end |
#reschedule_orphans ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/mini_scheduler/manager.rb', line 51 def reschedule_orphans @manager.reschedule_orphans! rescue => ex MiniScheduler.handle_job_exception( ex, message: "Error during MiniScheduler reschedule_orphans", ) end |
#set_current_worker_thread_id! ⇒ Object
97 98 99 100 101 |
# File 'lib/mini_scheduler/manager.rb', line 97 def set_current_worker_thread_id! Thread.current[ :mini_scheduler_worker_thread_id ] = "#{@manager.identity_key}:thread_#{SecureRandom.alphanumeric(10)}" end |
#stop! ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mini_scheduler/manager.rb', line 173 def stop! return if @stopped @mutex.synchronize do @stopped = true @keep_alive_thread.kill @recovery_thread.kill @keep_alive_thread.join @recovery_thread.join enq(nil) kill_thread = Thread.new do sleep 0.5 @threads.each(&:kill) end @threads.each(&:join) kill_thread.kill kill_thread.join end end |
#wait_till_done ⇒ Object
203 204 205 206 207 208 209 |
# File 'lib/mini_scheduler/manager.rb', line 203 def wait_till_done sleep 0.001 while !@queue.empty? && !(@queue.num_waiting > 0) # this is a hack, but is only used for test anyway # if tests fail that depend on this we are forced to increase it. sleep 0.010 sleep 0.001 while @running end |
#worker_loop ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mini_scheduler/manager.rb', line 71 def worker_loop set_current_worker_thread_id! keep_alive(current_worker_thread_id) while !@stopped begin process_queue rescue => ex MiniScheduler.handle_job_exception( ex, message: "Error during MiniScheduler worker_loop", ) break # Data could be in a bad state - stop the thread end end end |
#worker_thread_ids ⇒ Object
103 104 105 |
# File 'lib/mini_scheduler/manager.rb', line 103 def worker_thread_ids @threads.filter(&:alive?).filter_map { |t| t[:mini_scheduler_worker_thread_id] } end |