2014-01-17 25 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的路径。