Class: Tasque::Processor
- Inherits:
-
Object
- Object
- Tasque::Processor
- Defined in:
- lib/tasque/processor.rb
Instance Attribute Summary collapse
-
#timers ⇒ Object
readonly
Returns the value of attribute timers.
Instance Method Summary collapse
- #add_handler(type, &block) ⇒ Object
-
#initialize ⇒ Processor
constructor
A new instance of Processor.
- #start ⇒ Object
Constructor Details
#initialize ⇒ Processor
Returns a new instance of Processor.
7 8 9 10 11 12 |
# File 'lib/tasque/processor.rb', line 7 def initialize @timers = Timers::Group.new @last_task = false @current_task = nil @handlers = {} end |
Instance Attribute Details
#timers ⇒ Object (readonly)
Returns the value of attribute timers.
5 6 7 |
# File 'lib/tasque/processor.rb', line 5 def timers @timers end |
Instance Method Details
#add_handler(type, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/tasque/processor.rb', line 14 def add_handler type, &block @handlers[type.to_sym] = Proc.new do |task| @current_task = task task.process begin task.result = block.call task task.complete rescue Tasque::TaskCancel => e task.cancel rescue Tasque::TaskError => e task.result = { task_error: e.task_error } task.failure rescue Exception => e task.result = { exception: e., backtrace: e.backtrace } task.failure end @current_task = nil end @timers.every(check_interval) do begin has_task = Tasque::Task.fetch(type) do |task| @handlers[type.to_sym].call(task) end rescue ActiveRecord::Deadlocked, ActiveRecord::LockWaitTimeout, RedisMutex::LockError retry end while has_task end end |
#start ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/tasque/processor.rb', line 48 def start shutdown = ->(signo) { if @last_task unless @current_task.nil? @current_task.failure @current_task.reprocess end exit! end @last_task = true } trap("SIGINT", shutdown) trap("SIGTERM", shutdown) if Tasque.config.heartbeat && defined?(Insque) heartbeat_thread = Thread.new do heartbeat_timers = Timers::Group.new heartbeat_timers.every(Tasque.config.heartbeat_interval) do = { worker: Tasque.config.worker, busy: !@current_task.nil?, current_task: @current_task.try(:id) }.merge(Tasque.config.heartbeat_payload) Insque.broadcast :heartbeat, end loop do heartbeat_timers.wait end end heartbeat_thread.abort_on_exception = true end loop do break if @last_task @timers.wait end end |