Class: Ap4r::Mongrel::Start

Inherits:
Object show all
Includes:
Command::Base
Defined in:
lib/ap4r/mongrel_ap4r.rb

Instance Method Summary collapse

Methods included from Command::Base

included, #send_signal

Instance Method Details

#configureObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ap4r/mongrel_ap4r.rb', line 73

def configure
  options [
    ["-d", "--daemonize", "Run in daemon mode", :@daemon, false],
    ['-p', '--port PORT', "Port number used by mongrel", :@port, 7438],
    ['-a', '--address HOST', "IP address used by mongrel", :@host, "0.0.0.0"],
    ['-A', '--ap4r-config FILE', "Config file for reliable-msg/AP4R", :@ap4r_config_file, "config/queues.cfg"],
    ['-l', '--log FILE', "Log file", :@log_file, "log/mongrel_ap4r.log"],
    ['-P', '--pid FILE', "PID file", :@pid_file, "log/mongrel_ap4r.pid"],
    ['-r', '--root PATH', "Document root (no meanings yet.)", :@docroot, "public"],
    ['-c', '--chdir PATH', "Change to dir", :@cwd, Dir.pwd],
  ]
end

#runObject



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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ap4r/mongrel_ap4r.rb', line 98

def run
  settings = {
    :host => @host,  :port => @port,
    :log_file => @log_file, :pid_file => @pid_file,
    :docroot => @docroot,
    :daemon => @daemon,
    :ap4r_config_file => @ap4r_config_file,
  }

  config = ::Ap4r::Mongrel::Ap4rConfigurator.new(settings) do
    if defaults[:daemon]
      if File.exist? defaults[:pid_file]
        log "PID file #{defaults[:pid_file]} exists!!! Exiting with error."
        exit 1
      end
      daemonize({:log_file => @log_file, :cwd => File.expand_path(".") })
    end

    listener do
      log "Starting AP4R Handler with #{defaults[:ap4r_config_file]}"
      uri "/", :handler => ::Ap4r::Mongrel::Ap4rHandler.new(defaults)
      uri "/queues", :handler => ::Ap4r::Mongrel::Ap4rSendMessageHandler.new(defaults)
      uri "/subscribes", :handler => ::Ap4r::Mongrel::Ap4rSubscribeMessageHandler.new(defaults)
      uri "/monitoring", :handler => ::Ap4r::Mongrel::Ap4rMonitoringHandler.new(defaults)
    end
    setup_signals(settings)
  end

  config.run
  config.log "Mongrel available at #{settings[:host]}:#{settings[:port]}"

  if config.defaults[:daemon]
    config.write_pid_file
  else
    config.log "Use CTRL-C to stop."
  end

  config.log "Mongrel start up process completed."
  config.join

  if config.needs_restart
    if RUBY_PLATFORM !~ /mswin/
      cmd = "ruby #{__FILE__} start #{original_args.join(' ')}"
      config.log "Restarting with arguments:  #{cmd}"
      config.stop
      config.remove_pid_file

      if config.defaults[:daemon]
        system cmd
      else
        STDERR.puts "Can't restart unless in daemon mode."
        exit 1
      end
    else
      config.log "Win32 does not support restarts. Exiting."
    end
  end
end

#validateObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ap4r/mongrel_ap4r.rb', line 86

def validate
  @cwd = File.expand_path(@cwd)
  valid_dir? @cwd, "Path of chdir not valid: #@cwd "
  Dir.chdir(@cwd)

  valid_dir? File.dirname(@log_file), "Path to log file not valid: #@log_file"
  valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file"
  valid_dir? @docroot, "Path to docroot not valid: #@docroot"

  return @valid
end