Class: Que::Worker

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/que/worker.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

.modeObject

Returns the value of attribute mode.



120
121
122
# File 'lib/que/worker.rb', line 120

def mode
  @mode
end

.wake_intervalObject

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

#queueObject (readonly)

Returns the value of attribute queue.



11
12
13
# File 'lib/que/worker.rb', line 11

def queue
  @queue
end

#stateObject (readonly)

Returns the value of attribute state.



11
12
13
# File 'lib/que/worker.rb', line 11

def state
  @state
end

#threadObject (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_countObject



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

.workersObject



131
132
133
# File 'lib/que/worker.rb', line 131

def workers
  @workers ||= []
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/que/worker.rb', line 20

def alive?
  !!@thread.status
end

#sleeping?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/que/worker.rb', line 24

def sleeping?
  synchronize { _sleeping? }
end

#stopObject

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_stoppedObject



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

Returns:

  • (Boolean)


28
29
30
# File 'lib/que/worker.rb', line 28

def working?
  synchronize { @state == :working }
end