Rails:何时+ delayed_job优化rake任务?

时间:2021-10-19 01:15:31

I use whenever to call rake tasks throughout the day, but each task launches a new Rails environment. How can I run tasks throughout the day without relaunching Rails for each job?

我每天都会调用rake任务,但是每个任务都会启动一个新的Rails环境。我如何在一天中运行任务而不重新启动每个作业的Rails ?

Here's what I came up with, would love to get some feedback on this...?

这是我想到的,想要得到一些反馈关于这个…?

  1. Refactor each rake task to instead be a method within the appropriate Model.

    将每个rake任务重构为适当模型中的一个方法。

  2. Use the delayed_job gem to assign low priority and ensure these methods run asynchronously.

    使用delayed_job gem分配低优先级并确保这些方法异步运行。

  3. Instruct whenever to call each Model.method instead of calling the rake task

    指示何时调用每个模型。方法而不是调用rake任务

Does this solution make sense? Will it help avoid launching a new Rails environment for each job? .. or is there a better way to do this?

这个解决方案有意义吗?它是否有助于避免为每个工作启动一个新的Rails环境?. .或者有更好的方法吗?

--

- - -

Running Rails 3

运行Rails 3

3 个解决方案

#1


4  

You could certainly look into enqueuing delayed_jobs via cron, then having one long running delayed_job worker.

您当然可以通过cron查询delayed_jobs队列,然后使用一个长时间运行的delayed_job worker。

Then you could use whenever to help you create the delayed_job enqueuing methods. It's probably easiest to have whenever's cron output call a small wrapper script which loads active_record and delayed_job directly rather than your whole rails stack. http://snippets.aktagon.com/snippets/257-How-to-use-ActiveRecord-without-Rails

然后您可以随时使用它来帮助您创建delayed_job队列方法。每当cron输出调用一个小的包装器脚本时,可能最容易的方法是直接加载active_record和delayed_job,而不是直接加载整个rails堆栈。http://snippets.aktagon.com/snippets/257-How-to-use-ActiveRecord-without-Rails

You might also like to look into clockwork.rb, which is a long-running process that would do the same thing you're using cron for (enqueuing delayed_jobs): http://rubydoc.info/gems/clockwork/0.2.3/frames

你也可以看看发条。rb是一个长时间运行的流程,它将执行与您正在使用cron(队列delayed_jobs)相同的操作:http://rubydoc.info/gems/clockwork/0.2.3/frame。

You could also just try using a requeuing strategy in your delayed_jobs: https://gist.github.com/704047

您也可以尝试在delayed_jobs中使用一个请求策略:https://gist.github.com/704047

#2


0  

Lots of good solutions to this problem, the one I eventually ended up integrating is as such:

这个问题有很多很好的解决方案,我最后积分的是这样的:

  1. Moved my rake code to the appropriate models
  2. 将rake代码移动到适当的模型
  3. Added controller/routing code for calling models methods from the browser
  4. 添加用于从浏览器调用模型方法的控制器/路由代码
  5. Configured cronjobs using the whenever gem to run command 'curl mywebsite.com/model#method'
  6. 使用时gem配置cronjobs来运行命令“curl mywebsite.com/model#方法”

I tried giving delayed_job a go but didn't like the idea of running another Rails instance. My methods are not too server intensive, and the above solution allows me to utilize the already-running Rails environment.

我尝试了delayed_job,但是不喜欢运行另一个Rails实例。我的方法不需要太多的服务器,上面的解决方案允许我使用已经运行的Rails环境。

#3


0  

Comment this line from schedule.rb

从schedule.rb注释这一行

require File.expand_path(File.dirname(__FILE__) + "/environment")

Instead load only required ruby files like models in your case.

相反,只加载需要的ruby文件,如您的案例中的模型。

#1


4  

You could certainly look into enqueuing delayed_jobs via cron, then having one long running delayed_job worker.

您当然可以通过cron查询delayed_jobs队列,然后使用一个长时间运行的delayed_job worker。

Then you could use whenever to help you create the delayed_job enqueuing methods. It's probably easiest to have whenever's cron output call a small wrapper script which loads active_record and delayed_job directly rather than your whole rails stack. http://snippets.aktagon.com/snippets/257-How-to-use-ActiveRecord-without-Rails

然后您可以随时使用它来帮助您创建delayed_job队列方法。每当cron输出调用一个小的包装器脚本时,可能最容易的方法是直接加载active_record和delayed_job,而不是直接加载整个rails堆栈。http://snippets.aktagon.com/snippets/257-How-to-use-ActiveRecord-without-Rails

You might also like to look into clockwork.rb, which is a long-running process that would do the same thing you're using cron for (enqueuing delayed_jobs): http://rubydoc.info/gems/clockwork/0.2.3/frames

你也可以看看发条。rb是一个长时间运行的流程,它将执行与您正在使用cron(队列delayed_jobs)相同的操作:http://rubydoc.info/gems/clockwork/0.2.3/frame。

You could also just try using a requeuing strategy in your delayed_jobs: https://gist.github.com/704047

您也可以尝试在delayed_jobs中使用一个请求策略:https://gist.github.com/704047

#2


0  

Lots of good solutions to this problem, the one I eventually ended up integrating is as such:

这个问题有很多很好的解决方案,我最后积分的是这样的:

  1. Moved my rake code to the appropriate models
  2. 将rake代码移动到适当的模型
  3. Added controller/routing code for calling models methods from the browser
  4. 添加用于从浏览器调用模型方法的控制器/路由代码
  5. Configured cronjobs using the whenever gem to run command 'curl mywebsite.com/model#method'
  6. 使用时gem配置cronjobs来运行命令“curl mywebsite.com/model#方法”

I tried giving delayed_job a go but didn't like the idea of running another Rails instance. My methods are not too server intensive, and the above solution allows me to utilize the already-running Rails environment.

我尝试了delayed_job,但是不喜欢运行另一个Rails实例。我的方法不需要太多的服务器,上面的解决方案允许我使用已经运行的Rails环境。

#3


0  

Comment this line from schedule.rb

从schedule.rb注释这一行

require File.expand_path(File.dirname(__FILE__) + "/environment")

Instead load only required ruby files like models in your case.

相反,只加载需要的ruby文件,如您的案例中的模型。