use*_*037 2 ruby-on-rails heroku scheduled-tasks clockwork
我试图了解如何使用发条执行自定义代码.这是lib/clock.rbHeroku在其devcenter文档中使用的示例文件.
require File.expand_path('../../config/boot', __FILE__)
require File.expand_path('../../config/environment', __FILE__)
require 'clockwork'
include Clockwork
every(4.minutes, 'Queueing interval job') { Delayed::Job.enqueue IntervalJob.new }
every(1.day, 'Queueing scheduled job', :at => '14:17') { Delayed::Job.enqueue ScheduledJob.new }
Run Code Online (Sandbox Code Playgroud)
什么是IntervalJob和ScheduledJob?这些文件应该放在哪里?我想运行自己的自定义作业,可以访问我的数据库记录.
编辑
这是我的 /lib/clock.rb
require 'clockwork'
require './config/boot'
require './config/environment'
module Clockwork
handler do |job|
puts "Running #{job}"
end
every(2.minutes, 'Filtering Streams') { Delayed::Job.enqueue FilterJob.new}
end
Run Code Online (Sandbox Code Playgroud)
这是我的 /lib/filter_job.rb
class FilterJob
def perform
@streams = Stream.all
@streams.each do |stream|
# manipulating stream properties
end
end
end
Run Code Online (Sandbox Code Playgroud)
我收到错误:
uninitialized constant Clockwork::FilterJob (NameError)
/app/lib/clock.rb:11:in `block in <module:Clockwork>'
Run Code Online (Sandbox Code Playgroud)
您需要执行以下操作:
首先安装发条宝石.
在lib文件夹中创建一个clock.rb
require 'clockwork'
require './config/boot'
require './config/environment'
module Clockwork
handler do |job|
puts "Running #{job}"
end
every(1.day, 'Creating Cycle', :at => '22:00') { Delayed::Job.enqueue CyclePlannerJob.new}
end
Run Code Online (Sandbox Code Playgroud)
在示例中,您提供的IntervalJob和ScheduledJob是延迟作业.发条在指定的时间触发它们.我正在调用CyclePlannerJob,这就是我的文件的样子.LIB/cycle_planner_job.rb
class CyclePlannerJob
def perform
CyclePlanner.all.each do |planner|
if Time.now.in_time_zone("Eastern Time (US & Canada)").to_date.send("#{planner.start_day.downcase}?")
planner.create_cycle
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
在每天晚上10点的示例中,我正在运行CyclePlanner作业,该作业运行我已设置的延迟作业.类似于Heroku的例子.为此,您需要在仪表板中设置Heroku应用程序上的时钟工作和延迟作业.你的Procfile也应该是这样的.
worker: bundle exec rake jobs:work
clock: bundle exec clockwork lib/clock.rb
Run Code Online (Sandbox Code Playgroud)
如果您有任何问题,请告诉我,如果需要,我可以详细介绍.
| 归档时间: |
|
| 查看次数: |
2516 次 |
| 最近记录: |