在 Rails 安排 ActiveJob
Schedule an ActiveJob in Rails
我正在做一个天气 API,它将从另一个 API 获取、处理和保存数据。为了获得每日更新(请求 URL 信息,获取 JSON/XML 数据,构建我的数据并将其保存到我的数据库)我认为最合适的方法是使用 ActiveJob。
我想定期将作业安排到 运行。我想要类似 UNIX cron 或 Spring @Scheduled 注释的东西 Java.
我在 Stack Overflow() about scheduling jobs but they use external gems like whenever. I have been looking for a backend that allows to execute the job in the Rails API (Backends) 上看到了另一个问题,但似乎 none 可用的允许安排作业。
Rails 框架(第 5 版)是否有任何功能可以让我做我想做的事?或者我必须使用外部 gem?
非常感谢。
编辑
如果对任何人有用,这里是工作的架构:
class ImportDailyDataJob < ApplicationJob
queue_as :default
def perform(*args)
# Do something later
end
private
def prepare_request
# return the request object
end
def request_data
# Get the data from external API.
end
def process_data
# Process the data
end
def save_processed_data
# Saves the data to the database
end
end
我认为你应该去 sidekiq 使用 redis 写一个任务来安排工作,为此只需安装这两个 gem 并可以获取他们的文档:
gem 'redis-rails'
然后安装sidekiq
gem 'sidekiq'
大多数主要的 AJ 适配器都具有一些用于安排作业的功能,但有些将该功能分成单独的 gems。例如,对于 Resque,有一个单独的 resque-scheduler gem 与香草 Resque gem 一起工作。
和here's关于重复性工作的一点。在一些基本设置之后,您创建一个 schedule.yml 文件,定义 how/when 您希望您的工作 运行,例如
# config/initializers/resque_scheduler.rb
Resque.schedule = YAML.load_file('../calendar.yml')
# config/calendar.yml
your_recurring_job:
cron: "0 0 * * *" # Every day at midnight
class: "YourJobClass"
queue: your_queue
args:
description: "This job does a thing, daily"
我不知道每个适配器,但主要的适配器通常有类似的姊妹 gem 提供此功能。
这不是真正的调度,但如果您不需要完全准确的调度,则在某些情况下可以伪造它,即在您的情况下 运行 每天一次作业:
class WeatherJob < ApplicationJob
def perform
reschedule_job
do_work
end
def do_work
# ...
end
def reschedule_job
self.class.set(wait: 24.hours).perform_later
end
end
将来使用 ActiveJob 将作业排队到 运行 应该与 DelayedJob 或 Sidekiq 一起使用,可能并非所有活动作业后端都支持?
我正在做一个天气 API,它将从另一个 API 获取、处理和保存数据。为了获得每日更新(请求 URL 信息,获取 JSON/XML 数据,构建我的数据并将其保存到我的数据库)我认为最合适的方法是使用 ActiveJob。
我想定期将作业安排到 运行。我想要类似 UNIX cron 或 Spring @Scheduled 注释的东西 Java.
我在 Stack Overflow(
Rails 框架(第 5 版)是否有任何功能可以让我做我想做的事?或者我必须使用外部 gem?
非常感谢。
编辑 如果对任何人有用,这里是工作的架构:
class ImportDailyDataJob < ApplicationJob
queue_as :default
def perform(*args)
# Do something later
end
private
def prepare_request
# return the request object
end
def request_data
# Get the data from external API.
end
def process_data
# Process the data
end
def save_processed_data
# Saves the data to the database
end
end
我认为你应该去 sidekiq 使用 redis 写一个任务来安排工作,为此只需安装这两个 gem 并可以获取他们的文档:
gem 'redis-rails'
然后安装sidekiq
gem 'sidekiq'
大多数主要的 AJ 适配器都具有一些用于安排作业的功能,但有些将该功能分成单独的 gems。例如,对于 Resque,有一个单独的 resque-scheduler gem 与香草 Resque gem 一起工作。
和here's关于重复性工作的一点。在一些基本设置之后,您创建一个 schedule.yml 文件,定义 how/when 您希望您的工作 运行,例如
# config/initializers/resque_scheduler.rb
Resque.schedule = YAML.load_file('../calendar.yml')
# config/calendar.yml
your_recurring_job:
cron: "0 0 * * *" # Every day at midnight
class: "YourJobClass"
queue: your_queue
args:
description: "This job does a thing, daily"
我不知道每个适配器,但主要的适配器通常有类似的姊妹 gem 提供此功能。
这不是真正的调度,但如果您不需要完全准确的调度,则在某些情况下可以伪造它,即在您的情况下 运行 每天一次作业:
class WeatherJob < ApplicationJob
def perform
reschedule_job
do_work
end
def do_work
# ...
end
def reschedule_job
self.class.set(wait: 24.hours).perform_later
end
end
将来使用 ActiveJob 将作业排队到 运行 应该与 DelayedJob 或 Sidekiq 一起使用,可能并非所有活动作业后端都支持?