2014-01-17 28 views
3

我正在使用守護進程工具包啓動後臺ruby進程,用於監聽Amazon SQS消息。一旦接收到消息,它就會啓動一個需要在JRuby中運行的Open3.popen3的子進程。在不同的ruby版本中啓動子進程

後臺進程需要在MRI中運行,因爲守護進程工具包使用Process.daemon來守護進程。但到目前爲止,我還無法強制子進程在JRuby中運行。

我使用rbenv管理紅寶石版本,所以起初我以爲這會工作:

Open3.popen3({"RUBY_VERSION" => "jruby-1.7.8"}, "rp5 run /path/to/sketch.rb") do |stdin, stdout, stderr, wait_thr| 
    # read stderr and stdout for status and error information .... 
end 

但是在子進程輸出我得到的錯誤:「rbenv:JRuby的:命令未找到」

然後我跟蹤了rbenv如何運行它的可執行文件,以便我可以繞過rbenv並直接在JRuby中運行rp5可執行文件。

我第一次發現該文件夾中的可執行RP5:〜/ .rbenv /版本/ JRuby的1.7.8 /斌/ RP5

#!/Users/fede/.rbenv/versions/jruby-1.7.8/bin/jruby 
# 
# This file was generated by RubyGems. 
# 
# The application 'ruby-processing' is installed as part of a gem, and 
# this file is here to facilitate running it. 
# 

require 'rubygems' 

version = ">= 0" 

if ARGV.first 
    str = ARGV.first 
    str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding 
    if str =~ /\A_(.*)_\z/ 
    version = $1 
    ARGV.shift 
    end 
end 

gem 'ruby-processing', version 
load Gem.bin_path('ruby-processing', 'rp5', version) 

然後我執行的Gem.bin_path方法找到它的可執行RP5它在呼喚。這是在寶石內:〜/ .rbenv/versions/jruby-1.7.8/lib/ruby​​/gems/shared/gems/ruby​​-processing-2.3.1/bin/rp5然後我試着運行子進程這個rp5可執行文件直接:

Open3.popen3("~/.rbenv/versions/jruby-1.7.8/lib/ruby/gems/shared/gems/ruby-processing-2.3.1/bin/rp5 run path/to/sketch.rb") do |stdin, stdout, stderr, wait_thr| 
    # read stderr and stdout for status and error information .... 
end 

但我仍然得到相同的「jruby命令未找到」錯誤。

然後我考察該可執行文件:

#!/usr/bin/env ruby 

file = __FILE__ 
if test(?l, file) 
    require "pathname" 
    file = Pathname.new(file).realpath 
end 

require File.expand_path(File.dirname(file) + "/../lib/ruby-processing") 
Processing::Runner.execute 

所以確實在頂部的家當意味着這個可執行文件是使用默認的紅寶石版本?

甚至可以在完全不同的ruby版本中啓動子進程嗎?

謝謝。

回答

0

您是否嘗試過通過簡單地運行ruby -S「強制」它在解釋器中運行,例如, :

Open3.popen3("/usr/bin/ruby ~/.rbenv/versions/jruby-1.7.8/lib/ruby/gems/shared/gems/ruby-processing-2.3.1/bin/rp5 run path/to/sketch.rb") do |stdin, stdout, stderr, wait_thr| 
    # ... 
end 

所以先找了MRI紅寶石可執行文件位於比用它來代替/usr/bin/ruby應該簡單地做你找什麼?

0

這是因爲不正確的「 〜'的家庭性格。你需要明確地將〜轉換爲絕對路徑。

如果你的〜是/ home /用戶名/然後:

Open3.popen3("/home/username/.rbenv/versions/jruby-1.7.8/lib/ruby/gems/shared/gems/ruby-processing-2.3.1/bin/rp5 run path/to/sketch.rb") do |stdin, stdout, stderr, wait_thr| 
    # read stderr and stdout for status and error information .... 
end 
0

謝謝您的回答。最後,這只是設置正確的ENV變量的問題。這是我不得不改變的ENV變量。

  1. rbenv使用RBENV_VERSION變量,不推薦使用RUBY_VERSION變量。
  2. 我啓動過程的環境甚至沒有PATH到rbenv shims目錄。
  3. 我試圖啓動的過程依賴於將gem捆綁在Gemfile中,所以我還必須將BUNDLE_GEMFILE env變量設置爲我的Gemfile的路徑。