简体   繁体   中英

How to update a cron based on a datetime in the db

Ok so i rails application with a mysql database which has a field named end_date in the orders table. This field is managed by the admin. I need a cronjob that runs 2 weeks before the end_date field. Any ideas on how to do this. I am also using the whenever gem

every 1.day, :at => '12:30 am' do 
  runner "Order.task_that_checks_date"
end

My first thought was run a cron job daily and check that value but i hate running a cron everyday if i only need to have the cron run once a year. Any ideas on how to improve this

The smoothest solution to that problem that I can think of would be to have a "next_cron" date somewhere in the app, and to update that on an after_save hook. The logic would be to find the soonest end_date in the app, and set the "next_cron" date to two weeks before that. The cron either doesn't run at all until that date, or every so often checks to see if today is next_cron day and either do its thing or exit.

That having been said, I think it's actually more performant to have your cron run daily, and just check to see whether there are any actions it needs to take. It depends on how many tasks you're expecting to put into the system, but as a general rule I'd rather have a background process take thirty seconds at three in the morning than have a user-facing operation take 0.05 extra seconds during peak traffic time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM