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
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

.modeObject

Returns the value of attribute mode.



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

def mode
  @mode
end

.wake_intervalObject

Returns the value of attribute wake_interval.



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

def wake_interval
  @wake_interval
end

.worker_countObject

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

#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



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

.workersObject



144
145
146
# File 'lib/que/worker.rb', line 144

def workers
  @workers ||= []
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


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

def alive?
  !!@thread.status
end

#sleeping?Boolean

Returns:

  • (Boolean)


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

def sleeping?
  synchronize { _sleeping? }
end

#stopObject

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_stoppedObject



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

Returns:

  • (Boolean)


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

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