Class: Resqued::Config::Worker

Inherits:
Base
  • Object
show all
Defined in:
lib/resqued/config/worker.rb

Overview

A config handler that builds workers.

No worker processes are spawned by this class.

Instance Method Summary collapse

Methods inherited from Base

#apply, #apply_all

Methods included from Dsl

#after_exit, #after_fork, #before_fork

Constructor Details

#initialize(options = {}) ⇒ Worker

Public.



11
12
13
14
15
16
# File 'lib/resqued/config/worker.rb', line 11

def initialize(options = {})
  options = options.dup
  @worker_class = options.delete(:worker_class) || Resqued::Worker
  @worker_options = options
  @workers = []
end

Instance Method Details

#queue(*queues) ⇒ Object

DSL: Define a queue for the worker_pool to work from.

queue 'one'
queue '*'
queue 'four-a', 'four-b', :percent => 10
queue 'five', :count => 5


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/resqued/config/worker.rb', line 53

def queue(*queues)
  options = queues.last.is_a?(Hash) ? queues.pop : {}
  concurrency =
    case options
    when Hash
      if percent = options[:percent]
        percent * 0.01
      elsif count = options[:count]
        count
      else
        1.0
      end
    else
      1.0
    end
  queues.each { |queue| @pool_queues[queue] = concurrency }
end

#worker(*queues) ⇒ Object

DSL: Create a worker for the exact queues listed.

worker 'one', :interval => 1


21
22
23
24
25
26
27
# File 'lib/resqued/config/worker.rb', line 21

def worker(*queues)
  options = queues.last.is_a?(Hash) ? queues.pop.dup : {}
  queues = queues.flatten
  queues = ["*"] if queues.empty?
  queues = queues.shuffle if options.delete(:shuffle_queues)
  @workers << @worker_class.new(options.merge(@worker_options).merge(queues: queues))
end

#worker_factory(&block) ⇒ Object

DSL: Define a factory Proc used to create Resque::Workers. The factory Proc receives a list of queues as an argument.

worker_factory { |queues| Resque::Worker.new(*queues) }


43
44
45
# File 'lib/resqued/config/worker.rb', line 43

def worker_factory(&block)
  @worker_options.merge!(worker_factory: block)
end

#worker_pool(count, *queues) ⇒ Object

DSL: Set up a pool of workers. Define queues for the members of the pool with ‘queue`.

worker_pool 20, :interval => 1


32
33
34
35
36
37
# File 'lib/resqued/config/worker.rb', line 32

def worker_pool(count, *queues)
  @pool_size = count
  @pool_options = queues.last.is_a?(Hash) ? queues.pop : {}
  @pool_queues = {}
  queues.each { |q| queue q }
end