Module: Wakame::StatusDB

Defined in:
lib/wakame/status_db.rb

Defined Under Namespace

Classes: Model, SequelAdapter, WorkerThread

Class Method Summary collapse

Class Method Details

.adapterObject



98
99
100
# File 'lib/wakame/status_db.rb', line 98

def self.adapter
  @adapter ||= SequelAdapter.new
end

.barrier(tout = 60*2, &blk) ⇒ Object



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
47
48
49
50
51
52
53
54
55
# File 'lib/wakame/status_db.rb', line 16

def self.barrier(tout=60*2, &blk)
  abort "Cant use barrier() in side of the EventMachine thread." if Kernel.const_defined?(:EventMachine) && ::EventMachine.reactor_thread?

  if Thread.current == WorkerThread.worker_thread
    return blk.call
  end
  
  Wakame.log.debug("StatusDB.barrier: Called at #{caller(1)[0..1].inspect} on the thread #{Thread.current}.")

  q = ::Queue.new
  time_start = ::Time.now
  
  self.pass {
    begin
      res = blk.call
      q << [true, res]
    rescue => e
      q << [false, e]
    end
  }

  res = nil
  begin
    timeout(tout) do
      res = q.shift
    end
  rescue Timeout::Error => e
    Wakame.log.error("WorkerThread.queue.size=#{WorkerThread.queue.size}")
    Wakame.log.error(e)
    raise e
  end

  time_elapsed = ::Time.now - time_start
  Wakame.log.debug("#{self}.barrier: Elapsed time for #{blk}: #{time_elapsed} sec") if time_elapsed > 0.05

  if res[0] == false && res[1].is_a?(Exception)
    raise res[1]
  end
  res[1]
end

.pass(&blk) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/wakame/status_db.rb', line 8

def self.pass(&blk)
  if Thread.current == WorkerThread.worker_thread
    blk.call
  else
    WorkerThread.queue.enq(blk)
  end
end