Class: ProconBypassMan::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/procon_bypass_man/worker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pidObject

Returns the value of attribute pid.



3
4
5
# File 'lib/procon_bypass_man/worker.rb', line 3

def pid
  @pid
end

Class Method Details

.runObject



5
6
7
# File 'lib/procon_bypass_man/worker.rb', line 5

def self.run
  new.run
end

Instance Method Details

#runWorker

Returns:



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/procon_bypass_man/worker.rb', line 10

def run
  return self if @thread
  @thread = Thread.new do
    while(item = ProconBypassMan::Background::JobQueue.pop)
      # プロセスを越えるので文字列になっている. evalしてクラスにする
      work(job_class: eval(item[:job_class]), args: item[:args])
      sleep(0.2) # busy loopしないように
    end
  end

  return self
end

#shutdownObject

重要な非同期ジョブは存在しないのでqueueが捌けるのを待たずにkill



36
37
38
# File 'lib/procon_bypass_man/worker.rb', line 36

def shutdown
  @thread&.kill
end

#work(job_class:, args:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/procon_bypass_man/worker.rb', line 23

def work(job_class: , args: )
  begin
    job_class.perform(*args)
  rescue => e
    ProconBypassMan.logger.error(e)
    if job_class.respond_to?(:re_enqueue_if_failed) && job_class.re_enqueue_if_failed
      job_class.perform_async(args)
      ProconBypassMan.logger.error("エラーが起きたので#{job_class}を積み直しました。")
    end
  end
end