2

Create a cronjob in ruby on rails by using whenever gem.

set :environment, "development"
set :output, {:error => "log/cron_error_log.log" , :standard => "log/cron_log.log" }
every '* * * * * ' do
   command "puts 'you can use raw cron syntax too'"
end

Also update crontab it also add in list of crontab. But when I test it manually by

/home/haseebahmad/projects/social/config/schedule.rb

it gives error

/home/haseebahmad/projects/social/config/schedule.rb: line 19: every: command not found
/home/haseebahmad/projects/social/config/schedule.rb: line 20: puts 'you can use raw cron syntax too': command not found
/home/haseebahmad/projects/social/config/schedule.rb: line 21: end: command not found

It doesn't work properly, how can I solve it

4
  • When you are testing manually, the script doesn't know where the every function is coming from. Rails will autoload your gems and not make you to use a require statement to include to gem. If you want to test manually by running the script, you will need to include the require statement that brings the gem into scope. Commented Aug 15, 2015 at 17:22
  • Did you start the server after installing the Gem.. I hope you didn't. Commented Aug 15, 2015 at 17:24
  • Justin any other way of testing it?
    – user3963226
    Commented Aug 15, 2015 at 17:26
  • Arup already restart the server
    – user3963226
    Commented Aug 15, 2015 at 17:27

2 Answers 2

3

Although, there are a few approaches to run scheduled tasks in a Rails environment, e.g. script/runner and Rake etc., among them, rake approach is super easy and convenient and also supported by popular deployment platforms like heroku.

To define a rake task, you have a create a file in the lib/tasks directory of your Rails app: lib/tasks/my_cron.rake and define the task simply like this:

task :my_cron => :environment do
  puts "doing stuff...."
  SomeClass.do_some_stuff
  puts "done."
end

Then, to run this rake task, you can just run this rake command from the command line:

rake my_cron

You can run: rake -T command to see all the available rake tasks in your Rails application if you provide a desc to the rake task like this:

desc 'Running my cron task...'  
task :my_cron => :environment do
  puts "doing stuff...."
  SomeClass.do_some_stuff
  puts "done."
end

then, when you run this: rake -T, this rake task will show up in the rake taska list!

These rake tasks can be very easily scheduled on heroku using heroku scheduler

When rake task works pretty well, there can be situations where you may want to process a lot of jobs in background, in those situations couple of most popular ruby gems resque and sidekiq can be used, depending on your need.

2
  • Thanksss........one thing more, I see in one exampe they use namespace in task. Can you tell me what its purpose?
    – user3963226
    Commented Aug 19, 2015 at 8:26
  • 1
    namespace are basically used to group and organize several tasks. For example, if you hit rake -T in any of your Rails application's directory, you will see different rake tasks like rake db:create, rake db:migrate, rake db:drop etc. tasks. These are all db related tasks and grouped under under the same namespace db. Is that clear now? Commented Aug 19, 2015 at 10:47
1

I would recommend moving your ruby commands/scripting to a rake task.

That way you can test that the ruby command works properly via rake, i.e. ruby stuff is taken care of by rake.

Then you only need to run the rake command in the cron job. So you don't need to test cron which is a pain in the butt.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.