Class: RCelery::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/rcelery/daemon.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Daemon

Returns a new instance of Daemon.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rcelery/daemon.rb', line 6

def initialize(args)
  @config = RCelery::Configuration.new
  opts = OptionParser.new do |opt|
    opt.on('-n', '--hostname HOSTNAME', 'Hostname of the AMQP broker') do |host|
      @config.host = host
    end

    opt.on('-p', '--port PORT', 'Port of the AMQP broker') do |port|
      @config.port = port
    end

    opt.on('-v', '--vhost VHOST', 'Vhost of the AMQP broker') do |vhost|
      @config.vhost = vhost
    end

    opt.on('-u', '--username USERNAME', 'Username to use during authentication with the AMQP broker') do |username|
      @config.username = username
    end

    opt.on('-w', '--password PASSWORD', 'Password to use during authentication with the AMQP broker') do |password|
      @config.password = password
    end

    opt.on('-a', '--application APPLICATION', 'Name of the application') do |application|
      @config.application = application
    end

    opt.on('-t', '--tasks lib1,lib2,...', Array, 'List of libraries to require that contain task definitions') do |requires|
      requires.each do |lib|
        require lib
      end
    end

    opt.on('-r', '--rails', 'Require \'config/environment\' to provide the Rails environment') do
      require 'config/environment'
      require 'rcelery/rails'
      RCelery::Rails.initialize
      ::Rails.logger.auto_flushing = true
    end

    opt.on('-W', '--workers NUMBER', 'The number of workers to launch (default 1)') do |num|
      @config.worker_count = num
    end

    opt.on_tail('-h', '--help', 'Show this message') do
      puts opts
      exit
    end
  end
  opts.parse!(args)
end

Instance Method Details

#runObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rcelery/daemon.rb', line 58

def run
  pool = RCelery::Pool.new(@config)
  @config.worker_count.times do
    Thread.new do
      @worker = RCelery::Worker.new
      @worker.start pool
    end
  end

  pool.start
  trap_signals
  RCelery.thread.join
end

#trap_signalsObject



72
73
74
75
76
77
78
79
80
# File 'lib/rcelery/daemon.rb', line 72

def trap_signals
  block = proc do
    @worker.stop
    RCelery.stop
    exit
  end
  Signal.trap('INT', &block)
  Signal.trap('TERM', &block)
end