如何在未安装 JRuby 且仅使用 Java 的 JRuby/Tomcat 部署中通过 Resque 应用程序运行 Ruby/Rails?
我有一个 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 任务。
- 在 Tomcat 中运行它,但在单独的 Java 线程中。我不想这样做,因为我希望能够终止工作进程并重新启动它。
- 从命令行运行它,并在启动时由 etc/init.d 调用该命令。
- 在与 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.
- 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.
- Run it from the command line, and have that command called by etc/init.d at startup.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者,您可以使用 https:// /github.com/kares/jruby-rack-worker。
只需配置 Warbler(或您的 Gemfile)以包含 jruby-rack-worker gem(或者您可以下载 .jar 并告诉 Warbler 将其放置在您的 WEB-INF/lib 目录中)并进行设置部署描述符中的 Resque。使用 Warbler 创建 config/web.xml.erb 文件并放入以下代码:
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 :
因此,为此您需要做的基本事情是编写一个小的 shell 脚本并从解压的 war 文件的 WEB-INF 目录运行它。
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.
Rakefile
and any other supporting scripts (e.g., db/migrations) included in the war file.