bin/ramaze in manveru-ramaze-2009.04.08 vs bin/ramaze in manveru-ramaze-2009.04.18

- old
+ new

@@ -17,51 +17,60 @@ def usage <<-TXT Usage: - ramaze <start [PIDFILE]|stop [PIDFILE]|restart [PIDFILE]|create PROJECT|console> [ruby/rack options] + ramaze <start [PIDFILE]|stop [PIDFILE]|restart [PIDFILE]|status [PIDFILE]|create PROJECT|console> [ruby/rack options] Commands: - start - Starts an instance of this application. Supply a pidfile name if you do - not want it to use the default (PROJECT.pid). + * All commands which take an optional PIDFILE default to PROJECT.pid if you do not supply one. + * All commands which start a ramaze instance will default to webrick on port 7000 unless you supply + the rack options -p/--port PORT and/or * -s/--server SERVER. - stop - Stops a running instance of this application. Supply a pidfile name if you - started it with a pidfile other than the default (PROJECT.pid). + start - Starts an instance of this application. + stop - Stops a running instance of this application. + restart - Stops running instance of this application, then starts it back up. Pidfile (if supplied) is used for both stop and start. + status - Gives status of a running ramaze instance + create - Creates a new prototype Ramaze application in a directory named PROJECT in the current directory. ramaze create foo would make ./foo containing an - application prototype. Rack options are meaningless here. + application prototype. Rack options are ignored here. console - Starts an irb console with app.rb (and irb completion) loaded. This command - ignores rack options. + ignores rack options, ARGV is passed on to IRB. Rack Options #{%x{rackup --help}.split("\n").reject { |line| line.match(/^Usage:/) }.join("\n\t")} TXT end ## Methods for commands {{{ def start # Find the name of this app app_name = default_pidfile.sub(/\.pid$/,'') + added_args = [] if daemonize = OURARGS.detect { |arg| arg.match(/^(-[dD]|--daemonize)$/) } if pid_arg = OURARGS.detect { |arg| arg.match(/^(-P|--pid)/) } puts "User supplied pid: #{pid_arg}" pid_file = OURARGS[OURARGS.index(pid_arg) + 1] puts "Starting daemon with user defined pidfile: #{pid_file}" - exec("rackup", "config.ru", *ARGV) else - puts "Starting daemon with default pidfile: #{default_pidfile}" - exec("rackup", "config.ru", "-P", default_pidfile, *ARGV) + puts "Starting daemon with default pidfile: #{pid_file = default_pidfile}" + added_args += ["-P", pid_file] end - else - exec("rackup", "config.ru", *ARGV) + if check_running?(pid_file) + $stderr.puts "Ramaze is already running with pidfile: #{pid_file}" + exit 127 + end end + added_args += ["-p", "7000"] unless OURARGS.detect { |arg| arg.match(/^(-p|--port)/) } + added_args += ["-s", "webrick"] unless OURARGS.detect { |arg| arg.match(/^(-s|--server)/) } + exec("rackup", "config.ru", *(ARGV + added_args)) end def create(command) project_name = OURARGS[OURARGS.index(command) + 1] if project_name.nil? @@ -82,38 +91,103 @@ require 'ramaze/tool/create' Ramaze::Tool::Create.create(project_name, opts) end def stop(command) - return false unless pid_file = find_pid(command) + unless pid_file = find_pid(OURARGS[OURARGS.index(command) + 1]) + $stderr.puts "No pid_file found! Cannot stop ramaze (may not be started)." + return false + end pid = File.read(pid_file).to_i - puts "Killing pid #{pid}" + puts "Stopping pid #{pid}" Process.kill("INT", pid) - sleep 1 - begin - Process.getpriority(Process::PRIO_PROCESS, pid) - $stdout.puts "Process #{pid} did not die, forcing it with -9" + sleep 2 + if is_running?(pid) + $stderr.puts "Process #{pid} did not die, forcing it with -9" Process.kill(9, pid) File.unlink(pid_file) if File.file?(pid_file) true - rescue Errno::ESRCH + else File.unlink(pid_file) if File.file?(pid_file) true end end +def status(command) + unless pid_file = find_pid(OURARGS[OURARGS.index(command) + 1]) + $stderr.puts "No pid_file found! Ramaze may not be started." + exit 1 + end + puts "Pid file #{pid_file} found, PID is #{pid = File.read(pid_file)}" + unless is_running?(pid.to_i) + $stderr.puts "PID #{pid} is not running" + exit 1 + end + if Object.const_defined?("WIN32OLE") + wmi = WIN32OLE.connect("winmgmts://") + processes, ours = wmi.ExecQuery("select * from win32_process where ProcessId = #{pid}"), [] + processes.each { |p| ours << [p.Name, p.CommandLine, p.VirtualSize, p.CreationDate, p.ExecutablePath, p.Status ] } + puts "Ramaze is running!\n\tName: %s\n\tCommand Line: %s\n\tVirtual Size: %s\n\tStarted: %s\n\tExec Path: %s\n\tStatus: %s" % ours.first + else + require "pathname" + # Check for /proc + if File.directory?(proc_dir = Pathname.new("/proc")) + proc_dir = proc_dir.join(pid) + # If we have a "stat" file, we'll assume linux and get as much info + # as we can + if File.file?(stat_file = proc_dir.join("stat")) + stats = File.read(stat_file).split + puts "Ramaze is running!\n\tCommand Line: %s\n\tVirtual Size: %s\n\tStarted: %s\n\tExec Path: %s\n\tStatus: %s" % [ + File.read(proc_dir.join("cmdline")).split("\000").join(" "), + "%s k" % (stats[22].to_f / 1024), + File.mtime(proc_dir), + File.readlink(proc_dir.join("exe")), + stats[2] + ] + exit + end + end + # Fallthrough status, just print a ps + puts "Ramaze process #{pid} is running!" + begin + puts %x{ps l #{pid}} + rescue + puts "No further information available" + end + end +end + ## End Command methods }}} # Helper methods {{{ def default_pidfile return @default_pidfile if @default_pidfile @default_pidfile = (File.basename(File.expand_path(ENV["PWD"])) + ".pid").strip - @default_pidfile end -def find_pid(command) - pid_file = OURARGS[OURARGS.index(command) + 1] +def is_running?(pid) + if Object.const_defined?("WIN32OLE") + wmi = WIN32OLE.connect("winmgmts://") + processes, ours = wmi.ExecQuery("select * from win32_process where ProcessId = #{pid}"), [] + processes.each { |process| ours << process.Name } + ours.first.nil? + else + begin + prio = Process.getpriority(Process::PRIO_PROCESS, pid) + true + rescue Errno::ESRCH + false + end + end +end + +def check_running?(pid_file) + return false unless File.file?(pid_file) + is_running?(File.read(pid_file).to_i) +end + +def find_pid(pid_file) if pid_file.nil? or not File.file?(pid_file) pid_file = default_pidfile end unless File.file?(pid_file) $stderr.puts "Could not find running process id." @@ -123,33 +197,41 @@ end ## End helper methods }}} OURARGS = ARGV.dup -command = ARGV.detect { |arg| arg.match(/^(?:--?)?(?:start|stop|restart|create|h(?:elp)?|v(?:ersion)?|console)/) } +command = ARGV.detect { |arg| arg.match(/^(?:--?)?(?:start|stop|restart|create|h(?:elp)?|v(?:ersion)?|console|status)/) } if command.nil? command = "" else ARGV.delete(command) end case command +when /^(?:--?)?status$/ + status(command) when /^(?:--?)?restart$/ stop(command) start when /^(?:--?)?start$/ start when /^(?:--?)?create$/ create(command) when /^(?:--?)?stop$/ if stop(command) - puts "Ramazement has ended, go in peace" + puts "Ramazement has ended, go in peace." $stdout.flush else puts "Ramaze failed to stop (or was not running)" end when /^(?:--?)?console$/ - exec("irb", "-r", "irb/completion", "-r", "app", *ARGV) + require "ramaze" + require "irb" + require "irb/completion" + Ramaze.options.started = true + require "start" + IRB.start + puts "Ramazement has ended, go in peace." when /^(?:--?)?h(elp)?$/ puts usage when /^(?:--?)?v(ersion)?$/ include_ramaze puts Ramaze::VERSION