Permalink
Browse files

Add daemon and proper logging support

The -D flag now causes mongrel to daemonize. It was previously a no op. This creates a log file (mongrel.log) and a pid file (mongrel.pid) in the log directory (<Park Place directory>/log/). Moved the camping.log file, created when debug is set, to that directory as well.
  • Loading branch information...
1 parent 92cd509 commit 548a3fa02a4f2497b62360510a55915598a3f9fa @joewest joewest committed Dec 7, 2010
Showing with 45 additions and 4 deletions.
  1. +10 −0 .gitignore
  2. +7 −3 bin/parkplace
  3. +28 −1 lib/parkplace.rb
View
@@ -0,0 +1,10 @@
+# Logs and pids
+*.log
+*.pid
+
+# Data storage
+*.db
+*storage*
+
+# Bundler
+Gemfile.lock
View
@@ -32,11 +32,11 @@ opts = OptionParser.new do |opts|
end
opts.on("-D", "--[no-]daemon", "Daemon mode") do |d|
- options.daemon = d
+ $DAEMONIZE = options.daemon = d
end
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
- options.verbose = v
+ $VERBOSE = options.verbose = v
end
opts.on("-u", "--debug", "Debug Mode") do |v|
@@ -71,7 +71,11 @@ ParkPlace.config(options)
include ParkPlace
ParkPlace::Models::Base.establish_connection(options.database)
-ParkPlace::Models::Base.logger = Logger.new('camping.log') if $DEBUG
+if $DEBUG
+ ParkPlace::Models::Base.logger = Logger.new(File.join(ParkPlace::LOG_PATH,'camping.log'))
+ ParkPlace::Models::Base.colorize_logging = false
+ ParkPlace::Models::Base.logger.formatter = Logger::Formatter.new
+end
ParkPlace.create
num_users = ParkPlace::Models::User.count
View
@@ -30,6 +30,7 @@
module ParkPlace
VERSION = "0.7"
BUFSIZE = (4 * 1024)
+ LOG_PATH = File.expand_path('../log', File.dirname(__FILE__))
STORAGE_PATH = File.join(Dir.pwd, 'storage')
STATIC_PATH = File.expand_path('../static', File.dirname(__FILE__))
RESOURCE_TYPES = %w[acl torrent]
@@ -89,24 +90,50 @@ def config(options)
end
end
ParkPlace::STORAGE_PATH.replace options.storage_dir
+
+ options.log_dir = File.expand_path(options.log_dir || 'log', options.parkplace_dir)
+ File.makedirs( options.log_dir )
+ ParkPlace::LOG_PATH.replace options.log_dir
end
def serve(host, port)
require 'mongrel'
require 'mongrel/camping'
+ require 'mongrel/debug' if $VERBOSE || $DAEMONIZE
+
if $PARKPLACE_PROGRESS
require_gem 'mongrel_upload_progress'
GemPlugin::Manager.instance.load "mongrel" => GemPlugin::INCLUDE
end
- config = Mongrel::Configurator.new :host => host do
+ cwd = Dir.pwd
+ pid_file = File.join(ParkPlace::LOG_PATH,'mongrel.pid')
+ log_file = File.join(ParkPlace::LOG_PATH,'mongrel.log')
+
+ config = Mongrel::Configurator.new :host => host, :pid_file => pid_file do
+
+ if $DAEMONIZE
+ daemonize :cwd => cwd, :log_file => log_file
+
+ # Need to reopen this log file because daemonize closes any
+ # file opened before it was called.
+ if $DEBUG
+ ParkPlace::Models::Base.logger = Logger.new(File.join(ParkPlace::LOG_PATH,'camping.log'))
+ ParkPlace::Models::Base.colorize_logging = false
+ ParkPlace::Models::Base.logger.formatter = Logger::Formatter.new
+ end
+ end
+
listener :port => port do
+
+ uri "/", :handler => plugin('/handlers/requestlog::access') if $VERBOSE || $DAEMONIZE
uri "/", :handler => Mongrel::Camping::CampingHandler.new(ParkPlace)
if $PARKPLACE_PROGRESS
uri "/control/buckets", :handler => plugin('/handlers/upload')
end
uri "/favicon", :handler => Mongrel::Error404Handler.new("")
trap("INT") { stop }
run
+ write_pid_file if $DAEMONIZE
end
end

0 comments on commit 548a3fa

Please sign in to comment.