Class: Que::Worker
Class Attribute Summary collapse
-
.mode ⇒ Object
Returns the value of attribute mode.
-
.wake_interval ⇒ Object
Returns the value of attribute wake_interval.
Instance Attribute Summary collapse
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Class Method Summary collapse
- .wake! ⇒ Object
- .wake_all! ⇒ Object
- .worker_count ⇒ Object
- .worker_count=(count) ⇒ Object
- .workers ⇒ Object
Instance Method Summary collapse
- #alive? ⇒ Boolean
-
#initialize(queue = '') ⇒ Worker
constructor
A new instance of Worker.
- #sleeping? ⇒ Boolean
-
#stop ⇒ Object
This needs to be called when trapping a signal, so it can’t lock the monitor.
- #wait_until_stopped ⇒ Object
- #wake! ⇒ Object
- #working? ⇒ Boolean
Constructor Details
#initialize(queue = '') ⇒ Worker
Returns a new instance of Worker.
13 14 15 16 17 18 |
# File 'lib/que/worker.rb', line 13 def initialize(queue = '') super() # For MonitorMixin. @queue = queue @state = :working @thread = Thread.new { work_loop } end |
Class Attribute Details
.mode ⇒ Object
Returns the value of attribute mode.
120 121 122 |
# File 'lib/que/worker.rb', line 120 def mode @mode end |
.wake_interval ⇒ Object
Returns the value of attribute wake_interval.
120 121 122 |
# File 'lib/que/worker.rb', line 120 def wake_interval @wake_interval end |
Instance Attribute Details
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
11 12 13 |
# File 'lib/que/worker.rb', line 11 def queue @queue end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
11 12 13 |
# File 'lib/que/worker.rb', line 11 def state @state end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
11 12 13 |
# File 'lib/que/worker.rb', line 11 def thread @thread end |
Class Method Details
.wake! ⇒ Object
149 150 151 |
# File 'lib/que/worker.rb', line 149 def wake! workers.find &:wake! end |
.wake_all! ⇒ Object
153 154 155 |
# File 'lib/que/worker.rb', line 153 def wake_all! workers.each &:wake! end |
.worker_count ⇒ Object
140 141 142 |
# File 'lib/que/worker.rb', line 140 def worker_count workers.count end |
.worker_count=(count) ⇒ Object
135 136 137 138 |
# File 'lib/que/worker.rb', line 135 def worker_count=(count) set_mode(count > 0 ? :async : :off) set_worker_count(count) end |
.workers ⇒ Object
131 132 133 |
# File 'lib/que/worker.rb', line 131 def workers @workers ||= [] end |
Instance Method Details
#alive? ⇒ Boolean
20 21 22 |
# File 'lib/que/worker.rb', line 20 def alive? !!@thread.status end |
#sleeping? ⇒ Boolean
24 25 26 |
# File 'lib/que/worker.rb', line 24 def sleeping? synchronize { _sleeping? } end |
#stop ⇒ Object
This needs to be called when trapping a signal, so it can’t lock the monitor.
45 46 47 48 |
# File 'lib/que/worker.rb', line 45 def stop @stop = true @thread.wakeup if _sleeping? end |
#wait_until_stopped ⇒ Object
50 51 52 |
# File 'lib/que/worker.rb', line 50 def wait_until_stopped wait while alive? end |
#wake! ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/que/worker.rb', line 32 def wake! synchronize do if sleeping? # Have to set the state here so that another thread checking # immediately after this won't see the worker as asleep. @state = :working @thread.wakeup true end end end |
#working? ⇒ Boolean
28 29 30 |
# File 'lib/que/worker.rb', line 28 def working? synchronize { @state == :working } end |