mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Removed the now obsolete _service and _svc scripts from trunk, yahoo!
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@405 19e92222-5c0b-0410-8929-a290d50e31e9
This commit is contained in:
parent
e594a02098
commit
7bbe221d77
2 changed files with 0 additions and 553 deletions
|
@ -1,246 +0,0 @@
|
|||
###############################################
|
||||
# mongrel_rails_service
|
||||
#
|
||||
# Control script for Rails powered by Mongrel
|
||||
#
|
||||
# WARNING: stop command still doesn't work with rails!
|
||||
###############################################
|
||||
require 'rubygems'
|
||||
require 'mongrel'
|
||||
require 'win32/service'
|
||||
include Win32
|
||||
|
||||
module GenericCommand
|
||||
def configure
|
||||
options [
|
||||
['-n', '--name SVC_NAME', "Required name for the service to be registered/installed.", :@svc_name, nil],
|
||||
]
|
||||
end
|
||||
|
||||
def validate
|
||||
valid? @svc_name != nil, "You must specify the service name to be uninstalled."
|
||||
|
||||
# Validate that the service exists
|
||||
begin
|
||||
valid? Service.exists?(@svc_name), "There is no service with that name, cannot proceed."
|
||||
rescue
|
||||
end
|
||||
|
||||
return @valid
|
||||
end
|
||||
end
|
||||
|
||||
class Install < GemPlugin::Plugin "/commands"
|
||||
include Mongrel::Command::Base
|
||||
|
||||
# Default every option to nil so only the defined ones get passed to service
|
||||
# (which will override ServiceCommand defaults).
|
||||
def configure
|
||||
options [
|
||||
['-n', '--name SVC_NAME', "Required name for the service to be registered/installed.", :@svc_name, nil],
|
||||
['-d', '--display SVC_DISPLAY', "Adjust the display name of the service.", :@svc_display, nil],
|
||||
['-r', '--root PATH', "Set the root path where your rails app resides.", :@rails_root, Dir.pwd],
|
||||
['-e', '--environment ENV', "Rails environment to run as. (default: production)", :@environment, 'production'],
|
||||
['-b', '--binding ADDR', "Address to bind to", :@ip, nil],
|
||||
['-p', '--port PORT', "Which port to bind to", :@port, 3000],
|
||||
['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil],
|
||||
['-P', '--num-procs INT', "Number of processor threads to use", :@num_procs, nil],
|
||||
['-t', '--timeout SECONDS', "Timeout all requests after SECONDS time", :@timeout, nil],
|
||||
['-c', '--cpu CPU', "Bind the process to specific cpu, starting from 1.", :@cpu, nil]
|
||||
]
|
||||
end
|
||||
|
||||
# When we validate the options, we need to make sure the --root is actually RAILS_ROOT
|
||||
# of the rails application we wanted to serve, because later "as service" no error
|
||||
# show to trace this.
|
||||
def validate
|
||||
@rails_root = File.expand_path(@rails_root)
|
||||
|
||||
# start with the premise of app really exist.
|
||||
app_exist = true
|
||||
paths = %w{app config db log public}
|
||||
paths.each do |path|
|
||||
if !File.directory?(@rails_root + '/' + path)
|
||||
app_exist = false
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
valid? @svc_name != nil, "You must specify a valid service name to install."
|
||||
valid? app_exist == true, "The root of rails app isn't valid, please verify."
|
||||
valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map
|
||||
|
||||
# Validate the number of cpu to bind to.
|
||||
valid? @cpu.to_i > 0, "You must specify a numeric value for cpu. (1..8)" if @cpu
|
||||
|
||||
# We should validate service existance here, right Zed?
|
||||
begin
|
||||
valid? !Service.exists?(@svc_name), "The service already exist, please uninstall it first."
|
||||
rescue
|
||||
end
|
||||
|
||||
# Expand to get full path for mime-types file
|
||||
@mime_map = File.expand_path(@mime_map) if @mime_map
|
||||
|
||||
# default service display to service name
|
||||
@svc_display = @svc_name if !@svc_display
|
||||
|
||||
return @valid
|
||||
end
|
||||
|
||||
def build_params
|
||||
# build the parameters that will be used when register/install the service
|
||||
@params = ""
|
||||
|
||||
# add "service" command
|
||||
@params << "service "
|
||||
|
||||
# rails_root, must be quoted to support long_names
|
||||
@params << "-r \"#{@rails_root}\" "
|
||||
|
||||
# environment
|
||||
@params << "-e #{@environment} " if @environment
|
||||
|
||||
# binding
|
||||
@params << "-b #{@ip} " if @ip
|
||||
|
||||
# port
|
||||
@params << "-p #{@port.to_i} " if @port
|
||||
|
||||
# mime
|
||||
@params << "-m \"#{@mime_map}\" " if @mime_map
|
||||
|
||||
# num_procs
|
||||
@params << "-P #{@num_procs.to_i} " if @num_procs
|
||||
|
||||
# timeout
|
||||
@params << "-t #{@timeout.to_i} " if @timeout
|
||||
|
||||
# cpu
|
||||
@params << "-c #{@cpu.to_i} " if @cpu
|
||||
end
|
||||
|
||||
def install_service
|
||||
# use rbconfig to get the path to bin ruby.exe
|
||||
require 'rbconfig'
|
||||
|
||||
# ruby.exe instead of rubyw.exe due a exception raised when stoping the service!
|
||||
binary_path = ""
|
||||
binary_path << '"' << Config::CONFIG['bindir'] << '/ruby.exe' << '" '
|
||||
|
||||
# add service_script
|
||||
service_script = File.expand_path(File.dirname(__FILE__) + '/mongrel_rails_svc')
|
||||
binary_path << '"' << service_script << '" '
|
||||
|
||||
# now add the parameters to it.
|
||||
binary_path << @params
|
||||
|
||||
puts "Installing service with these options:"
|
||||
puts "service name: " << @svc_name
|
||||
puts "service display: " << @svc_display
|
||||
|
||||
puts "RAILS_ROOT: " << @rails_root
|
||||
puts "RAILS_ENV: " << @environment if @environment
|
||||
puts "binding: " << @ip if @ip
|
||||
puts "port: " << @port.to_s if @port
|
||||
|
||||
puts "mime_map: " << @mime_map if @mime_map
|
||||
puts "num_procs: " << @num_procs.to_s if @num_procs
|
||||
puts "timeout: " << @timeout.to_s if @timeout
|
||||
|
||||
puts "ruby.exe: " << Config::CONFIG['bindir'] << '/ruby.exe'
|
||||
puts "service script: " << service_script
|
||||
puts
|
||||
|
||||
svc = Service.new
|
||||
begin
|
||||
svc.create_service{ |s|
|
||||
s.service_name = @svc_name
|
||||
s.display_name = @svc_display
|
||||
s.binary_path_name = binary_path
|
||||
s.dependencies = []
|
||||
}
|
||||
puts "#{@svc_display} service installed."
|
||||
rescue ServiceError => err
|
||||
puts "There was a problem installing the service:"
|
||||
puts err
|
||||
end
|
||||
svc.close
|
||||
end
|
||||
|
||||
def run
|
||||
build_params
|
||||
install_service
|
||||
end
|
||||
end
|
||||
|
||||
class Delete < GemPlugin::Plugin "/commands"
|
||||
include Mongrel::Command::Base
|
||||
include GenericCommand
|
||||
|
||||
def run
|
||||
display_name = Service.getdisplayname(@svc_name)
|
||||
|
||||
begin
|
||||
Service.stop(@svc_name)
|
||||
rescue
|
||||
end
|
||||
begin
|
||||
Service.delete(@svc_name)
|
||||
rescue
|
||||
end
|
||||
puts "#{display_name} service deleted."
|
||||
end
|
||||
end
|
||||
|
||||
class Start < GemPlugin::Plugin "/commands"
|
||||
include Mongrel::Command::Base
|
||||
include GenericCommand
|
||||
|
||||
def run
|
||||
display_name = Service.getdisplayname(@svc_name)
|
||||
|
||||
begin
|
||||
Service.start(@svc_name)
|
||||
started = false
|
||||
while started == false
|
||||
s = Service.status(@svc_name)
|
||||
started = true if s.current_state == "running"
|
||||
break if started == true
|
||||
puts "One moment, " + s.current_state
|
||||
sleep 1
|
||||
end
|
||||
puts "#{display_name} service started"
|
||||
rescue ServiceError => err
|
||||
puts "There was a problem starting the service:"
|
||||
puts err
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Stop < GemPlugin::Plugin "/commands"
|
||||
include Mongrel::Command::Base
|
||||
include GenericCommand
|
||||
|
||||
def run
|
||||
display_name = Service.getdisplayname(@svc_name)
|
||||
|
||||
begin
|
||||
Service.stop(@svc_name)
|
||||
stopped = false
|
||||
while stopped == false
|
||||
s = Service.status(@svc_name)
|
||||
stopped = true if s.current_state == "stopped"
|
||||
break if stopped == true
|
||||
puts "One moment, " + s.current_state
|
||||
sleep 1
|
||||
end
|
||||
puts "#{display_name} service stopped"
|
||||
rescue ServiceError => err
|
||||
puts "There was a problem stopping the service:"
|
||||
puts err
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Mongrel::Command::Registry.instance.run ARGV
|
|
@ -1,307 +0,0 @@
|
|||
###############################################
|
||||
# mongrel_rails_svc
|
||||
#
|
||||
# This is where Win32::Daemon resides.
|
||||
###############################################
|
||||
require 'rubygems'
|
||||
require 'mongrel'
|
||||
require 'mongrel/rails'
|
||||
require 'optparse'
|
||||
require 'win32/service'
|
||||
|
||||
# We need to use OpenProcess and SetProcessAffinityMask on WinNT/2K/XP for
|
||||
# binding the process to each cpu.
|
||||
# Kernel32 Module Just for Win32 :D
|
||||
require 'dl/win32'
|
||||
|
||||
module Kernel32
|
||||
[
|
||||
%w/OpenProcess LLL L/,
|
||||
%w/SetProcessAffinityMask LL L/,
|
||||
].each do |fn|
|
||||
const_set fn[0].intern, Win32API.new('kernel32.dll', *fn)
|
||||
end
|
||||
|
||||
PROCESS_ALL_ACCESS = 0x1f0fff
|
||||
|
||||
module_function
|
||||
|
||||
def set_affinity(pid, cpu)
|
||||
handle = OpenProcess.call(PROCESS_ALL_ACCESS, 0, pid)
|
||||
|
||||
# CPU mask is a bit weird, hehehe :)
|
||||
# default mask for CPU 1
|
||||
mask = 1
|
||||
mask = %w{1 2 4 8 16 32 64 128}[cpu.to_i - 1] if cpu.to_i.between?(1, 8)
|
||||
|
||||
SetProcessAffinityMask.call(handle, mask.to_i)
|
||||
end
|
||||
end
|
||||
# End Kernel32 Module
|
||||
|
||||
DEBUG_LOG_FILE = File.expand_path(File.dirname(__FILE__) + '/debug.log')
|
||||
DEBUG_THREAD_LOG_FILE = File.expand_path(File.dirname(__FILE__) + '/debug_thread.log')
|
||||
|
||||
def dbg(msg)
|
||||
File.open(DEBUG_LOG_FILE,"a+") { |f| f.puts("#{Time.now} - #{msg}") }
|
||||
end
|
||||
|
||||
def dbg_th(msg)
|
||||
File.open(DEBUG_THREAD_LOG_FILE,"a+") { |f| f.puts("#{Time.now} - #{msg}") }
|
||||
end
|
||||
|
||||
# This class encapsulate the handler registering, http_server and working thread
|
||||
# Is standalone, so using MongrelRails in your app get everything runnig
|
||||
# (in case you don't want use mongrel_rails script)
|
||||
class MongrelRails
|
||||
def initialize(ip, port, rails_root, docroot, environment, mime_map, num_procs, timeout)
|
||||
dbg "mongrelrails_initialize entered"
|
||||
|
||||
@ip = ip
|
||||
@port = port
|
||||
@rails_root = rails_root
|
||||
@docroot = docroot
|
||||
@environment = environment
|
||||
@mime_map = mime_map
|
||||
@num_procs = num_procs
|
||||
@timeout = timeout
|
||||
|
||||
dbg "mongrelrails_initialize left"
|
||||
end
|
||||
|
||||
def delayed_initialize
|
||||
dbg "delayed_initialize entered"
|
||||
|
||||
@rails = configure_rails
|
||||
|
||||
# start up mongrel with the right configurations
|
||||
@server = Mongrel::HttpServer.new(@ip, @port, @num_procs.to_i, @timeout.to_i)
|
||||
@server.register("/", @rails)
|
||||
|
||||
dbg "delayed_initialize left"
|
||||
|
||||
end
|
||||
|
||||
def load_mime_map
|
||||
dbg "load_mime_map entered"
|
||||
|
||||
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
|
||||
|
||||
dbg "load_mime_map left"
|
||||
|
||||
return mime
|
||||
end
|
||||
|
||||
def configure_rails
|
||||
dbg "configure_rails entered"
|
||||
|
||||
Dir.chdir(@rails_root)
|
||||
|
||||
|
||||
ENV['RAILS_ENV'] = @environment
|
||||
require 'config/environment'
|
||||
|
||||
# configure the rails handler
|
||||
rails = Mongrel::Rails::RailsHandler.new(@docroot, load_mime_map)
|
||||
|
||||
dbg "configure_rails left"
|
||||
|
||||
return rails
|
||||
end
|
||||
|
||||
def start_serve
|
||||
begin
|
||||
dbg "start_serve entered"
|
||||
|
||||
@runner = Thread.new do
|
||||
dbg_th "runner_thread suspended"
|
||||
Thread.stop
|
||||
|
||||
dbg_th "runner_thread resumed"
|
||||
dbg_th "runner_thread acceptor.join"
|
||||
@server.acceptor.join
|
||||
end
|
||||
|
||||
dbg "server.run"
|
||||
@server.run
|
||||
|
||||
dbg "runner.run"
|
||||
@runner.run
|
||||
|
||||
dbg "start_serve left"
|
||||
rescue
|
||||
dbg "ERROR: #$!\r\n"
|
||||
dbg $!.backtrace.join("\r\n")
|
||||
end
|
||||
end
|
||||
|
||||
def stop_serve
|
||||
dbg "stop_serve entered"
|
||||
|
||||
if @runner.alive?
|
||||
dbg "killing thread"
|
||||
@runner.kill
|
||||
end
|
||||
|
||||
@server.stop
|
||||
|
||||
dbg "stop_serve left"
|
||||
end
|
||||
end
|
||||
|
||||
class RailsDaemon < Win32::Daemon
|
||||
def initialize(rails)
|
||||
dbg "daemon_initialize entered"
|
||||
|
||||
@rails = rails
|
||||
|
||||
dbg "daemon_initialize left"
|
||||
end
|
||||
|
||||
def service_init
|
||||
dbg "service_init entered"
|
||||
|
||||
@rails.delayed_initialize
|
||||
|
||||
dbg "service_init left"
|
||||
end
|
||||
|
||||
def service_main
|
||||
dbg "service_main entered"
|
||||
|
||||
dbg "rails.start_serve"
|
||||
@rails.start_serve
|
||||
|
||||
dbg "while RUNNING"
|
||||
while state == RUNNING
|
||||
sleep 1
|
||||
end
|
||||
dbg "state !RUNNING"
|
||||
|
||||
dbg "rails.stop_serve"
|
||||
@rails.stop_serve
|
||||
|
||||
dbg "service_main left"
|
||||
end
|
||||
|
||||
def service_stop
|
||||
dbg "service_stop entered"
|
||||
|
||||
dbg "service_stop left"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
begin
|
||||
if ARGV[0] == 'service'
|
||||
ARGV.shift
|
||||
|
||||
# default options
|
||||
OPTIONS = {
|
||||
:rails_root => Dir.pwd,
|
||||
:environment => 'production',
|
||||
:ip => '0.0.0.0',
|
||||
:port => 3000,
|
||||
:mime_map => nil,
|
||||
:num_procs => 1024,
|
||||
:timeout => 0,
|
||||
:cpu => nil
|
||||
}
|
||||
|
||||
ARGV.options do |opts|
|
||||
opts.on('-r', '--root PATH', "Set the root path where your rails app resides.") { |OPTIONS[:rails_root]| }
|
||||
opts.on('-e', '--environment ENV', "Rails environment to run as. (default: production)") { |OPTIONS[:environment]| }
|
||||
opts.on('-b', '--binding ADDR', "Address to bind to") { |OPTIONS[:ip]| }
|
||||
opts.on('-p', '--port PORT', "Which port to bind to") { |OPTIONS[:port]| }
|
||||
opts.on('-m', '--mime PATH', "A YAML file that lists additional MIME types") { |OPTIONS[:mime_map]| }
|
||||
opts.on('-P', '--num-procs INT', "Number of processor threads to use") { |OPTIONS[:num_procs]| }
|
||||
opts.on('-t', '--timeout SECONDS', "Timeout all requests after SECONDS time") { |OPTIONS[:timeout]| }
|
||||
opts.on('-c', '--cpu CPU', "Bind the process to specific cpu") { |OPTIONS[:cpu]| }
|
||||
|
||||
opts.parse!
|
||||
end
|
||||
|
||||
#expand RAILS_ROOT
|
||||
OPTIONS[:rails_root] = File.expand_path(OPTIONS[:rails_root])
|
||||
|
||||
OPTIONS[:docroot] = File.expand_path(OPTIONS[:rails_root] + '/public')
|
||||
|
||||
# We must bind to a specific cpu?
|
||||
if OPTIONS[:cpu]
|
||||
Kernel32.set_affinity(Process.pid, OPTIONS[:cpu])
|
||||
end
|
||||
|
||||
rails = MongrelRails.new(OPTIONS[:ip], OPTIONS[:port], OPTIONS[:rails_root], OPTIONS[:docroot], OPTIONS[:environment], OPTIONS[:mime_map], OPTIONS[:num_procs].to_i, OPTIONS[:timeout].to_i)
|
||||
rails_svc = RailsDaemon.new(rails)
|
||||
rails_svc.mainloop
|
||||
|
||||
elsif ARGV[0] == 'debug'
|
||||
ARGV.shift
|
||||
|
||||
# default options
|
||||
OPTIONS = {
|
||||
:rails_root => Dir.pwd,
|
||||
:environment => 'production',
|
||||
:ip => '0.0.0.0',
|
||||
:port => 3000,
|
||||
:mime_map => nil,
|
||||
:num_procs => 20,
|
||||
:timeout => 120,
|
||||
:cpu => nil
|
||||
}
|
||||
|
||||
ARGV.options do |opts|
|
||||
opts.on('-r', '--root PATH', "Set the root path where your rails app resides.") { |OPTIONS[:rails_root]| }
|
||||
opts.on('-e', '--environment ENV', "Rails environment to run as.") { |OPTIONS[:environment]| }
|
||||
opts.on('-b', '--binding ADDR', "Address to bind to") { |OPTIONS[:ip]| }
|
||||
opts.on('-p', '--port PORT', "Which port to bind to") { |OPTIONS[:port]| }
|
||||
opts.on('-m', '--mime PATH', "A YAML file that lists additional MIME types") { |OPTIONS[:mime_map]| }
|
||||
opts.on('-P', '--num-procs INT', "Number of processor threads to use") { |OPTIONS[:num_procs]| }
|
||||
opts.on('-t', '--timeout SECONDS', "Timeout all requests after SECONDS time") { |OPTIONS[:timeout]| }
|
||||
opts.on('-c', '--cpu CPU', "Bind the process to specific cpu") { |OPTIONS[:cpu]| }
|
||||
|
||||
opts.parse!
|
||||
end
|
||||
|
||||
#expand RAILS_ROOT
|
||||
OPTIONS[:rails_root] = File.expand_path(OPTIONS[:rails_root])
|
||||
|
||||
OPTIONS[:docroot] = File.expand_path(OPTIONS[:rails_root] + '/public')
|
||||
|
||||
# We must bind to a specific cpu?
|
||||
if OPTIONS[:cpu]
|
||||
Kernel32.set_affinity(Process.pid, OPTIONS[:cpu])
|
||||
end
|
||||
|
||||
rails = MongrelRails.new(OPTIONS[:ip], OPTIONS[:port], OPTIONS[:rails_root], OPTIONS[:docroot], OPTIONS[:environment], OPTIONS[:mime_map], OPTIONS[:num_procs].to_i, OPTIONS[:timeout].to_i)
|
||||
rails.delayed_initialize
|
||||
rails.start_serve
|
||||
|
||||
begin
|
||||
sleep
|
||||
rescue Interrupt
|
||||
dbg "ERROR: #$!\r\n"
|
||||
dbg $!.backtrace.join("\r\n")
|
||||
puts "graceful shutdown?"
|
||||
end
|
||||
|
||||
begin
|
||||
rails.stop_serve
|
||||
rescue
|
||||
dbg "ERROR: #$!\r\n"
|
||||
dbg $!.backtrace.join("\r\n")
|
||||
end
|
||||
end
|
||||
rescue
|
||||
dbg "ERROR: #$!\r\n"
|
||||
dbg $!.backtrace.join("\r\n")
|
||||
end
|
Loading…
Reference in a new issue