2

Hi um quite new to rails platform and um looking for a rails cron job scheduling tutorials . I ve gone through with tutorials use whenever and the other scheduling gems , but um looking for a core ruby implementation with the cron tab on rails . Thank you in advance

not 0x12
  • 19,360
  • 22
  • 67
  • 133
  • 1
    cron is cron, nothing related with rails. – xdazz Jun 18 '13 at 09:01
  • 1
    Hey you can schedule cron jobs within rails app. Use "whenever" gem. its a simple and good gem. See my answer. It may help you. I am using the same gem in my application. – Jyothu Jun 18 '13 at 09:30
  • @xdazz yah cron is cron question was asked for an implementation in ruby – not 0x12 Jan 10 '18 at 20:36

6 Answers6

3

Railscasts has a decent tutorial on using Cron.

EDIT

If you would like an example of how it was implemented from scratch, you could have a look at how the Whenever Gem is implemented here

TangoKilo
  • 1,785
  • 3
  • 25
  • 43
  • 1
    Hi I want to learn from the scratch without whenever . Thank you – not 0x12 Jun 18 '13 at 09:39
  • 1
    Ok well I suppose you could look at the source code (https://github.com/javan/whenever) for the Whenever gem, and that might give you a good starting point seeing as it's doing what you want to do from scratch... – TangoKilo Jun 18 '13 at 09:44
  • 1
    No problem, I've edited the answer to better reflect what the original question was – TangoKilo Jun 18 '13 at 09:53
2

To set cron jobs, you can depend the help of a simple gem named WHENEVER

Its very easy to implement. Go for it.

Jyothu
  • 3,104
  • 17
  • 26
1

Many people are against this approach (see this SO thread) but triggering your application's action with curl/wget from a cron job may be a quick-and-easy solution for periodic tasks.

You just have to keep a few things in mind:

  • Keep execution time of your action low (as it will block your application just like a regular web request would do)
  • Make sure that you do not allow anyone to trigger the action (by using IP restrictions, secret tokens or other security measures)

For more information on this topic, I have written an article about it.

Community
  • 1
  • 1
Michael Trojanek
  • 1,813
  • 17
  • 15
1

For minimal setup of "cron-like" tasks in "core" rails/ruby, I created https://github.com/Ebbe/arask

No need to install anything (other than the gem) or setup anything outside of rails.

Add gem 'arask' to your Gemfile, run bundle install, rails generate arask:install and rails db:migrate.

Now you can setup your tasks in the file config/initializers/arask.rb:

arask.create task: 'send:logs', cron: '0 2 * * *' # At 02:00 every day
arask.create script: 'puts "IM ALIVE!"', interval: :daily
arask.create task: 'my:awesome_task', interval: :hourly
arask.create task: 'my:other_awesome_task', interval: 2.hours

The tasks will automatically run if the server is running.

Esben Damgaard
  • 309
  • 1
  • 10
1

I'm using rufus-scheduler, It uses threads to execute functions scheduled. Very simple to configure. just 3 steps:

1 - Add gem gem 'rufus-scheduler', '~> 3.6'

2 - Create file config/initializers/scheduler.rb

3 - Programming schedule in scheduler.rb:

require 'rufus-scheduler'
s = Rufus::Scheduler.singleton
s.every '5s' do
    #do every 5 seconds exec this code
    puts "WOWWWWWWWWWWWWWWWWWWWW"
end

s.in '2d' do
  # every 2 days exec this
  puts "Now it's me"
end

For more doubts : https://github.com/jmettraux/rufus-scheduler

Darlan Dieterich
  • 2,369
  • 1
  • 27
  • 37
0

You can also use and external free service to outsource cronjobs http://guardiano.getpeople.in

DISCLAIMER: I made it

Lorenzo Sinisi
  • 450
  • 5
  • 8