Class: Wakame::Runner::Agent

Inherits:
Object
  • Object
show all
Includes:
Daemonize
Defined in:
lib/wakame/runner/agent.rb

Instance Method Summary collapse

Methods included from Daemonize

#change_privilege, #daemonize, #on_restart, #pid, #pid_file, #remove_pidfile, #restart, #setup_pidfile

Constructor Details

#initialize(argv) ⇒ Agent

Returns a new instance of Agent.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/wakame/runner/agent.rb', line 11

def initialize(argv)
  @argv = argv.dup
  
  @options = {
    :amqp_server => URI.parse('amqp://guest@localhost/'),
    :log_file => '/var/log/wakame-agent.log',
    :pid_file => '/var/run/wakame/wakame-agent.pid',
    :daemonize => true
  }
  
  parser.parse! @argv
end

Instance Method Details

#parserObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wakame/runner/agent.rb', line 25

def parser
  @parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: agent [options]"
    
    opts.separator ""
    opts.separator "Agent options:"
    opts.on( "-p", "--pid PIDFILE", "pid file path" ) {|str| @options[:pid_file] = str }
    opts.on( "-s", "--server AMQP_URI", "amqp server" ) {|str|
      begin 
        @options[:amqp_server] = URI.parse(str)
      rescue URI::InvalidURIError => e
        fail "#{e}"
      end
    }
    opts.on("-X", "", "daemonize flag" ) { @options[:daemonize] = false }
    
  end


end

#runObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/wakame/runner/agent.rb', line 47

def run
  %w(QUIT INT TERM).each { |i|
    Signal.trap(i) { Wakame::Agent.stop{ remove_pidfile } }
  }

  unless @options[:amqp_server].nil?
    uri = @options[:amqp_server]
    default = ::AMQP.settings
    opts = {:host => uri.host, 
      :port => uri.port || default[:port],
      :vhost => uri.vhost || default[:vhost],
      :user=>uri.user || default[:user],
      :pass=>uri.password ||default[:pass]
    }
  else
    opts = nil
  end
  
  if @options[:daemonize]
    daemonize(@options[:log_file])
  end

  EM.epoll if Wakame.config.eventmachine_use_epoll
  EM.run {
    Wakame::Agent.start(opts)
  }
end