lib/dante/runner.rb in dante-0.0.1 vs lib/dante/runner.rb in dante-0.0.2

- old
+ new

@@ -5,49 +5,56 @@ =begin This is a utility for setting up a binary executable for a service. -# Dante::Runner.run("buffet", :pid_path => "/var/run/buffet.pid") do +# Dante::Runner.new("buffet", :pid_path => "/var/run/buffet.pid") do # ...startup service here... # end =end module Dante class Runner # Signal to application that the process is shutting down class Abort < Exception; end - attr_accessor :options + attr_accessor :options, :name, :description class << self def run(*args, &block) self.new(*args, &block) end end def initialize(name, defaults={}, &block) @name = name @startup_command = block - self.options = { + @options = { :host => '0.0.0.0', :pid_path => "/var/run/#{@name}.pid" }.merge(defaults) + end + # Accepts options for the process + # @runner.with_options { |opts| opts.on(...) } + def with_options(&block) + @with_options = block + end + + # Executes the runner based on options + # @runner.execute + # @runner.execute { ... } + def execute(&block) parse_options + kill_pid(options[:kill] || '*') if options.include?(:kill) - if options.include?(:kill) - kill_pid(options[:kill] || '*') - end - Process.euid = options[:user] if options[:user] Process.egid = options[:group] if options[:group] - end - # Executes the runner based on options - def execute! + @startup_command = block if block_given? + if !options[:daemonize] start else daemonize end @@ -63,24 +70,25 @@ trap("TERM"){ stop exit } - @startup_command.call + @startup_command.call(self.options) if @startup_command end def stop raise Abort sleep(1) end def parse_options + headline = [@name, @description].compact.join(" - ") OptionParser.new do |opts| opts.summary_width = 25 - opts.banner = ["#{@name} (#{VERSION})\n\n", - "Usage: #{@name} [-P file] [-d] [-k port]\n", - " #{@name} --help\n"].join("") + opts.banner = [headline, "\n\n", + "Usage: #{@name} [-p port] [-P file] [-d] [-k]\n", + " #{@name} --help\n"].compact.join("") opts.separator "" opts.on("-p", "--port PORT", Integer, "Specify port", "(default: #{options[:port]})") do |v| options[:port] = v end @@ -107,9 +115,12 @@ opts.on_tail("-?", "--help", "Display this usage information.") do puts "#{opts}\n" exit end + + # Load options specified through 'with_options' + instance_exec(opts, &@with_options) if @with_options end.parse! options end private \ No newline at end of file