Class: Narou::Worker
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Class Method Summary collapse
- .cancel ⇒ Object
- .canceled? ⇒ Boolean
- .push(&block) ⇒ Object
-
.push_as_system_worker(&block) ⇒ Object
システム用のワーカー追加。内部カウントは増やさない.
Instance Method Summary collapse
- #cancel ⇒ Object
- #canceled? ⇒ Boolean
- #countdown ⇒ Object
- #countup ⇒ Object
-
#initialize ⇒ Worker
constructor
A new instance of Worker.
- #notification_queue ⇒ Object
- #push(counting = true, &block) ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize ⇒ Worker
Returns a new instance of Worker.
14 15 16 17 18 19 20 21 22 |
# File 'lib/web/worker.rb', line 14 def initialize @queue = Queue.new @size = 0 @mutex = Mutex.new @worker_thread = nil @push_server = Narou::PushServer.instance @cancel_signal = false @thread_of_block_executing = nil end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
12 13 14 |
# File 'lib/web/worker.rb', line 12 def size @size end |
Class Method Details
.cancel ⇒ Object
64 65 66 |
# File 'lib/web/worker.rb', line 64 def self.cancel instance.cancel end |
.canceled? ⇒ Boolean
79 80 81 |
# File 'lib/web/worker.rb', line 79 def self.canceled? instance.canceled? end |
.push(&block) ⇒ Object
99 100 101 |
# File 'lib/web/worker.rb', line 99 def self.push(&block) instance.push(&block) end |
.push_as_system_worker(&block) ⇒ Object
システム用のワーカー追加。内部カウントは増やさない
95 96 97 |
# File 'lib/web/worker.rb', line 95 def self.push_as_system_worker(&block) instance.push(false, &block) end |
Instance Method Details
#cancel ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/web/worker.rb', line 68 def cancel @mutex.synchronize do if @size > 0 @cancel_signal = true @size = 0 @thread_of_block_executing.raise(Interrupt) if @thread_of_block_executing Thread.pass end end end |
#canceled? ⇒ Boolean
83 84 85 |
# File 'lib/web/worker.rb', line 83 def canceled? @cancel_signal end |
#countdown ⇒ Object
119 120 121 122 123 124 |
# File 'lib/web/worker.rb', line 119 def countdown @mutex.synchronize do @size -= 1 @size = 0 if @size < 0 end end |
#countup ⇒ Object
113 114 115 116 117 |
# File 'lib/web/worker.rb', line 113 def countup @mutex.synchronize do @size += 1 end end |
#notification_queue ⇒ Object
109 110 111 |
# File 'lib/web/worker.rb', line 109 def notification_queue @push_server.send_all("notification.queue" => @size) end |
#push(counting = true, &block) ⇒ Object
103 104 105 106 107 |
# File 'lib/web/worker.rb', line 103 def push(counting = true, &block) countup if counting notification_queue @queue.push(block: block, counting: counting) end |
#running? ⇒ Boolean
24 25 26 |
# File 'lib/web/worker.rb', line 24 def running? !@worker_thread.! end |
#start ⇒ Object
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 56 57 58 59 60 61 62 |
# File 'lib/web/worker.rb', line 28 def start return if running? @worker_thread = Thread.new do loop do begin q = @queue.pop if canceled? @queue.clear @cancel_signal = false else #q[:block].call @thread_of_block_executing = Thread.new do q[:block].call end @thread_of_block_executing.tap do |th| th.join th = nil end end rescue SystemExit rescue Exception => e # Workerスレッド内での例外は表示するだけしてスレッドは生かしたままにする STDOUT.puts $@.shift + ": #{e.} (#{e.class})" $@.each do |b| STDOUT.puts " from #{b}" end ensure if q[:counting] countdown notification_queue end end end end end |
#stop ⇒ Object
87 88 89 90 |
# File 'lib/web/worker.rb', line 87 def stop @worker_thread.kill if @worker_thread @worker_thread = nil end |