【问题标题】:How to pass dynamic value in schedule.rb file ( Ruby on rails )如何在 schedule.rb 文件中传递动态值(Ruby on rails)
【发布时间】:2026-01-13 08:15:02
【问题描述】:

如何在正则表达式中传递动态值

new_date_value = query_value
# minute(0-59), hours(0-23), day of month(1-31), month of year(1-12), day of week(0-6) 0=sunday

every '0 4 #{new_date_value} * *' do
   rake "db:get_free_tag_latest_post_batch", :environment => "production"
end

【问题讨论】:

    标签: ruby-on-rails ruby cron whenever


    【解决方案1】:

    为了避免这种事情,我通常会检查任务上的日期。例如,我有一个任务必须在每个月的最后一天运行。我不能在当天使用3031,因为例如,二月有 28 天。

    这就是我设置任务的方式

    # Apply interest accrued for all loans
    # every day at: '22:00'
    trigger_interest_accrued_application:
      cron: "0 22 * * *"
      class: "Jobs::TriggerInterestAccruedApplication"
      queue: admin
    

    关于任务定义

    class TriggerInterestAccruedApplication < ::ApplicationJob
      queue_as :admin
    
      def perform
        return unless Date.current.end_of_month.today?
        
        perform_task
      end
    end
    

    你认为这样的事情对你有用吗? IMO 更好,因为现在您要做的是在调度程序文件中添加一些逻辑,这可能会在以后咬您

    【讨论】:

    • 谢谢你的回答我会试试的