我正在使用守護進程工具包啓動後臺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版本中啓動子進程嗎?
謝謝。