问题描述
我在之前的代码中使用 resque-scheduler 延迟作业:
I used resque-scheduler for delay jobs in previous code:
Resque.enqueue_in(options[:delay].seconds, self, context)
现在我想包括 resque-status 来完成这项工作,但不知道它们如何协同工作.最新的resque-status源码支持scheduler,源码如下:
Now I want to include resque-status to do the job but have no idea how they can work together. The latest resque-status source code supports scheduler, as in the source code:
https://github.com/quirkey/resque-status/blob/master/lib/resque/plugins/status.rb
# Wrapper API to forward a Resque::Job creation API call into a Resque::Plugins::Status call.
# This is needed to be used with resque scheduler
# http://github.com/bvandenbos/resque-scheduler
def scheduled(queue, klass, *args)
self.enqueue_to(queue, self, *args)
end
结束
但我不知道如何使用它.我应该只调用 SampleJob.scheduled(queue, myclass, :delay => delay) 而不是 SampleJob.create(options) 吗?
But I'm not sure how to use it. Shall I just call SampleJob.scheduled(queue, myclass, :delay => delay) instead of SampleJob.create(options)?
========================================================================
======================================================================
此外,还支持 resque-status(和其他自定义作业):
Also, there is Support for resque-status (and other custom jobs):
https://github.com/bvandenbos/resque-scheduler
某些 Resque 扩展(如 resque-status)使用 API 签名略有不同的自定义作业类.Resque-scheduler 并不试图支持所有现有和未来的自定义作业类,而是支持调度标志,以便您可以扩展自定义类并使其支持计划作业.
Some Resque extensions like resque-status use custom job classes with a slightly different API signature. Resque-scheduler isn't trying to support all existing and future custom job classes, instead it supports a schedule flag so you can extend your custom class and make it support scheduled job.
假设我们有一个名为 FakeLeaderboard 的 JobWithStatus 类
Let's pretend we have a JobWithStatus class called FakeLeaderboard
class FakeLeaderboard < Resque::JobWithStatus
def perform
# do something and keep track of the status
end
end
然后是时间表:
create_fake_leaderboards:
cron: "30 6 * * 1"
queue: scoring
custom_job_class: FakeLeaderboard
args:
rails_env: demo
description: "This job will auto-create leaderboards for our online demo and the status will update as the worker makes progress"
但它似乎只适用于经常性工作.我可以找到 cron 的参数,但不能找到延迟.那么我该如何处理延迟的工作呢?
But it seems only for recurring jobs. I can find params of cron, but not delay. So how can I handle delayed jobs with it?
谢谢!
推荐答案
resque-scheduler
将在提供的类上调用 scheduled
方法,每当它为您的作业创建作业时工人消费.它还很好地将队列名称和类名称作为字符串传递,以便您可以创建类级别方法来处理计划的作业创建.
resque-scheduler
will call the scheduled
method on the supplied class whenever it creates the job for your workers to consume. It also nicely passes both the queue name and class name as a string so you can create a class level method to handle scheduled job creation.
虽然 Fabio 的回答可以解决问题,但回答您的具体问题有点过度设计.假设您有一个 JobWithStatus
类,您的所有 resque-status
工作人员都继承自该类,您只需将 scheduled
方法添加到它即可使用 resque-scheduler
如下:
While Fabio's answer will solve the problem, it is a bit over-engineered to answer your specific question.Assuming you have a JobWithStatus
class that all of your resque-status
workers inherit from, you need only add the scheduled
method to it for it to work with the resque-scheduler
as so:
class JobWithStatus
include Resque::Plugins::Status
def self.scheduled(queue, klass, *args)
Resque.constantize(klass).create(*args)
end
end
这篇关于延迟作业的 resque-status 和 resque-scheduler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!