簡體   English   中英

如何在 schedule.rb 文件中傳遞動態值(Ruby on rails)

[英]How to pass dynamic value in schedule.rb file ( Ruby on rails )

如何在正則表達式中傳遞動態值

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

為了避免這種事情,我通常會檢查任務上的日期。 例如,我有一個任務必須在每個月的最后一天運行。 例如,我不能在當天使用3031 ,因為 2 月有 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 更好,因為現在您要做的是在調度程序文件中添加一些邏輯,這可能會在以后咬您

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM