简体   繁体   中英

In Rails, how can I have a list of commands automatically run say every 24 hours?

I have a backend Rails JSON API.

Every X amount of hours, there is some data I would like to purge.

So I need to be able to run some commands every X hours.

How can I accomplish this in Rails?

Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs.

It is an easy way to write cron jobs to a cron tab file which is later on executed by the system. It provides you a nice DSL to schedule your tasks.

Let's assume you have a test action in YourModel that you want to run every 24 hours, so you will be doing this:

your_model.rb

def test
    # Do Something...
end

schedule.rb

every 24.hours do
  runner 'YourModel.test'
end

You need to run the following command on your project to do create the crontab:

whenever -i

您可以随时使用此gem( https://github.com/javan/whenever ),这将帮助您使用crontab管理调度。

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