Class: Isono::Runner::Base

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/isono/runner/base.rb

Direct Known Subclasses

CLI::Oneshot, RpcServer::Server

Instance Method Summary collapse

Methods included from Logger

included, initialize

Constructor Details

#initializeBase

Returns a new instance of Base.



48
49
50
51
52
53
54
55
56
57
# File 'lib/isono/runner/base.rb', line 48

def initialize()
  @options = {
    :amqp_server_uri => URI.parse('amqp://guest:guest@localhost/'),
    :log_file => nil,
    :pid_file => nil,
    :daemonize => false,
    :config_path => nil,
    :manifest_path => nil,
  }
end

Instance Method Details

#optparse(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/isono/runner/base.rb', line 59

def optparse(args)
  optparse = OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} [options]"
    
    opts.separator ""
    opts.separator "Options:"
    opts.on( "-i", "--id ID", "Manually specify the Agent ID" ) {|str| @options[:node_id] = str }
    opts.on( "-p", "--pid PIDFILE", "pid file path" ) {|str| @options[:pid_file] = str }
    opts.on( "--log LOGFILE", "log file path" ) {|str| @options[:log_file] = str }
    opts.on( "--config CONFFILE", "config file path" ) {|str| @options[:config_file] = str }
    opts.on( "-s", "--server AMQP_URI", "amqp broker server to connect" ) {|str|
      begin
        @options[:amqp_server_uri] = URI.parse(str)
      rescue URI::InvalidURIError => e
        abort "#{e}"
      end
    }
    opts.on("-b", "Run in background" ) { @options[:daemonize] = false }
  end

  optparse.parse!(args)
end

#run(manifest = nil, opts = {}, &blk) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/isono/runner/base.rb', line 82

def run(manifest=nil, opts={}, &blk)
  @options = @options.merge(opts)
  optparse(ARGV.dup)
  
  if manifest.is_a?(String)
    # load manifest file
    manifest = Manifest.load_file(manifest)
  elsif manifest.nil? && @options[:manifest_path]
    manifest = Manifest.load_file(@options[:manifest_path])
  end

  if @options[:node_id]
    # force overwrite node_id if the command line arg was given.
    manifest.node_instance_id(@options[:node_id])
  elsif manifest.node_id.nil?
    # nobody specified the node_id then set the ID in the
    # default manner. 
    manifest.node_id(default_node_id)
  end

  @options[:log_file] ||= "/var/log/%s.log" % [manifest.node_name]
  @options[:pid_file] ||= "/var/run/%s.pid" % [manifest.node_name]

  if @options[:daemonize]
    if @options[:log_file]
      logio = File.open(@options[:log_file], "a")
    end
    Daemonize.daemonize(logio || STDOUT)
  end

  # EM's reactor is shutdown already when EXIT signal is
  # caught. so that set handler for TERM, INT
  %w(TERM INT).each { |i|
    Signal.trap(i) {
      if @node
        # force the block to push next loop.
        # EM.schedule gets the current thread stucked.
        EventMachine.next_tick {
          @node.close { EventMachine.stop }
          @node = nil
        }
      end
    }
  }
  Signal.trap(:EXIT) { remove_pidfile if @options[:daemonize] }

  EventMachine.epoll
  EventMachine.run {
    @node = run_main(manifest, &blk)
    raise "run_main() must return Isono::Node object: #{@node.class}" unless @node.is_a?(Isono::Node)
  }
end