如果 rask 任务在 ruby 中的星期日,则将其重新安排到第二天

Reschedule rask task to the next day if it falls on sunday in ruby

我在每个月的 2 号和 17 号定义了 运行 的佣金任务。但是我不希望这个任务 运行 如果星期天是 2 号和 17 号。在这种情况下,任务应该在第二天 运行。有人知道如何实现吗?

#run this task at 8am on 17th of every month
every '0 8 17 * *' do
  rake 'money:reminder', environment: ENV['RAILS_ENV']
end


#run this task at 8am on the 2nd day of every month
every '0 8 2 * *' do
  rake 'money:reminder', environment: ENV['RAILS_ENV']
end

您可以将任务包装在 ActiveJob 中,稍后将其排队到 运行 :

# Enqueue a job to be performed tomorrow at noon.
GuestsCleanupJob.set(wait_until: Date.tomorrow.noon).perform_later(guest)

(来源:railsguide

在你的情况下,在工作日有一个条件:

# your_class.rb
def self.reminder
  if Time.current.sunday?
    YourJob.set(wait_until: Date.tomorrow.noon).perform_later
  else
    YourJob.perform_later
  end
end

# schedule.rb
#run this task at 8am on 17th of every month
every '0 8 17 * *' do
  bundle exec rails runner 'YourModel.reminder'
end


#run this task at 8am on the 2nd day of every month
every '0 8 2 * *' do
  bundle exec rails runner 'YourModel.reminder'
end

edit : 看了你的评论后,你的工人好像是 Sidekiq 工人。如果是这样,您可以使用:

HardWorker.perform_in(5.minutes, 'bob', 5)
HardWorker.perform_at(5.minutes.from_now, 'bob', 5)

(来源:getting started