当部署为 WAR 时,JRuby 中的线程安全 Resque Workers
目前我们在JRuby中使用Resque,并且在开发时我们使用两种方式启动Worker。
- 使用 Rake:
QUEUE=* jruby -J-cp /path/to/library -S rake environment resque:work
- 以编程方式,通过 Sinatra Rack 应用程序(或其他应用程序)初始化,最终调用一个类:
def start
@worker = Resque::Worker.new(@queues)
@worker.verbose = @vervose
@worker.work(@interval)
end
def stop
@worker.try(:shutdown)
end
尽管这两种解决方案对于我们的开发来说都是可以接受的。例如,我担心部署到 Tomcat 时这将如何工作。
通常在 Ruby 中,您会生成或妖魔化工作线程,然后使用监视工具来监视 pid。
在部署时以编程方式启动 Workers 是否有意义?我想知道它是否在 java 中启动一个新线程或使 jruby 进程混乱,如果不是,我应该使用另一个调度库(如quartz)来启动工作程序?或者在部署任务时启动的 rake 任务?
我可以创建一个工作人员模型,然后让它跟踪数据库中的工作人员,但这对我来说没有意义。
任何帮助或知识将不胜感激。
谢谢。
参考文献:
https://github.com/defunkt/resque
http://rubydoc.info/github/defunkt/resque/master/Resque/
http://blog.thomasmango.com/post/636319317/resque-in-Production
Currently we are using Resque in JRuby and we use two ways of starting a Worker when developing.
- Using Rake:
QUEUE=* jruby -J-cp /path/to/library -S rake environment resque:work
- Programmatically, initialized through a Sinatra Rack App (or whatever), eventually calling a class with:
def start
@worker = Resque::Worker.new(@queues)
@worker.verbose = @vervose
@worker.work(@interval)
end
def stop
@worker.try(:shutdown)
end
Although both these solutions are acceptable for us in development. I am concern about how this would work when deployed to Tomcat for example.
In Ruby normally, you would spawn or demonize workers, then use a monitoring tool to watch the pid's.
Would it make sense to start the Workers programmatically when deployed? I'm wondering if it starts a new thread in java or clutters the jruby process, if not should I use, yet another scheduling library like quartz to start a worker? or a rake task that launches at deploy task?
I could create a Worker model and then have that keep track of the workers in a db, but that doesn't make sense to me.
Any help or knowledge will be appreciated.
Thank You.
Refs:
https://github.com/defunkt/resque
http://rubydoc.info/github/defunkt/resque/master/Resque/
http://blog.thomasmango.com/post/636319317/resque-in-production
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 rake resque:work 将启动 Rails 环境并启动一个工作进程,该工作进程会轮询作业,将自身分叉到一个(子)工作进程中,处理该作业,然后退出。
如果您想运行多个工作程序,请使用
COUNT
变量:QUEUE=* COUNT=5 jruby -J-cp /path/to/library -S rakeenvironment resque:work
这方面的哪些方面将部署到 Tomcat?这都是命令行。
Using
rake resque:work
will boot your Rails environment and start one worker, which polls for a job, forks itself into a (sub-)worker process, processes that job and then quits.If you want to run multiple workers then use the
COUNT
variable:QUEUE=* COUNT=5 jruby -J-cp /path/to/library -S rake environment resque:work
What aspect of this would be deployed to Tomcat? This is all command line.
晚了一年多,但我认为您正在寻找的答案是在(本机)Java 线程中与您的应用程序一起运行 Resque 工作线程。由于您使用的是 JRuby::Rack,答案是 JRuby::Rack::Worker 。将其添加到您的web.xml中:
如果您正在颤抖,这里有一个web.xml.erb示例https://github.com/kares/jruby-rack-worker 只需创建一个 config/web.xml.erb 文件并将内容复制到其中...如果您碰巧使用 Trinidad,则会在其之上构建一个 扩展 (因此您不需要设置 web.xml 并复制 .jar 文件)。
More than a year late but I think the answer you were looking for is to run Resque workers along side your application in (native) Java threads. Since you're using JRuby::Rack the answer is JRuby::Rack::Worker. Add this to your web.xml :
If you're warbling there's a web.xml.erb sample https://github.com/kares/jruby-rack-worker just create a config/web.xml.erb file and copy the content to it ... If you happen to use Trinidad there's an extension built on top of it (so you do not need to setup a web.xml and copy the .jar file).