Class: Narou::WebWorker
- Inherits:
-
Object
- Object
- Narou::WebWorker
- Includes:
- Mixin::OutputError, Singleton
- Defined in:
- lib/web/web_worker.rb
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
システム用のワーカー追加。内部カウントは増やさない.
- .run ⇒ Object
- .stop ⇒ Object
Instance Method Summary collapse
- #cancel ⇒ Object
- #canceled? ⇒ Boolean
- #countdown ⇒ Object
- #countup ⇒ Object
-
#initialize ⇒ WebWorker
constructor
A new instance of WebWorker.
- #notification_queue ⇒ Object
- #push(counting = true, &block) ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
Methods included from Mixin::OutputError
Constructor Details
#initialize ⇒ WebWorker
Returns a new instance of WebWorker.
22 23 24 25 26 27 28 29 30 |
# File 'lib/web/web_worker.rb', line 22 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.
16 17 18 |
# File 'lib/web/web_worker.rb', line 16 def size @size end |
Class Method Details
.cancel ⇒ Object
65 66 67 |
# File 'lib/web/web_worker.rb', line 65 def self.cancel instance.cancel end |
.canceled? ⇒ Boolean
80 81 82 |
# File 'lib/web/web_worker.rb', line 80 def self.canceled? instance.canceled? end |
.push(&block) ⇒ Object
104 105 106 |
# File 'lib/web/web_worker.rb', line 104 def self.push(&block) instance.push(&block) end |
.push_as_system_worker(&block) ⇒ Object
システム用のワーカー追加。内部カウントは増やさない
100 101 102 |
# File 'lib/web/web_worker.rb', line 100 def self.push_as_system_worker(&block) instance.push(false, &block) end |
.run ⇒ Object
18 19 20 |
# File 'lib/web/web_worker.rb', line 18 def self.run instance.start end |
.stop ⇒ Object
88 89 90 |
# File 'lib/web/web_worker.rb', line 88 def self.stop instance.stop end |
Instance Method Details
#cancel ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/web/web_worker.rb', line 69 def cancel @mutex.synchronize do if @size > 0 @cancel_signal = true @size = 0 @thread_of_block_executing&.raise(Interrupt) end end Thread.pass end |
#canceled? ⇒ Boolean
84 85 86 |
# File 'lib/web/web_worker.rb', line 84 def canceled? @cancel_signal end |
#countdown ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/web/web_worker.rb', line 124 def countdown @mutex.synchronize do @size -= 1 @size = 0 if @size < 0 notification_queue end end |
#countup ⇒ Object
117 118 119 120 121 122 |
# File 'lib/web/web_worker.rb', line 117 def countup @mutex.synchronize do @size += 1 notification_queue end end |
#notification_queue ⇒ Object
113 114 115 |
# File 'lib/web/web_worker.rb', line 113 def notification_queue @push_server.send_all("notification.queue" => [@size, Narou::Worker.size]) end |
#push(counting = true, &block) ⇒ Object
108 109 110 111 |
# File 'lib/web/web_worker.rb', line 108 def push(counting = true, &block) countup if counting @queue.push(block: block, counting: counting) end |
#running? ⇒ Boolean
32 33 34 |
# File 'lib/web/web_worker.rb', line 32 def running? !@worker_thread.! end |
#start ⇒ Object
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 63 |
# File 'lib/web/web_worker.rb', line 36 def start return if running? @worker_thread = Thread.new do loop do begin q = @queue.pop if canceled? @queue.clear @cancel_signal = false else @thread_of_block_executing = Thread.new do q[:block].call end @thread_of_block_executing.join @thread_of_block_executing = nil end rescue SystemExit rescue Exception => e # WebWorkerスレッド内での例外は表示するだけしてスレッドは生かしたままにする output_error($stdout, e) ensure if q && q[:counting] countdown end end end end end |
#stop ⇒ Object
92 93 94 95 |
# File 'lib/web/web_worker.rb', line 92 def stop @worker_thread&.kill @worker_thread = nil end |