Resque-Scheduler在Rails 4.2中不使用ActiveJob

时间:2014-09-16 09:17:07

标签: ruby-on-rails ruby-on-rails-4 resque resque-scheduler rails-activejob

有没有人能够让预定的工作在Rails 4.2中工作?

我正在使用resque,我正在尝试使用resque-scheduler来安排作业。我有一个计划加载和调度程序运行,甚至看起来它正在运行作业,但它没有做任何事情。

resque-scheduler: [INFO] 2014-09-16T01:54:25-07:00: Starting
resque-scheduler: [INFO] 2014-09-16T01:54:25-07:00: Loading Schedule
resque-scheduler: [INFO] 2014-09-16T01:54:25-07:00: Scheduling friends 
resque-scheduler: [INFO] 2014-09-16T01:54:25-07:00: Schedules Loaded
resque-scheduler: [INFO] 2014-09-16T01:54:55-07:00: queueing FriendsJob (friends)

我可以将这样的工作排队并加以处理。

TestJob.enqueue(params[:id])

我在工作日志中得到了这个输出

got: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])
** [01:24:01 2014-09-16] 54841: resque-1.25.2: Processing default since 1410855841  [ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper]
** [01:24:01 2014-09-16] 54841: Running before_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])]
** [01:24:01 2014-09-16] 54841: resque-1.25.2: Forked 54882 at 1410855841
** [01:24:01 2014-09-16] 54882: Running after_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])]
Hello World!!
** [01:24:01 2014-09-16] 54882: done: (Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])

但是当我尝试安排一份工作时,看起来好像已经入队,但他们没有做任何事情。

这是schedule.yml

friends:
  every: "30s"
  queue: "friends"
  class: "FriendsJob"
  args: 8
  description: "Friends jobs scheduler"

以下是预定作业的输出。

** [01:23:36 2014-09-16] 54841: got: (Job{friends} | FriendsJob | [8])
** [01:23:36 2014-09-16] 54841: resque-1.25.2: Processing friends since 1410855816 [FriendsJob]
** [01:23:36 2014-09-16] 54841: Running before_fork hooks with [(Job{friends} | FriendsJob | [8])]
** [01:23:36 2014-09-16] 54841: resque-1.25.2: Forked 54880 at 1410855816
** [01:23:36 2014-09-16] 54880: Running after_fork hooks with [(Job{friends} | FriendsJob | [8])]
** [01:23:36 2014-09-16] 54880: done: (Job{friends} | FriendsJob | [8])

阅读此http://dev.mikamai.com/post/96343027199/rails-4-2-new-gems-active-job-and-global-id后 我怀疑它与ActiveJob和GlobalId有关。

看一下排队的差异

** [01:24:01 2014-09-16] 54841: Running before_fork hooks with [(Job{default} | ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper | ["TestJob", "98732ce5-17f7-4da3-9a03-a5d2f8f74e84", "8"])]

vs预定

** [01:23:36 2014-09-16] 54841: Running before_fork hooks with [(Job{friends} | FriendsJob | [8])]

作业本身是通过

生成的
 rails g job <JobName>

他们看起来像这样:

class TestJob < ActiveJob::Base
  queue_as :default
  def perform(friend_id)
    friend = Friend.find(friend_id)
    name = friend.name.swapcase
    puts "Hello World!!"
  end
end


class FriendsJob < ActiveJob::Base
  queue_as :friends
  def perform(friend_id)
    friend = Friend.find(friend_id)
    name = friend.name.swapcase
    puts "Hello World!!"
  end
end

对此的任何帮助将不胜感激,并提前感谢。

********* UPDATE *********

我已删除了action_job铁路,我只使用Resque和Resque-Scheduler,而且预定的工作现在正在运行。所以这似乎与ActionJob / GlobalId有关。我在rails项目中打开了一个问题。希望他们能尽快解决。

******第二次更新********* 我得到了这方面的最新消息。在ActiveJob代码库工作的Cristianbica说。 &#34;到目前为止,我们还没有想到重复工作,我们不支持这一点,因为没有外部宝石,没有适配器支持这一点。然而,这是一个非常好的功能,但我认为我们无法及时制作4.2。此外,我不确定它是否适合包含在rails&#34;

3 个答案:

答案 0 :(得分:4)

https://github.com/JustinAiken/active_scheduler是一个宝石,将其包装到另一个

答案 1 :(得分:2)

更新:4/4/16 - 虽然以下答案对于当前版本的Rails仍然是正确的,但我现在使用Justin创建的active_scheduler gem,如上面的答案中所述:https://stackoverflow.com/a/29155372/1299792

原始答案: 如果您需要安排定期工作,请避免使用ActiveJob。

来自issue that Luis raised

  

由于我们目前不支持ActiveJob的重复性工作,我们将关闭它。如果可以支持重复工作,我将其视为单独的gem或者在rails 5中。围绕它们的功能请求和谈话通常在邮件列表(https://groups.google.com/forum/#!forum/rubyonrails-core)中进行讨论。   作为您的问题@luismadrigal的解决方案,我建议您使用resque-scheduler方法来重复工作。

https://github.com/rails/rails/issues/16933#issuecomment-58945932

有关于创建宝石in the mailing list的讨论,但我找不到任何进一步的信息。

答案 2 :(得分:1)

似乎resque-scheduler中的Rails 4.2中的ActiveJob 不支持。因此,作业未正确调度,这解释了使用ActiveJob API和resque-scheduler将作业排入队列时的日志差异。

要解决这个问题,我们应该找到一种在ActiveJob包装器中安排作业的方法:

ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper

Resque-scheduler提供了支持默认情况下不支持的扩展的方法。为此,我们应该扩展自定义作业类以支持#scheduled方法。这样我们就可以使用ActiveJob API手动排队作业。

最简单的方法是在基础作业中编写通用代码方法,然后从中扩展所有作业:

# custom base job
class Job < ActiveJob::Base

    # method called by resque-scheduler to enqueue job in a queue
    def self.scheduled(queue, klass, *args)

        # create the job instance and pass the arguments
        job = self.job_or_instantiate(*args)

        # set correct queue
        job.queue_name = queue

        # enqueue job using ActiveJob API
        job.enqueue
    end
end

Reque-scheduler将调用此方法来安排从Job类扩展的每个作业。这样,作业将在ActiveJob包装器中排队。结果与调用MyJob.perform_later(*args)相同。