2012-02-03 58 views
0

我有一個JRuby/Rails web應用程序。我使用rbenv和JRuby在本地進行測試(使用「rails server」),並且它工作正常。 我還將生產版本作爲WAR文件分發並在Tomcat中運行,並且該機器沒有安裝JRuby。有用。 我現在需要能夠使用Resque和Redis來處理長時間運行的作業。我在本地安裝Resque/Redis的和命令行運行Resque 作爲如何在沒有安裝JRuby並僅使用Java的JRuby/Tomcat deploymet中使用Resque應用運行Ruby/Rails?

linux> "QUEUE=* bundle exec rake environment resque:work 

它的工作原理。我創建了一個小工人類(FooClass)剛剛打印出的參數,由Resque, 打來電話,我可以通過運行

irb(main):041:0>Resque.enqueue(FooWorker, [1,"4"]) 
在Rails的控制檯

入隊的請求,並Resque最終處理請求並打印[1,「4」]

我想能夠在Tomcat/Java環境(沒有安裝JRuby)中運行此Resque Rake任務, ,我有3個選項可以運行Resque耙任務。

  1. 在Tomcat中運行它,但在單獨的Java線程中運行。我寧願不這樣做,因爲我希望能夠殺死工作進程並重新啓動它。
  2. 從命令行運行它,並在啓動時通過etc/init.d調用該命令。
  3. 從Web應用程序的單獨Tomcat運行它。

我鶯配置使得下列文件存在於應用程序WAR文件,並獲得爆炸到的webapps/WEB-INF 出來的webapps/ABC/WEB-INF當Tomcat啓動應用程序。

Gemfile 
Rakefile 
web.xml 
lib/jruby-core-1.6.4.jar 
lib/jruby-rack-1.0.10.jar 
lib/jruby-stdlib-1.6.4.jar 
lib/tasks/abc.rake 
lib/tasks/resque.rake 
gems/gems/bundler-1.0.21 
gems/gems/warbler-1.3.2 
gems/gems/rails-3.0.10 
(other gem files in gems/gems) 
config/warble.rb 

文件的config/warble.rb看起來是這樣的:

Warble::Config.new do |config| 
    config.dirs=%w{app config lib lob vendor tmp} 
    config.includes=FileList["./Rakefile"] 
    config.webxml.jruby.compat.version = "1.9"  
end 

,將文件lib /任務/ resque.rake有

require "resque/tasks" 
task "resque:setup" => :environment 
task "resque:work" => :environment 

我的Gemfile包括下面幾行:

source 'http://rubygems.org' 
gem 'bundler', '1.0.21' 
gem 'rails', '3.0.10' 
gem 'rake', '0.8.7' 
gem 'resque' 

在網上搜索產生運行Rake任務(從WEB-INF /內)通過以下方式,而不使用JRuby:

linux> java -cp lib/jruby-core-1.6.4.jar -S rake 

的結果

Unrecognized option: -S 
Could not create the Java virtual machine 

只是爲了好玩,我試圖

linux> java -jar lib/jruby-core-1.6.4.jar -S rake 

結果是

jruby: No such file or directory -- rake (LoadError) 

然後我嘗試下載jruby-complete-1.6。4到那個目錄,我跑

linux> java -jar lib/jruby-complete-1.6.4.jar -S rake -T 
(in /WEB-INF) 
rake aborted! 
no such file to load -- bundler/setup 
/WEB-INF/Rakefile:4:in `(root)' 

在這一點上,我在一個完整的損失。如何在Java/Tomcat或Java環境中運行我所需的Resque Rake任務,而無需在該服務器上安裝JRuby?

回答

0

所以,你需要做的基本事情是編寫一個小的shell腳本,並從解壓後的war文件的WEB-INF目錄中運行它。

  1. 確保您擁有此腳本以及Rakefile以及包含在war文件中的任何其他支持腳本(例如db/migrations)。
  2. 腳本看起來應該像下面這樣:
 
#!/bin/bash 
# 
# Put in WEB-INF/; call this rake.sh or whatever you prefer. 

dir=$(dirname $0) 

# Put all jar files in the classpath 
for jar in $dir/*; do 
    CLASSPATH="$jar:$CLASSPATH" 
done 

# You might need to adjust Java memory settings; currently JRuby sets 512m by default 
JAVA_OPTS=-Xmx512m 

# Set up GEM_HOME and GEM_PATH 
GEM_HOME=$dir/gems 
GEM_PATH=$GEM_HOME 

export CLASSPATH GEM_HOME GEM_PATH 

java $JAVA_OPTS org.jruby.Main -S rake [email protected] 
1

或者您可以在守護線程使用https://github.com/kares/jruby-rack-worker您的應用程序(而不是單獨的耙進程)運行Resque。

只需要配置鶯(或您的的Gemfile),包括JRuby的架工寶石(或者你也可以下載.jar和告訴鶯它放置在您的WEB-INF/lib目錄),並設置部署描述符中的Resque。用鶯創建config/web.xml.erb文件並在其中放置以下代碼:

<!DOCTYPE web-app PUBLIC 
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app> 
<% webxml.context_params.each do |k,v| %> 
    <context-param> 
    <param-name><%= k %></param-name> 
    <param-value><%= v %></param-value> 
    </context-param> 
<% end %> 

    <filter> 
    <filter-name>RackFilter</filter-name> 
    <filter-class>org.jruby.rack.RackFilter</filter-class> 
    </filter> 
    <filter-mapping> 
    <filter-name>RackFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <listener> 
    <listener-class><%= webxml.servlet_context_listener %></listener-class> 
    </listener> 

<% if webxml.jndi then [webxml.jndi].flatten.each do |jndi| %> 
    <resource-ref> 
    <res-ref-name><%= jndi %></res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
    </resource-ref> 
<% end; end %> 

    <!-- jruby-rack-worker setup using the built-in libraries support : --> 

    <context-param> 
    <param-name>jruby.worker</param-name> 
    <param-value>resque</param-value> 
    </context-param> 

    <listener> 
    <listener-class>org.kares.jruby.rack.WorkerContextListener</listener-class> 
    </listener> 

</web-app> 
相關問題