Rails Periodic Task

rails periodic task

Generally speaking, there's no built in way that I know of to create a periodic task within the application. Rails is built on Rack and it expects to receive http requests, do something, and then return. So you just have to manage the periodic task externally yourself.

I think given the frequency that you need to run the task, a decent solution could be just to write yourself a simple rake task that loops forever, and to kick it off at the same time that you start your application, using something like Foreman. Foreman is often used like this to manage starting up/shutting down background workers along with their apps. On production, you may want to use something else to manage the processes, like Monit.

A cron job for rails: best practices?

I'm using the rake approach (as supported by heroku)

With a file called lib/tasks/cron.rake ..

task :cron => :environment do
puts "Pulling new requests..."
EdiListener.process_new_messages
puts "done."
end

To execute from the command line, this is just "rake cron". This command can then be put on the operating system cron/task scheduler as desired.

Update this is quite an old question and answer! Some new info:

  • the heroku cron service I referenced has since been replaced by Heroku Scheduler
  • for frequent tasks (esp. where you want to avoid the Rails environment startup cost) my preferred approach is to use system cron to call a script that will either (a) poke a secure/private webhook API to invoke the required task in the background or (b) directly enqueue a task on your queuing system of choice

How to schedule whenever task based on a table column?

Step 1:

First we have to load all the files in the schedule.rb Then only we can execute the ActiveRecord queries.

That could be done by the below command

 require File.expand_path(File.dirname(__FILE__) + "/environment")

Step 2:

Execute the ActiveRecord query and get the time.

  run_at=Scheduler.last.run_at
time=[run_at.hour.to_s,run_at.min.to_s].join(":")
every 1.day, :at => time do
rake "notifications:run_mailer"
end

step 3:

We have to update the whenever crontab using below command.

 system "whenever --update-crontab"

system keyword is used to run linux terminal commands inside rails code.

Handling recurring tasks in Rails

Yes, there is a better way to do recurring tasks using rake tasks and whenever gem. It has an extremely easy to used DSL

All you need to do is define your rake task and then put the schedule configuration inside a schedule.rb.

However, whenever utilizes cron jobs and is not supported by Heroku. If you are using Heroku, then you should use Heroku Scheduler. All you need to do is define your tasks in a tasks/scheduler.rake, install the addon and let Heroku Scheduler do the rest.

This approaches will help keep your models cleaner and remove the scheduling information from them.

To the second part of your question that is marking recurring tasks as complete, all you need to do is set a boolean attribute say completed to true when the recurring task is marked as complete and then adding a guard clause in your rake tasks like return if task.completed? to skip processing for that task.

Best practice to implement scheduled task execution with Rails?

Don't do that.

It's a bad design to schedule many jobs for a few days or weeks later, especially when they perform a critical task.

Why?

Mainly because the scheduled job's date is not stored in your application database but another one, most of the time in Redis. In the majority of cases, it is not included in the backup. So the day you need to restore a backup, you'll lose all pending scheduled job. All the accounts that were pending for the last subscription period to end may remain open.

The safest solution is to rely on a database field holding the actual end of the subscription date. Let's name it subscription_ends_on. If your user unsubscribes on April 12th, you set subscription_ends_on with the value "March 1rst".

Then you create a background job that runs every day at midnight and unsubscribe all accounts where subscription_ends_on is not null and is less than or equal to today.

Monitoring a periodic task in Rails

The easy solution is to create a rake task in your application that finds the fax tasks you need to check and then updates them. Stick it in a cron job and your good to do. Check out whenever for configuring the cron jobs for you.

I would like to suggest a nice job scheduling system but I have been looking for on to use in Rails for several years.



Related Topics



Leave a reply



Submit