every 3.hours do runner "MyModel.some_process" rake "my:rake:task" command "/usr/bin/my_great_command" end
whenever可以由三种方式执行任务
1.runner 后面直接加要执行的函数即可
2.rake 后面加编写的rake脚本即可
Rake脚本的编写在lib/tasks目录下
例:
config/schedule.rb中
every 6.hours do
rake "order_process:cancel_order_24_hours"
end
lib/tasks/order_process.rake中
namespace :order_process do
task :cancel_order_24_hours => :environment do
states = %w(accepted pre_approved)
Order.where(state: states).find_each do |order|
tracking = order.trackings.find_by(field: "state", value: order.state)
if tracking.present? and tracking.created_at <= 24.hours.ago
order.cancel_order!(User.first, "pay expired")
else
if order.updated_at <= 24.hours.ago
order.cancel_order!(User.first, "pay expired")
end
end
end
end
end
3.command直接执行文件