Class: Wakame::Runner::Master

Inherits:
Object
  • Object
show all
Includes:
Daemonize
Defined in:
lib/wakame/runner/master.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) ⇒ Master

Returns a new instance of Master.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/wakame/runner/master.rb', line 9

def initialize(argv)
  @argv = argv

  @options = {
    :amqp_server => URI.parse('amqp://guest@localhost/'),
    :log_file => '/var/log/wakame-master.log',
    :pid_file => '/var/run/wakame/wakame-master.pid',
    :daemonize => true
  }

  parser.parse! @argv
end

Instance Method Details

#parserObject



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

def parser
  @parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: master [options]"

    opts.separator ""
    opts.separator "Master options:"
    opts.on( "-p", "--pid PIDFILE", "pid file path" ) {|str| @options[:pid_file] = str }
    opts.on( "-u", "--uid UID", "user id for the running process" ) {|str| @options[:uid] = 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



46
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
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/wakame/runner/master.rb', line 46

def run
  %w(QUIT INT TERM).each { |i|
    Signal.trap(i) { Wakame::Master.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

  change_privilege(@options[:uid]) if @options[:uid]
  

  EM.epoll if Wakame.config.eventmachine_use_epoll
  EM.run {
    Wakame::Master.start(opts)

    EM.add_periodic_timer(5) {
      next
      buf = ''
      buf << "<--- RUNNING THREADS --->\n"
      ThreadGroup::Default.list.each { |i|
        buf << "#{i.inspect} #{i[:name].to_s}\n"
      }
      buf << ">--- RUNNING THREADS ---<\n"
      puts buf
    }
  }
end