lib/mail_sandbox/runner.rb in mail_sandbox-0.0.7 vs lib/mail_sandbox/runner.rb in mail_sandbox-0.1.0
- old
+ new
@@ -1,7 +1,8 @@
require "eventmachine"
require 'optparse'
+require 'simple_pid'
require 'fileutils'
module MailSandbox
class Runner
@@ -20,10 +21,18 @@
opts.on("-E", "--environment", "Environment") do |f|
config.environment = f
end
+ opts.on("-D", "--daemonize", "Run daemonized") do |f|
+ config.daemonize = f
+ end
+
+ opts.on("-P", "--pid FILE", "File to store PID") do |f|
+ config.pidfile = ::File.expand_path(f)
+ end
+
end.parse!
end
def configure
config.load_from_yml_file(env) if config.config_file
@@ -37,25 +46,48 @@
when :error then Logger::ERROR
when :warn then Logger::WARN
when :debug then Logger::DEBUG
end
STDOUT.sync = true
- MailSandbox::Signals.trap
+ MailSandbox::Signals.trap(self)
MailSandbox::Server.parms = config.server_params
end
def start
configure
+ Process.daemon if config.daemonize
+ write_pidfile if config.pidfile
+
MailSandbox.logger.info "Start MailSandbox::Server on #{config.listen}:#{config.port}"
EventMachine::run {
EventMachine::start_server config.listen, config.port, MailSandbox::Server
}
end
def env
config.environment || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || :development
+ end
+
+ def write_pidfile
+ @simple_pid = SimplePid.new(config.pidfile)
+
+ if @simple_pid.exists?
+ unless @simple_pid.running?
+ @simple_pid.cleanup
+ @simple_pid.write!
+ end
+ else
+ @simple_pid.write!
+ end
+
+ end
+
+ def terminate
+ MailSandbox.logger.info "Got quit/terminate signal. Bye."
+ @simple_pid.cleanup if @simple_pid
+ exit
end
end
end