有人能够在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 scheduled
** [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
非常感谢你的帮助,提前表示感谢。
****** 更新 *********
我已经移除了action_job railtie,现在只使用Resque和Resque-Scheduler,预定的作业正在工作。因此,这似乎与ActionJob/GlobalId有关。我在rails项目中开了一个问题。希望他们能尽快解决。
****** 第二次更新 *********
我得到了一个更新。在ActiveJob代码库中工作的Cristianbica说过这样一句话。 “我们迄今为止还没有考虑过重复作业,而且没有支持它,因为没有任何适配器能够支持它,除非使用外部gem。然而,这是一个非常好的功能,但我认为我们无法及时将其纳入4.2版本。此外,我不确定它是否适合包含在Rails中。”