lib/vegas/runner.rb in vegas-0.1.8 vs lib/vegas/runner.rb in vegas-0.1.9

- old
+ new

@@ -15,11 +15,11 @@ module Vegas class Runner attr_reader :app, :app_name, :filesystem_friendly_app_name, :quoted_app_name, :rack_handler, :port, :host, :options, :args - ROOT_DIR = File.expand_path(File.join('~', '.vegas')) + ROOT_DIR = ENV['HOME'] ? File.expand_path(File.join('~', '.vegas')) : nil PORT = 5678 HOST = WINDOWS ? 'localhost' : '0.0.0.0' def initialize(app, app_name, set_options = {}, runtime_args = ARGV, &block) @options = set_options || {} @@ -76,10 +76,13 @@ start(path) end def app_dir + if !options[:app_dir] && !ROOT_DIR + raise ArgumentError.new("nor --app-dir neither EVN['HOME'] defined") + end options[:app_dir] || File.join(ROOT_DIR, filesystem_friendly_app_name) end def pid_file options[:pid_file] || File.join(app_dir, "#{filesystem_friendly_app_name}.pid") @@ -97,10 +100,11 @@ "http://#{host}:#{port}" end def start(path = nil) logger.info "Running with Windows Settings" if WINDOWS + logger.info "Running with JRuby" if JRUBY logger.info "Starting #{quoted_app_name}..." begin check_for_running(path) find_port write_url @@ -166,29 +170,41 @@ def run! logger.info "Running with Rack handler: #{@rack_handler.inspect}" rack_handler.run app, :Host => host, :Port => port do |server| - trap(kill_command) do - ## Use thins' hard #stop! if available, otherwise just #stop - server.respond_to?(:stop!) ? server.stop! : server.stop - logger.info "#{quoted_app_name} received INT ... stopping" - delete_pid! + kill_commands.each do |command| + trap(command) do + ## Use thins' hard #stop! if available, otherwise just #stop + server.respond_to?(:stop!) ? server.stop! : server.stop + logger.info "#{quoted_app_name} received INT ... stopping" + delete_pid! + end end end end # Adapted from Rackup def daemonize! - if RUBY_VERSION < "1.9" + if JRUBY + # It's not a true daemon but when executed with & works like one + thread = Thread.new {daemon_execute} + thread.join + + elsif RUBY_VERSION < "1.9" logger.debug "Parent Process: #{Process.pid}" exit!(0) if fork logger.debug "Child Process: #{Process.pid}" + daemon_execute + else Process.daemon(true, true) + daemon_execute end - + end + + def daemon_execute File.umask 0000 FileUtils.touch log_file STDIN.reopen log_file STDOUT.reopen log_file, "a" STDERR.reopen log_file, "a" @@ -205,11 +221,11 @@ system "#{cmd} #{specific_url || url}#{path}" end def kill! pid = File.read(pid_file) - logger.warn "Sending INT to #{pid.to_i}" + logger.warn "Sending #{kill_command} to #{pid.to_i}" Process.kill(kill_command, pid.to_i) rescue => e logger.warn "pid not found at #{pid_file} : #{e}" end @@ -277,11 +293,11 @@ Rack::Handler.get(@app.server) end # If all else fails, we'll use Thin else - Rack::Handler::Thin + JRUBY ? Rack::Handler::WEBrick : Rack::Handler::Thin end end def define_options OptionParser.new("", 24, ' ') do |opts| @@ -373,11 +389,11 @@ rescue OptionParser::MissingArgument => e logger.warn "#{e}, run -h for options" exit end - def kill_command - WINDOWS ? 1 : :INT + def kill_commands + WINDOWS ? [1] : [:INT, :TERM] end def delete_pid! File.delete(pid_file) if File.exist?(pid_file) end