lib/ginst/web_server.rb in ginst-0.2.2 vs lib/ginst/web_server.rb in ginst-2.0.0

- old
+ new

@@ -1,43 +1,65 @@ -require 'daemons' -module Ginst - module WebServer - class << self - - SERVER=File.dirname(__FILE__)+'/../app/webserver.rb' - DAEMON_OPT = { - :app_name => "ginst", - :dir_mode => :normal, - :ARGV => ["status"], - :dir => Dir.pwd, - :multiple => false, - :ontop => false, - :mode => :exec, - :monitor => true - } +class Ginst::WebServer + + require('daemons') - def start - fork{Daemons.run(SERVER,build_opts('start'))} + def self.start + execute('start') + end + + def self.restart + execute('restart') + end + + def self.stop + execute('stop') + end + + def self.status + execute('status') + end + + def self.run + execute('run') + end + + private + + def self.execute(command) + script = Ginst.root+'/script/server' + + if command == 'status' + capture_output do + Daemons.run(script, generate_options_for_command(command)) end - - def stop - Daemons.run(SERVER,build_opts('stop')) + else + pid = fork do + Daemons.run(script, generate_options_for_command(command)) + exit(0) end - - def status - Daemons.run(SERVER,build_opts('status')) - end - - private - - def build_opts(action) - case action - when 'start' - DAEMON_OPT.merge({:ARGV => [action, '--', '-e production']}) - else - DAEMON_OPT.merge({:ARGV => [action]}) - end - end - + Process.waitpid2(pid) + status end end -end \ No newline at end of file + + def self.capture_output + $stdout, $stderr = StringIO.new, StringIO.new + yield + $stdout.string + $stderr.string + ensure + $stdout = STDOUT + $stderr = STDERR + end + + def self.generate_options_for_command(command) + options = { + :app_name => 'ginst', + :dir_mode => :normal, + :dir => File.expand_path((Ginst.data_dir || ENV['GINST_DIR'])+'/tmp'), + :monitor => false, + :mode => :exec, + :log_output => true, + :ARGV => [command] + } + end +end +