如何在未安装 JRuby 且仅使用 Java 的 JRuby/Tomcat 部署中通过 Resque 应用程序运行 Ruby/Rails?

发布于 2025-01-02 06:59:30 字数 2452 浏览 2 评论 0原文

我有一个 JRuby/Rails Web 应用程序。我使用 rbenv 和 JRuby 在本地进行测试(使用“rails 服务器”),并且它有效。 我还将生产版本作为 WAR 文件分发并在 Tomcat 中运行,并且该机器没有安装 JRuby。有用。 我现在需要能够使用 Resque 和 Redis 来处理长时间运行的作业。我在本地安装了Resque/redis并运行了Resque 从命令行运行

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 环境中运行此 Resque Rake 任务(其中没有安装 JRuby), 我有 3 个选项来运行 Resque Rake 任务。

  1. 在 Tomcat 中运行它,但在单独的 Java 线程中。我不想这样做,因为我希望能够终止工作进程并重新启动它。
  2. 从命令行运行它,并在启动时由 etc/init.d 调用该命令。
  3. 在与 Web 应用程序不同的 Tomcat 中运行它。

我的 warbler 配置为应用程序 WAR 文件中存在以下文件,并分解为 webapps/WEB-INF 当 Tomcat 启动应用程序时,输出到 webapps/abc/WEB-INF。

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/tasks/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?

I have a JRuby/Rails web application. I use rbenv and JRuby to test locally (using "rails server"), and it works.
I also distribute the production version as a WAR file and run in Tomcat, and that machine has no JRuby installation. It works.
I now need to be able to use Resque and Redis to process long-running jobs. I installed Resque/redis locally and ran Resque
from the command line as

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

It works. I created a little worker class (FooClass) which just prints out the parameter, is called by Resque,
and I am able to enqueue requests by running

irb(main):041:0>Resque.enqueue(FooWorker, [1,"4"])

in the Rails console, and the Resque eventually processes the request and prints [1,"4"]

I would like to be able to run this Resque Rake task in the Tomcat/Java environment (which does not have JRuby installed),
and I have 3 options to run the Resque Rake Task.

  1. Run it within the Tomcat, but in a separate Java thread. I prefer not to do this because I want to be able to kill the worker process and restart it.
  2. Run it from the command line, and have that command called by etc/init.d at startup.
  3. Run it in a separate Tomcat from the web app.

My warbler is configured such that the following files are present in the application WAR file, and get exploded to webapps/WEB-INF
out to webapps/abc/WEB-INF when Tomcat starts the app.

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

The file config/warble.rb looks like this:

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

The file lib/tasks/resque.rake has

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

My gemfile includes the following lines:

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

Searching on the web yielded the following ways of running Rake tasks (from within WEB-INF/) without using JRuby:

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

The result was

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

Just for fun, I tried

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

The result was

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

I then tried downloading jruby-complete-1.6.4 to that directory and I ran

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)'

At this point I am at a complete loss. How can I run my desired Resque Rake task in the Java/Tomcat or just Java environment without installing JRuby on that server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离去的眼神 2025-01-09 06:59:30

或者,您可以使用 https:// /github.com/kares/jruby-rack-worker

只需配置 Warbler(或您的 Gemfile)以包含 jruby-rack-worker gem(或者您可以下载 .jar 并告诉 Warbler 将其放置在您的 WEB-INF/lib 目录中)并进行设置部署描述符中的 Resque。使用 Warbler 创建 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>

Alternatively you can run Resque with your application in daemon Threads (instead of a separate rake process) using https://github.com/kares/jruby-rack-worker.

Simply configure Warbler (or your Gemfile) to include the jruby-rack-worker gem (alternatively you can download the .jar and tell Warbler it's to be placed in your WEB-INF/lib dir) and setup the Resque in the deployment descriptor. With Warbler create the config/web.xml.erb file and put there the following code :

<!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>
感情废物 2025-01-09 06:59:30

因此,为此您需要做的基本事情是编写一个小的 shell 脚本并从解压的 war 文件的 WEB-INF 目录运行它。

  1. 确保您的 war 文件中包含此脚本以及 Rakefile 和任何其他支持脚本(例如 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 $@

So, the basic thing you need to do for this is to write a small shell script and run it from the WEB-INF directory of the unpacked war file.

  1. Ensure you have this script as well as Rakefile and any other supporting scripts (e.g., db/migrations) included in the war file.
  2. Script should look like the following:
#!/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 $@
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文