【问题标题】:Running a cron job at different times for each job为每个作业在不同时间运行一个 cron 作业
【发布时间】:2016-09-22 11:12:15
【问题描述】:

我正在 Rails 中构建一个应用程序,允许用户在一段时间内发布产品进行销售。每个产品都有一个不同的“end_time”,我想在其中运行一个方法,将产品状态更新为“dead”。
我一直在阅读有关 Eachize gem,但据我所知,我会用它来执行每分钟、每小时、每天等重复性任务...... 关于我将如何运行将在每个产品的特定“结束时间”运行的 cron 作业的任何想法?

例如:

live_products = Product.where('status = ?', 'live')

live_products.each do |product|
 at: product.end_time do
   product.status = 'dead'
   product.save
 end
end 

【问题讨论】:

标签: ruby-on-rails ruby postgresql whenever


【解决方案1】:

Cronjob,每分钟都在检查那些需要终止事务的人。

Product.turn_dead.update_all status: :dead 



#product.rb
scope :live, -> {where status: :live}
scope :out_of_time, -> {where "end_time <= ?", Time.now}
scope :turn_dead, ->{ live.out_of_time }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-11
    • 2011-09-27
    • 2021-10-23
    • 1970-01-01
    • 2016-09-29
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多