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.
-
.worker_count ⇒ Object
Returns the value of attribute worker_count.
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
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 19 |
# File 'lib/que/worker.rb', line 13 def initialize(queue = '') super() # For MonitorMixin. @queue = queue @state = :working @thread = Thread.new { work_loop } @thread.abort_on_exception = true end |
Class Attribute Details
.mode ⇒ Object
Returns the value of attribute mode.
121 122 123 |
# File 'lib/que/worker.rb', line 121 def mode @mode end |
.wake_interval ⇒ Object
Returns the value of attribute wake_interval.
121 122 123 |
# File 'lib/que/worker.rb', line 121 def wake_interval @wake_interval end |
.worker_count ⇒ Object
Returns the value of attribute worker_count.
121 122 123 |
# File 'lib/que/worker.rb', line 121 def worker_count @worker_count 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
153 154 155 |
# File 'lib/que/worker.rb', line 153 def wake! workers.find(&:wake!) end |
.wake_all! ⇒ Object
157 158 159 |
# File 'lib/que/worker.rb', line 157 def wake_all! workers.each(&:wake!) end |
.workers ⇒ Object
144 145 146 |
# File 'lib/que/worker.rb', line 144 def workers @workers ||= [] end |
Instance Method Details
#alive? ⇒ Boolean
21 22 23 |
# File 'lib/que/worker.rb', line 21 def alive? !!@thread.status end |
#sleeping? ⇒ Boolean
25 26 27 |
# File 'lib/que/worker.rb', line 25 def sleeping? synchronize { _sleeping? } end |
#stop ⇒ Object
This needs to be called when trapping a signal, so it can’t lock the monitor.
46 47 48 49 |
# File 'lib/que/worker.rb', line 46 def stop @stop = true @thread.wakeup if _sleeping? end |
#wait_until_stopped ⇒ Object
51 52 53 |
# File 'lib/que/worker.rb', line 51 def wait_until_stopped wait while alive? end |
#wake! ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/que/worker.rb', line 33 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
29 30 31 |
# File 'lib/que/worker.rb', line 29 def working? synchronize { @state == :working } end |