我已经定义了一个rake任务,但是我不确定如何使它在每个月的第一个和第三个星期一在Ruby中运行。请帮助我。
schedule.rb
#run this task on every 1st and 3rd monday of the month
rake 'office:reminder', environment: ENV['RAILS_ENV']
office.rake
namespace :office do
desc "reminder emails"
task reminder: :environment do
PaymentReminderWorker.perform_async(arg1, arg2)
end
end
请帮我弄清楚。
忘了第一个星期一,我在想这样的事情。
require 'date'
Date.today.mday <= 7
更新的答案(这是一个好方法吗?)
namespace :office do
desc "reminder emails"
task reminder: :environment do
today_date = DateTime.now
first_monday = Chronic.parse("1st monday of this month", :now => today_date.to_date.beginning_of_month)
third_monday = Chronic.parse("3rd monday of this month", :now => today_date.to_date.beginning_of_month)
if today_date == first_monday || today_date == third_monday
PaymentReminderWorker.perform_async(arg1, arg2)
end
end
end
答案 0 :(得分:0)
您可以执行以下操作:
const cron = new CronJob('* */30 * * *', () => {
// do stuff w/ mysql
});
cron.start();
或者只使用不带日期部分的常规任务,在cron中将是:
namespace :office do
desc "reminder emails"
task reminder: :environment do
if (Date.today.monday?) & ((Date.today.mday.in? (1..7)) || (Date.today.mday.in? (15..22)))
PaymentReminderWorker.perform_async(arg1, arg2)
end
end
end