作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
试图了解 Rails 中的 delay_job,我想更新我画廊中已经过期的所有 PIN
class UpdatePinJob < ApplicationJob
queue_as :default
def perform(gallery)
gallery.where('DATE(expired_pin) > ?', Date.today).update_all('pin = ?', 'new_pin_here')
end
end
最佳答案
你走在正确的 rails 上。我建议按照此处的说明进行操作:ActiveJobsBasics
要在您的 Controller 中调用它,您应该这样做:
# Enqueue a job to be performed as soon as the queuing system is free.
UpdatePinJob.perform_later(gallery)
# Enqueue a job to be performed 1 week from now.
UpdatePinJob.set(wait: 1.week).perform_later(gallery)
For enqueuing and executing jobs in production you need to set up a queuing backend, that is to say you need to decide for a 3rd-party queuing library that Rails should use. Rails itself only provides an in-process queuing system, which only keeps the jobs in RAM. If the process crashes or the machine is reset, then all outstanding jobs are lost with the default async backend. This may be fine for smaller apps or non-critical jobs, but most production apps will need to pick a persistent backend.
# config/application.rb
module YourApp
class Application < Rails::Application
...
config.active_job.queue_adapter = :sidekiq
...
end
end
关于ruby-on-rails - 任何示例如何在 rails 中使用延迟作业进行大规模更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61775462/
我是一名优秀的程序员,十分优秀!