首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > Ruby Rails >

ruby 兑现windows service启动和关闭 nginx和jruby trinidad

2013-04-20 
ruby 实现windows service启动和关闭 nginx和jruby trinidadrequire win32/serviceinclude Win32class S

ruby 实现windows service启动和关闭 nginx和jruby trinidad
require "win32/service"include Win32class String; def to_dos() self.tr('/','\\') end endclass String; def from_dos() self.tr('\\','/') end end#override to support utf8class String; def strip() self end endrubyexe="C:/bin/ruby.exe".to_dosSERVICE_FILE= (File.expand_path(File.dirname(File.dirname(__FILE__)))+ '/service.rb').to_dosSERVICE_NAME="Test Service"if ARGV[0] case ARGV[0] when "install" Service.new( :service_name => SERVICE_NAME, :service_type => Service::WIN32_OWN_PROCESS, :start_type => Service::AUTO_START, :error_control => Service::ERROR_NORMAL, :binary_path_name => "#{rubyexe} #{SERVICE_FILE}", :description => 'Run nginx, jruby all in one' ) when "uninstall" Service.delete(SERVICE_NAME) endend

?注意:为何重载String.strip

原因在于中文版windows在安装服务过程中调用get_last_error会返回utf-8的字符,此时win32-service gem的error.rb会报invalidate byte sequence的错误。

原因是对字符串进行了strip

如果不进行strip则没有任何问题。

?

服务部分的代码如下:

#service.rbrequire 'win32/daemon'include Win32SERVICE_LOG = File.expand_path(File.dirname(File.dirname(__FILE__)))+ '/services.log'JR_PATH = 'c:\jruby-1.7.0\bin'JR_LOG = File.expand_path(File.dirname(File.dirname(__FILE__)))+ '/jruby.log'class Daemon  def service_main     while running?        if @pid.nil?          @pid = Process.spawn('c:\nginx-1.2.1\nginx.exe',             chdir: 'c:\nginx-1.2.1',            out: SERVICE_LOG, err: :out)          Process.detach @pid          if @pid_jruby.nil?            @pid_jruby = []            start_jruby          end                   Process.waitpid(@pid)        else           sleep(3)        end     end  end  def service_stop     if @pid        pid_kill = Process.spawn('c:\nginx-1.2.1\nginx.exe -s stop',             chdir: 'c:\nginx-1.2.1',            out: SERVICE_LOG, err: :out)          Process.waitpid(pid_kill)     end     if @pid_jruby.size > 0        @pid_jruby.each do |pid|           pid_kill = Process.kill(9, pid)       end     end   end  def start_jruby    if @pid_jruby.empty?      [3000, 4000].each do |port|        cmd = "#{JR_PATH}\\jruby.exe -S trinidad -e production -p #{port}"        pid =  Process.spawn(cmd, chdir: 'C:\app\', out:JR_LOG, err: :out)        @pid_jruby << pid        Process.detach pid      end    end  endendDaemon.mainloop

?结果:

以上代码在ruby 1.9.3 win7 上通过

如在win7上报安装权限不足,请使用administrator 启动cmd console。

代码尚有refactory的空间,时间仓促,敬请谅解。

?

?

热点排行