rails periodic task

rails scheduler
rails whenever
rails 5 whenever
rails whenever example
rails job scheduler
activejob scheduler
schedule rake task rails
rails every

I have a ruby on rails app in which I'm trying to find a way to run some code every few seconds.

I've found lots of info and ideas using cron, or cron-like implementations, but these are only accurate down to the minute, and/or require external tools. I want to kick the task off every 15 seconds or so, and I want it to be entirely self contained within the application (if the app stops, the tasks stop, and no external setup).

This is being used for background generation of cache data. Every few seconds, the task will assemble some data, and then store it in a cache which gets used by all the client requests. The task is pretty slow, so it needs to run in the background and not block client requests.

I'm fairly new to ruby, but have a strong perl background, and the way I'd solve this there would be to create an interval timer & handler which forks, runs the code, and then exits when done. It might be even nicer to just simulate a client request and have the rails controller fork itself. This way I could kick off the task by hitting the URI for it (though since the task will be running every few seconds, I doubt I'll ever need to, but might have future use). Though it would be trivial to just have the controller call whatever method is being called by the periodic task scheduler (once I have one).


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.

Schedule Tasks Using 'Whenever' Gem- Ruby on Rails, People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates,  Whether you use ruby for system management or rails for web development, you may encounter periodic tasks, which are triggered continuously according to a certain period of time (1 hour, 2 days…).


I'd suggest the whenever gem https://github.com/javan/whenever

It allows you to specify a schedule like:

every 15.minutes do
  MyClass.do_stuff
end

There's no scheduling cron jobs or monkeying with external services.

Periodic Tasks with sidekiq-cron, Using a third-party add-on, sidekiq-cron, we can create periodic tasks to extend the functionality of sidekiq. rails background processing 4:59. Then Active Elastic Job is the right gem. It provides an adapter for Rails' Active Job framework that allows your application to queue jobs as messages in an Amazon SQS queue. Elastic Beanstalk provides worker environments that automatically pull messages from the queue and transforms them into HTTP requests.


You can either write you own method, something like

class MyWorker
  def self.work
    #do you work
    sleep 15
  end
end

run it with rails runner MyWorker.work There will be a separate process running in the background

Or you can use something like Resque, but that's a different approach. It works like that: something adds a task to the queue, meanwhile a worker is fetching whatever job it is in the queue, and tries to finish it.

So that depends on your own need.

Cron jobs in Rails: A simple guide to ACTUALLY using the , I had the chance to use whenever to schedule a task in Rails, and I'm writing task itself (in my case, sending out batch messages periodically)  Redmine periodictask. In some projects there are tasks that need to be assigned on a schedule. After you installed the plugin you can add it as a module to a project that already exists or activate it as default module for new projects. On each project it will add a new tab named "Periodic Task" - just go there to add your tasks.


I know it is an old question. But maybe for someone this answer could be helpful. There is a gem called crono.

Crono is a time-based background job scheduler daemon (just like Cron) for Ruby on Rails.

Crono is pure Ruby. It doesn't use Unix Cron and other platform-dependent things. So you can use it on all platforms supported by Ruby. It persists job states to your database using Active Record. You have full control of jobs performing process. It's Ruby, so you can understand and modify it to fit your needs.

The awesome thing with crono is that its code is self explained. In order to do a task periodically you can just do:

Crono.perform(YourJob).every 2.days

Maybe you can also do:

Crono.perform(YourJob).every 30.seconds

Anyway you really can do a lot of things. Another example could be:

Crono.perform(TestJob).every 1.week, on: :monday, at: "15:30"

I suggest this gem instead of whenever because whenever uses Unix Cron table which not always is available.

Active Job Basics, Simple In/Out is a Rails app running on Heroku with several background jobs that need to be scheduled to run either hourly, daily, or weekly. In some projects there are tasks that need to be assigned on a schedule. Such as check the ssl registration once per year or run security checks every 3 months. This is the redmine plugin for you if you need such a thing.


Use something like delayed job, and requeue it every so often?

Dev Post: Schedule Recurring Rails Tasks to Run at Any Interval , Thread based scheduler. The most obvious way to schedule a task from within your program is to either loop it forever with a periodic sleep  Hi! I'm the maintainer, didn't see this message. I fixed this issue. It should be working ok now (version 3.0.3) Please let me know if something is not working right, directly opening an issue in gihub.


Scheduling tasks in Ruby / Rails, Recurring / Periodic / Scheduled / Cron job extension for Sidekiq Task scheduler that doesn't need to bootstrap your Rails environment every time it executes  In some projects there are tasks that need to be assigned on a schedule. Such as check the ssl registration once per year or run security checks every 3 months After you installed the plugin you can add it as a module to a project that already exists or activate it as default module for new projects.


Category: Scheduling, A time-based background job scheduler daemon (just like Cron) for Rails jobs are convenient because you can use one job in both periodic and enqueued ways. lib/tasks/test.rake namespace :crono do desc 'Update all tables' task :​hello  After you installed the plugin you can add it as a module to a project that already exists or activate it as default module for new projects. On each project it will add a new tab named "Periodic Task" - just go there to add your tasks.


plashchynski/crono: A time-based background job , Cron task is a time-based job scheduler in Unix-like computer operating systems. Cron can be used to schedule jobs to run periodically at fixed  With periodic tasks, you can also configure the worker daemon to queue messages based on a cron schedule. Each periodic task can POST to a different path. Enable periodic tasks by including a YAML file in your source code that defines the schedule and path for each task.