Rails 帮助从 rake 文件创建delayed_job

发布于 2024-11-01 10:09:19 字数 885 浏览 0 评论 0原文

如何从 rake 文件创建延迟作业。我应该如何将其移动到控制器中并创建一个每隔 15 分钟运行一次任务的elasto_job。

以下是我的 rake 文件的示例:

namespace :reklamer do

    task :runall => [:iqmedier, :euroads, :mikkelsen] do
      # This will run after all those tasks have run
    end

      task :iqmedier => :environment do
              require 'Mechanize'
              agent = WWW::Mechanize.new
              agent.get("http://www.iqmedier.dk")
      end
      task :euroads => :environment do
            require 'Mechanize'
            require 'pp'
            agent = Mechanize.new { |agent|
      end

      task :mikkelsen => :environment do
            require 'Mechanize'
            agent = Mechanize.new
            agent.get("http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php")
       end
    end

我应该将什么更改为延迟作业(https://github.com/collectiveidea/delayed_job)?

How do I create a delayed job from a rake file. How should I move it into a controller and create a delayed_job that runs the task every 15 minutes.

Here is an example how my rake file:

namespace :reklamer do

    task :runall => [:iqmedier, :euroads, :mikkelsen] do
      # This will run after all those tasks have run
    end

      task :iqmedier => :environment do
              require 'Mechanize'
              agent = WWW::Mechanize.new
              agent.get("http://www.iqmedier.dk")
      end
      task :euroads => :environment do
            require 'Mechanize'
            require 'pp'
            agent = Mechanize.new { |agent|
      end

      task :mikkelsen => :environment do
            require 'Mechanize'
            agent = Mechanize.new
            agent.get("http://affilate.mikkelsenmedia.dk/partnersystem/mylogins.php")
       end
    end

What should I change to be a delayed job (https://github.com/collectiveidea/delayed_job)?

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

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

发布评论

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

评论(2

愛上了 2024-11-08 10:09:19

DelayedJob 本身不会对您有帮助,因为它是基于一次性工作的。您仍然需要定期运行的东西来创造这些工作。

假设:

  1. 你在 Heroku 上并且只能获得 1 小时的 cron,
  2. 你需要每 15 分钟运行一次作业

你可以做这样的事情......

为你的作业创建一个类:

class MechanizeJob < Struct.new(:url)
  def perform
    agent = Mechanize.new
    agent.get(url)
  end
end

从你的 Rakefile 安排作业:

task :schedulejobs => :environment do
  urls = ["http://...", "http://...", "http://..."]
  urls.each do |url|
    # 1 is the job priority
    Delayed::Job.enqueue MechanizeJob.new(url), 1, Time.now
    Delayed::Job.enqueue MechanizeJob.new(url), 1, 15.minutes.from_now
    Delayed::Job.enqueue MechanizeJob.new(url), 1, 30.minutes.from_now
    Delayed::Job.enqueue MechanizeJob.new(url), 1, 45.minutes.from_now
  end
end

这将每 15 分钟针对每个 url 运行一个作业。

DelayedJob alone will not help you since it is based around one-time jobs. You will still need something that runs on a regular basis that creates these jobs.

Assuming:

  1. you're on Heroku and can only get a 1-hour cron
  2. you need to run a job every 15 minutes

You can do something like this...

Make a class for your jobs:

class MechanizeJob < Struct.new(:url)
  def perform
    agent = Mechanize.new
    agent.get(url)
  end
end

Schedule the jobs from your Rakefile:

task :schedulejobs => :environment do
  urls = ["http://...", "http://...", "http://..."]
  urls.each do |url|
    # 1 is the job priority
    Delayed::Job.enqueue MechanizeJob.new(url), 1, Time.now
    Delayed::Job.enqueue MechanizeJob.new(url), 1, 15.minutes.from_now
    Delayed::Job.enqueue MechanizeJob.new(url), 1, 30.minutes.from_now
    Delayed::Job.enqueue MechanizeJob.new(url), 1, 45.minutes.from_now
  end
end

This will run a job per url every 15 minutes.

血之狂魔 2024-11-08 10:09:19

建议您看一下 SimpleWorker,这是一个用于 Ruby 应用程序的基于云的后台处理/工作队列。它专为卸载任务、运行计划作业以及横向扩展以同时处理许多并行作业而设计。它简单、经济且可扩展。

(披露,我为公司工作。)

您创建您的工作人员(在应用程序/工作人员中),然后在您的控制器和其他地方将它们排队以立即运行或安排它们稍后运行或定期运行,只需几行代码。这是一个基本示例。

  worker = ReportWorker.new
  worker.user_id = @current_user.id
  worker.schedule(:start_at => 1.hours.since, :run_every => 900)

  #Or to run once right away
  #worker.queue

ReportWorker 类将包含为当前用户创建报告并根据需要发送或发布报告的逻辑。

Suggest you take a look at SimpleWorker, a cloud-based background processing / worker queue for Ruby apps. It's designed for offloading tasks, running scheduled jobs, and scaling out to handle many parallel jobs at once. It's simple, affordable, and scalable.

(Disclosure, I work for the company.)

You create your workers (in app/worker) and then in your controllers and elsewhere queue them up to run right away or schedule them for later or on a recurring basis with just a few lines of code. Here's a basic example.

  worker = ReportWorker.new
  worker.user_id = @current_user.id
  worker.schedule(:start_at => 1.hours.since, :run_every => 900)

  #Or to run once right away
  #worker.queue

The ReportWorker class would contain the logic to create the report for the current user and sent it or post it needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文