1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Added a new console plugin for rails.

git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@114 19e92222-5c0b-0410-8929-a290d50e31e9
This commit is contained in:
zedshaw 2006-03-19 05:17:05 +00:00
parent c40e160685
commit 8876b9a847
9 changed files with 363 additions and 6 deletions

View file

@ -0,0 +1 @@
No copying restrictions/license given.

View file

@ -0,0 +1 @@
No license given.

View file

@ -0,0 +1,5 @@
== Mongrel_console GemPlugin
You should document your project here.

View file

@ -0,0 +1,38 @@
require 'rake'
require 'rake/testtask'
require 'rake/clean'
require 'rake/gempackagetask'
require 'rake/rdoctask'
require 'tools/rakehelp'
require 'fileutils'
include FileUtils
setup_tests
setup_clean ["pkg", "lib/*.bundle", "*.gem", ".config"]
setup_rdoc ['README', 'LICENSE', 'COPYING', 'lib/**/*.rb', 'doc/**/*.rdoc']
desc "Does a full compile, test run"
task :default => [:test, :package]
version="0.1"
name="mongrel_console"
setup_gem(name, version) do |spec|
spec.summary = "Provides a combined Mongrel and Rails IRB console."
spec.description = spec.summary
spec.author="Zed A. Shaw"
spec.add_dependency('gem_plugin', '>= 0.2.1')
spec.add_dependency('mongrel', '>= 0.2.1')
spec.files += Dir.glob("resources/**/*")
end
task :install => [:test, :package] do
sh %{sudo gem install pkg/#{name}-#{version}.gem}
end
task :uninstall => [:clean] do
sh %{sudo gem uninstall #{name}}
end

View file

@ -0,0 +1,174 @@
require "irb"
begin
require "irb/completion"
rescue
STDERR.puts "Problem lading irb/completion: #$!"
end
require 'rubygems'
require 'yaml'
require 'mongrel'
require 'config/environment'
require 'dispatcher'
require 'mongrel/rails'
$mongrel = {:host => "0.0.0.0", :port => 3000, :mime => nil, :server => nil, :docroot => "public", :tracing => false}
# Tweak the rails handler to allow for tracing
class RailsHandler
alias :real_process :process
def process(request, response)
if $mongrel[:tracing]
open("log/mongrel.log", "a+") do |f|
f.puts ">>>> REQUEST #{Time.now}"
f.write(request.params.to_yaml)
f.puts ""
end
end
real_process(request, response)
if $mongrel[:tracing]
open("log/mongrel.log", "a+") do |f|
response.reset
f.puts ">>>> RESPONSE status=#{response.status} #{Time.now}"
f.write(response.header.out.read)
f.puts ""
end
end
end
end
def load_mime_map(mime_map)
mime = {}
# configure any requested mime map
if mime_map
puts "Loading additional MIME types from #{mime_map}"
mime.merge!(YAML.load_file(mime_map))
# check all the mime types to make sure they are the right format
mime.each {|k,v| puts "WARNING: MIME type #{k} must start with '.'" if k.index(".") != 0 }
end
return mime
end
# define a bunch of mongrel goodies
def self.start(options={})
if $mongrel[:server]
STDERR.puts "Mongrel already running on #{$mongrel[:host]}:#{$mongrel[:port]}"
else
$mongrel.merge! options
# need this later for safe reloading
$orig_dollar_quote = $".clone
# configure the rails handler
rails = RailsHandler.new($mongrel[:docroot], load_mime_map($mongrel[:mime]))
server = Mongrel::HttpServer.new($mongrel[:host], $mongrel[:port])
server.register("/", rails)
$mongrel[:rails] = rails
# start mongrel processing thread
server.run
STDERR.puts "Mongrel running in #{ENV['RAILS_ENV']} mode on #{$mongrel[:host]}:#{$mongrel[:port]}."
$mongrel[:server] = server
nil
end
end
def self.stop
if $mongrel[:server]
$mongrel[:server].stop
$mongrel[:server] = nil
$mongrel[:rails] = nil
else
STDERR.puts "Mongrel not running."
end
nil
end
def self.restart
stop
start
nil
end
def self.reload
if $mongrel[:rails]
STDERR.puts "Reloading rails..."
$mongrel[:rails].reload!
STDERR.puts "Done reloading rails."
else
STDERR.puts "Mongrel not running."
end
nil
end
def self.status
if $mongrel[:server]
STDERR.puts "Mongrel running with:"
$mongrel.each do |k,v|
STDERR.puts "* #{k}: \t#{v}"
end
else
STDERR.puts "Mongrel not running."
end
nil
end
def self.trace
$mongrel[:tracing] = !$mongrel[:tracing]
if $mongrel[:tracing]
STDERR.puts "Tracing mongrel requests and responses to log/mongrel.log"
else
STDERR.puts "Tracing is OFF."
end
end
def tail(file="log/#{ENV['RAILS_ENV']}.log")
STDERR.puts "Tailing #{file}. CTRL-C to stop it."
cursor = File.size(file)
last_checked = Time.now
tail_thread = Thread.new do
File.open(file, 'r') do |f|
loop do
if f.mtime > last_checked
f.seek cursor
last_checked = f.mtime
contents = f.read
cursor += contents.length
print contents
end
sleep 1
end
end
end
trap("INT") { tail_thread.kill }
tail_thread.join
nil
end
GemPlugin::Manager.instance.load "mongrel" => GemPlugin::INCLUDE, "rails" => GemPlugin::EXCLUDE
ENV['RAILS_ENV'] ||= 'development'
puts "Loading #{ENV['RAILS_ENV']} environment."
# hook up any rails specific plugins
GemPlugin::Manager.instance.load "mongrel" => GemPlugin::INCLUDE
puts "Starting console. Mongrel Commands: start, stop, reload, restart, status, trace, tail"
IRB.start(__FILE__)

View file

@ -0,0 +1,29 @@
require 'rubygems'
require 'gem_plugin'
require 'mongrel'
class Console < GemPlugin::Plugin "/commands"
include Mongrel::Command::Base
def configure
options [
['-c', '--chdir DIR', "Change to directory before running", :@dir, "."]
]
end
def validate
valid_dir? @dir, "Directory is not valid"
return @valid
end
def run
begin
Dir.chdir @dir
load File.join(File.dirname(__FILE__), "console.rb")
rescue Object
STDERR.puts "Cannot run the console script: #$!"
end
end
end

View file

@ -0,0 +1,2 @@
---
:debug: false

View file

@ -0,0 +1,105 @@
def make(makedir)
Dir.chdir(makedir) do
sh(PLATFORM =~ /win32/ ? 'nmake' : 'make')
end
end
def extconf(dir)
Dir.chdir(dir) do ruby "extconf.rb" end
end
def setup_tests
Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/test*.rb']
t.verbose = true
end
end
def setup_clean otherfiles
files = ['build/*', '**/*.o', '**/*.so', '**/*.a', 'lib/*-*', '**/*.log'] + otherfiles
CLEAN.include(files)
end
def setup_rdoc files
Rake::RDocTask.new do |rdoc|
rdoc.rdoc_dir = 'doc/rdoc'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.add(files)
end
end
def setup_extension(dir, extension)
ext = "ext/#{dir}"
ext_so = "#{ext}/#{extension}.#{Config::CONFIG['DLEXT']}"
ext_files = FileList[
"#{ext}/*.c",
"#{ext}/*.h",
"#{ext}/extconf.rb",
"#{ext}/Makefile",
"lib"
]
task "lib" do
directory "lib"
end
desc "Builds just the #{extension} extension"
task extension.to_sym => ["#{ext}/Makefile", ext_so ]
file "#{ext}/Makefile" => ["#{ext}/extconf.rb"] do
extconf "#{ext}"
end
file ext_so => ext_files do
make "#{ext}"
cp ext_so, "lib"
end
end
def base_gem_spec(pkg_name, pkg_version)
pkg_version = pkg_version
pkg_name = pkg_name
pkg_file_name = "#{pkg_name}-#{pkg_version}"
Gem::Specification.new do |s|
s.name = pkg_name
s.version = pkg_version
s.platform = Gem::Platform::RUBY
s.has_rdoc = true
s.extra_rdoc_files = [ "README" ]
s.files = %w(COPYING LICENSE README Rakefile) +
Dir.glob("{bin,doc/rdoc,test,lib}/**/*") +
Dir.glob("ext/**/*.{h,c,rb}") +
Dir.glob("examples/**/*.rb") +
Dir.glob("tools/*.rb")
s.require_path = "lib"
s.extensions = FileList["ext/**/extconf.rb"].to_a
s.bindir = "bin"
end
end
def setup_gem(pkg_name, pkg_version)
spec = base_gem_spec(pkg_name, pkg_version)
yield spec if block_given?
Rake::GemPackageTask.new(spec) do |p|
p.gem_spec = spec
p.need_tar = true
end
end
def setup_win32_gem(pkg_name, pkg_version)
spec = base_gem_spec(pkg_name, pkg_version)
yield spec if block_given?
Gem::Builder.new(spec).build
end

View file

@ -1,30 +1,32 @@
require 'mongrel'
require 'gem_plugin'
class Status < GemPlugin::Plugin "/commands"
include Mongrel::Command::Base
def configure
options [
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"]
]
]
end
def validate
@cwd = File.expand_path(@cwd)
valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
@pid_file = File.join(@cwd,@pid_file)
valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?"
return @valid
end
def run
pid = open(@pid_file) {|f| f.read }
puts "Mongrel status:"
puts "PID: #{pid}"
end
end