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.



15
16
17
18
19
20
21
# File 'lib/que/worker.rb', line 15

def initialize(queue = '')
  super() # For MonitorMixin.
  @queue  = queue
  @state  = :working
  @thread = Thread.new { work_loop }
  @thread.abort_on_exception = true
end

Class Attribute Details

.modeObject

Returns the value of attribute mode.



123
124
125
# File 'lib/que/worker.rb', line 123

def mode
  @mode
end

.queue_nameObject

Returns the value of attribute queue_name.



124
125
126
# File 'lib/que/worker.rb', line 124

def queue_name
  @queue_name
end

.wake_intervalObject

Returns the value of attribute wake_interval.



123
124
125
# File 'lib/que/worker.rb', line 123

def wake_interval
  @wake_interval
end

.worker_countObject

Returns the value of attribute worker_count.



123
124
125
# File 'lib/que/worker.rb', line 123

def worker_count
  @worker_count
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



13
14
15
# File 'lib/que/worker.rb', line 13

def queue
  @queue
end

#stateObject (readonly)

Returns the value of attribute state.



13
14
15
# File 'lib/que/worker.rb', line 13

def state
  @state
end

#threadObject (readonly)

Returns the value of attribute thread.



13
14
15
# File 'lib/que/worker.rb', line 13

def thread
  @thread
end

Class Method Details

.wake!Object



163
164
165
# File 'lib/que/worker.rb', line 163

def wake!
  workers.find(&:wake!)
end

.wake_all!Object



167
168
169
# File 'lib/que/worker.rb', line 167

def wake_all!
  workers.each(&:wake!)
end

.workersObject



147
148
149
# File 'lib/que/worker.rb', line 147

def workers
  @workers ||= []
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


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

def alive?
  !!@thread.status
end

#sleeping?Boolean

Returns:

  • (Boolean)


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

def sleeping?
  synchronize { _sleeping? }
end

#stopObject

This needs to be called when trapping a signal, so it can’t lock the monitor.



48
49
50
51
# File 'lib/que/worker.rb', line 48

def stop
  @stop = true
  @thread.wakeup if _sleeping?
end

#wait_until_stoppedObject



53
54
55
# File 'lib/que/worker.rb', line 53

def wait_until_stopped
  wait while alive?
end

#wake!Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/que/worker.rb', line 35

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)


31
32
33
# File 'lib/que/worker.rb', line 31

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