Skip to content

Commit

Permalink
Fix randomly failing Daemonize#kill test.
Browse files Browse the repository at this point in the history
Move cwd from an option to an arg in thin script.
Handle a couple of socket error in server.


git-svn-id: http://code.macournoyer.com/svn/thin/trunk@322 ab7993de-5426-0410-a80b-baa00616f331
  • Loading branch information
macournoyer committed Nov 23, 2007
1 parent 530be7d commit b99d66d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 17 deletions.
7 changes: 3 additions & 4 deletions bin/thin
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require 'thin/commands'
Thin.define_commands do
program_name 'thin'
version Thin::VERSION
help 'Thin is a web server take can run your Rails app in no time'

option :address, :short => :a, :default => '0.0.0.0', :message => 'Address to bind to'
option :port, :short => :p, :default => 3000, :message => 'Port number to bind to',
Expand All @@ -31,9 +32,7 @@ Thin.define_commands do
option :log_file, :short => :l, :message => 'File to write log output to'
option :daemonize, :short => :d, :message => 'Run in the background'
option :pid_file, :short => :P, :default => 'tmp/pids/thin.pid', :message => 'File to write the PID (use with -d)'
option :cwd, :short => :c, :message => 'Change to dir before starting',
:param_name => 'PATH'

command :start, Thin::Commands::Server::Start, :valid_options => [:address, :port, :environment, :log_file, :daemonize, :pid_file, :cwd]
command :stop, Thin::Commands::Server::Stop, :valid_options => [:pid_file, :cwd]
command :start, Thin::Commands::Server::Start, :valid_options => [:address, :port, :environment, :log_file, :daemonize, :pid_file]
command :stop, Thin::Commands::Server::Stop, :valid_options => [:pid_file]
end
1 change: 1 addition & 0 deletions bin/thin_cluster
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require 'thin/commands'
Thin.define_commands do
program_name 'thin_cluster'
version Thin::VERSION
help 'Thin cluster is a monitoring tool for multiple Thin web server instances'

option :address, :short => :a, :default => '0.0.0.0', :message => 'Address to bind to'
option :port, :short => :p, :default => 3000, :message => 'Port number to bind to',
Expand Down
13 changes: 13 additions & 0 deletions lib/thin/commands/server/start.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module Thin::Commands::Server
class Start < Thin::Commands::Command
attr_accessor :address, :port, :environment, :log_file, :daemonize, :pid_file, :cwd

def cwd
args.first || '.'
end

def run
Dir.chdir cwd
server = Thin::Server.new(address, port,
Expand All @@ -22,5 +26,14 @@ def run
def self.help
"Starts a new Thin web server for a Rails application."
end

def self.detailed_help
<<-EOF
usage: thin start [PATH]
Starts a new Thin web server for the Rails application in PATH
(default to current directory).
EOF
end
end
end
18 changes: 16 additions & 2 deletions lib/thin/commands/server/stop.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Thin::Commands::Server
class Stop < Thin::Commands::Command
attr_accessor :pid_file, :cwd
attr_accessor :pid_file

def cwd
args.first || '.'
end

def run
raise Thin::Commands::CommandError, 'PID file required' unless pid_file
Expand All @@ -9,7 +13,17 @@ def run
end

def self.help
"Stops a web server running in the background."
"Stops the web server running in the background."
end

def self.detailed_help
<<-EOF
usage: thin stop [PATH]
Stops the web server running in the background
which PID is in the file PATH/<pid-file>
(default to <current directory>/tmp/pids/thin.pid).
EOF
end
end
end
14 changes: 10 additions & 4 deletions lib/thin/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ def start

logger.info ">> Thin web server (v#{VERSION})"

logger.info ">> Starting handlers ..."
@handlers.each { |h| h.start }
@handlers.each do |handler|
logger.info ">> Starting #{handler.class.name} ..."
handler.start
end

logger.info ">> Listening on #{host}:#{port}, CTRL+C to stop"
until @stop
Expand Down Expand Up @@ -85,11 +87,15 @@ def process(client)
client.write ERROR_404_RESPONSE
end

rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, Errno::EINVAL, Errno::EBADF
client.close rescue nil
rescue InvalidRequest => e
logger.error "Invalid request : #{e}"
logger.error "Invalid request: #{e.message}"
logger.error "Request data:\n#{data}"
client.write ERROR_404_RESPONSE rescue nil
rescue Object => e
logger.error "Unexpected error while processing request : #{e}"
logger.error "Unexpected error while processing request: #{e.inspect}"
logger.error e.backtrace.join("\n")
ensure
request.close if request rescue nil
response.close if response rescue nil
Expand Down
15 changes: 11 additions & 4 deletions lib/transat/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ def option(name, options={})
def command(name, klass, options={})
@commands[name.to_s] = options.merge(:class => klass)
end

def help(message)
@help = message
end

def parse_and_execute(args=ARGV)
begin
Expand Down Expand Up @@ -182,16 +186,19 @@ def usage(command=nil)
message << "Type '#{program_name.downcase} help <command>' for help on a specific command."
message << "Type '#{program_name.downcase} version' to get this program's version."
message << ""
message << "Available subcommands are:"
message << "Available commands are:"
@commands.sort.each do |command, options|
command_klass = options[:class]
command_help = command_klass.respond_to?(:help) ? command_klass.help : ''
if command_klass.respond_to?(:aliases) then
message << " #{command} (#{command_klass.aliases.join(", ")}) ".ljust(15) + command_help
message << " #{command} (#{command_klass.aliases.join(", ")})"
else
message << " #{command} ".ljust(15) + command_help
message << " #{command}"
end
end
if @help
message << ""
message << @help
end
end

message.map {|line| line.chomp}.join("\n")
Expand Down
7 changes: 4 additions & 3 deletions test/daemonizer_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/test_helper'
require 'timeout'

class DaemonizerTest < Test::Unit::TestCase
def setup
Expand All @@ -16,9 +17,9 @@ def test_daemonize
def test_kill
@daemonizer.daemonize { empty_loop }

sleep 1

assert File.exist?('thin.pid')
Timeout.timeout(5) do
sleep 0.5 until File.exist?('thin.pid')
end

@daemonizer.kill

Expand Down

0 comments on commit b99d66d

Please sign in to comment.