使用 ActiveJobs 在 Delayed_Job 中排队
Queueing in Delayed_Job with ActiveJobs
在控制器中,我有一个 post 请求,该请求来自已验证用户触发 ActiveJob。
RunReportsJob.perform_later(param1, param2, param3)
它工作没有错误。但是,如果用户再次按下按钮(触发 post 请求)以触发 RunReportsJob,而它已经 运行,则会同时发生 2 个作业。我设置 delayed_job 是因为我认为这是一种排队服务,并且一次可以将所有内容都放在一个作业中。但这并没有发生。以下是我的配置:
# app/jobs/run_reports_job.rb:
class RunReportsJob < ActiveJob::Base
queue_as :RunReports
include ShopifyApp::Controller
# --------------------------------------------------------------
# JOB ERROR MANAGEMENT
# --------------------------------------------------------------
rescue_from(ActiveRecord::RecordNotFound) do |exception|
# Do something with the exception
logger.debug "Hit rescue_from"
end
# --------------------------------------------------------------
# JOB FUNCTION
# --------------------------------------------------------------
def perform(param1, param2, param3)
# some code is performed here
end
end
# config/application.rb
:
config.active_job.queue_adapter = :delayed_job
# config/initializers/delayed_job_config.rb
:
Delayed::Worker.max_attempts = 1
Delayed::Worker.max_run_time = 12.hours
您必须在 RunReportsJob.perform_later(param1, param2, param3)
之前添加一些逻辑来检查已经排队的作业优先级并增加优先级编号(根据文档,编号越小优先级越高)在此示例中队列:
highest_priority = Delayed::Job.where(queue: :RunReports).maximum(:priority)
Delayed::Worker.default_priority = highest_priority + 1 if highest_priority
RunReportsJob.perform_later(param1, param2, param3)
DelayedJob
使用 table 保存所有工作信息,请查看 https://github.com/collectiveidea/delayed_job#gory-details
上的文档
在控制器中,我有一个 post 请求,该请求来自已验证用户触发 ActiveJob。
RunReportsJob.perform_later(param1, param2, param3)
它工作没有错误。但是,如果用户再次按下按钮(触发 post 请求)以触发 RunReportsJob,而它已经 运行,则会同时发生 2 个作业。我设置 delayed_job 是因为我认为这是一种排队服务,并且一次可以将所有内容都放在一个作业中。但这并没有发生。以下是我的配置:
# app/jobs/run_reports_job.rb:
class RunReportsJob < ActiveJob::Base
queue_as :RunReports
include ShopifyApp::Controller
# --------------------------------------------------------------
# JOB ERROR MANAGEMENT
# --------------------------------------------------------------
rescue_from(ActiveRecord::RecordNotFound) do |exception|
# Do something with the exception
logger.debug "Hit rescue_from"
end
# --------------------------------------------------------------
# JOB FUNCTION
# --------------------------------------------------------------
def perform(param1, param2, param3)
# some code is performed here
end
end
# config/application.rb
:
config.active_job.queue_adapter = :delayed_job
# config/initializers/delayed_job_config.rb
:
Delayed::Worker.max_attempts = 1
Delayed::Worker.max_run_time = 12.hours
您必须在 RunReportsJob.perform_later(param1, param2, param3)
之前添加一些逻辑来检查已经排队的作业优先级并增加优先级编号(根据文档,编号越小优先级越高)在此示例中队列:
highest_priority = Delayed::Job.where(queue: :RunReports).maximum(:priority)
Delayed::Worker.default_priority = highest_priority + 1 if highest_priority
RunReportsJob.perform_later(param1, param2, param3)
DelayedJob
使用 table 保存所有工作信息,请查看 https://github.com/collectiveidea/delayed_job#gory-details