ruby-on-rails - 在 ruby 中安排任务在每月的 15 日和最后一天工作
问题描述
我在 schedule.rb 文件中定义了一个 rake 任务,在每月的 15 号和最后一天早上 8 点工作。我只是想确认我是否以正确的方式做到了。请看一下并提出建议。
每月 15 日早上 8 点运行此任务
every '0 8 15 * *' do
rake 'office:reminder', environment: ENV['RAILS_ENV']
end
在每个月的最后一天早上 8 点运行此任务
every '0 8 28-31 * *' do
rake 'office:reminder', environment: ENV['RAILS_ENV']
end
解决方案
由于cron
有一个非常简单的界面,如果没有外部帮助,很难向它传达“一个月的最后一天”这样的概念。但是您可以将逻辑转移到任务中:
every '0 8 28-31 * *' do
rake 'office:end_of_month_reminder', environment: ENV['RAILS_ENV']
end
在一个名为 office:end_of_month_reminder 的新任务中:
if Date.today.day == Date.today.end_of_month.day
#your task here
else
puts "not the end of the month, skipping"
end
你仍然有你的第一个月的任务。但是,如果您想将其合并为一个:
every '0 8 15,28-31 * *' do
rake 'office:reminder', environment: ENV['RAILS_ENV']
end
在你的任务中:
if (Date.today.day == 15) || (Date.today.day == Date.today.end_of_month.day)
#your task here
else
puts "not the first or last of the month, skipping"
end
推荐阅读
- python-3.x - TensorFlow 无效形状(InvalidArgumentError)
- powershell - 使用 PowerShell 调用 cmd.exe,异步生成多个实例。不要等待 cmd.exe 关闭
- discord.js - 如何修复“提供的参数既不是用户也不是角色”。
- javascript - 如何在 A-Frame 中离线使用库存字体?
- python - 如何用列表中的单词制作二进制熊猫矩阵?
- amazon-web-services - 具有其他实例类型的 Auto Scaling 组无法在 AWS 上运行
- spring-boot - Spring Cloud Stream Kafka 无法创建生产者绑定
- python - 查找包含集合中所有值的最短连续子数组的算法
- angular - 属性更改时重新呈现视图后的生命周期挂钩
- python - Python PuLP 性能问题 - 花费太多时间来解决