Class: EventMachine::Resque::WorkerMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/em-resque/worker_machine.rb

Overview

WorkerMachine is an EventMachine with Resque workers wrapped in Ruby fibers.

An instance contains the workers and a system monitor running inside an EventMachine. The monitoring takes care of stopping the machine when all workers are shut down.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ WorkerMachine

Initializes the machine, creates the fibers and workers, traps quit signals and prunes dead workers

Options

concurrency

The number of green threads inside the machine (default 20)

interval

Time in seconds how often the workers check for new work (default 5)

fibers_count

How many fibers (and workers) to be run inside the machine (default 1)

queues

Which queues to poll (default all)

verbose

Verbose log output (default false)

vverbose

Even more verbose log output (default false)

pidfile

The file to save the process id number

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/em-resque/worker_machine.rb', line 28

def initialize(opts = {})
  @concurrency = opts[:concurrency] || 20
  @interval = opts[:interval] || 5
  @fibers_count = opts[:fibers] || 1
  @queues = opts[:queue] || opts[:queues] || '*'
  @verbose = opts[:logging] || opts[:verbose] || false
  @very_verbose = opts[:vverbose] || false
  @pidfile = opts[:pidfile]
  @redis_uri = opts[:redis] || "redis://127.0.0.1:6379"

  raise(ArgumentError, "Should have at least one fiber") if @fibers_count.to_i < 1

  build_workers
  build_fibers
  create_pidfile
end

Instance Method Details

#fibersObject



61
62
63
# File 'lib/em-resque/worker_machine.rb', line 61

def fibers
  @fibers || []
end

#startObject

Start the machine and start polling queues.



46
47
48
49
50
51
52
53
54
# File 'lib/em-resque/worker_machine.rb', line 46

def start
  EM.synchrony do
    EM::Resque.initialize_redis(@redis_uri, @fibers_count)
    trap_signals
    prune_dead_workers
    @fibers.each(&:resume)
    system_monitor.resume
  end
end

#stopObject

Stop the machine.



57
58
59
# File 'lib/em-resque/worker_machine.rb', line 57

def stop
  @workers.each(&:shutdown)
end

#workersObject



65
66
67
# File 'lib/em-resque/worker_machine.rb', line 65

def workers
  @workers || []
end