lib/dante/runner.rb in dante-0.1.5 vs lib/dante/runner.rb in dante-0.2.0

- old
+ new

@@ -16,11 +16,11 @@ module Dante class Runner MAX_START_TRIES = 5 - attr_accessor :options, :name, :description + attr_accessor :options, :name, :description, :verify_options_hook class << self def run(*args, &block) self.new(*args, &block) end @@ -48,10 +48,12 @@ # @runner.execute { ... } def execute(opts={}, &block) parse_options self.options.merge!(opts) + @verify_options_hook.call(self.options) if @verify_options_hook + if options.include?(:kill) self.stop else # create process self.stop if options.include?(:restart) @@ -74,20 +76,25 @@ end def daemonize return log("Process is already started") if self.daemon_running? # daemon already started + if !options[:log_path] + options[:log_path] = "/var/log/#{@name}.log" + end + # Start process pid = fork do exit if fork Process.setsid exit if fork store_pid(Process.pid) File.umask 0000 redirect_output! start end + Process.waitpid pid # Ensure process is running if until_true(MAX_START_TRIES) { self.daemon_running? } log "Daemon has started successfully" true else # Failed to start @@ -97,10 +104,14 @@ end def start log "Starting #{@name} service..." + if log_path = options[:log_path] && options[:daemonize].nil? + redirect_output! + end + trap("INT") { interrupt exit } @@ -173,10 +184,10 @@ opts.on("-d", "--daemon", "Daemonize mode") do |v| options[:daemonize] = v end - opts.on("-l", "--log FILE", String, "Logfile for output") do |v| + opts.on("-l", "--log FILE", String, "Logfile for output", "(default: /var/log/#{@name}.log)") do |v| options[:log_path] = v end opts.on("-k", "--kill [PORT]", String, "Kill specified running daemons - leave blank to kill all.") do |v| options[:kill] = v