Whenever: How to test in development environment?

Created on 10 Jun 2013  ·  5Comments  ·  Source: javan/whenever

I have a cron on my app and I'm trying to test it, but it's not running. My config:

# Gemfile
gem 'whenever', require: false

My schedule

# config/schedule.rb
set :output, { error: 'error.log', standard: 'cron.log' }

every 2.minutes do
  runner "Cron.get_categories"
end

And my cron

# lib/cron.rb
class Cron
  def self.get_categories
    p "Testing..."
  end
end

And the text is not printing on my console. Is there any other configuration to do?

Thanks in advance!

Most helpful comment

To update your local crontab in development mode, you'll need to run the whenever command manually, instead of relying on the included Capistrano tasks to do it for you. whenever --update-crontab --set environment=development should do the trick.

From there, you can run crontab -l to ensure that your crontab file was written. Your job should be running every 2 minutes now. Note, you won't ever see any text in your console; the runner occurs in a new process

All 5 comments

To update your local crontab in development mode, you'll need to run the whenever command manually, instead of relying on the included Capistrano tasks to do it for you. whenever --update-crontab --set environment=development should do the trick.

From there, you can run crontab -l to ensure that your crontab file was written. Your job should be running every 2 minutes now. Note, you won't ever see any text in your console; the runner occurs in a new process

Thanks @javan! However, this didn't work for me. I ran:

$ whenever --update-crontab --set environment=development
# => [write] crontab file updated

$ crontab -l
# => # Begin Whenever generated tasks for: config/schedule.rb
# => 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd /Users/app && script/rails runner -e production '\''Cron.get_categories'\'' >> log/cron.log 2>> log/error.log'

# => # End Whenever generated tasks for: config/schedule.rb

# => # Begin Whenever generated tasks for: /Users/app/config/schedule.rb
# => * * * * * /bin/bash -l -c 'cd /Users/app && script/rails runner -e development '\''Cron.get_categories'\'' >> log/cron.log 2>> log/error.log'

# => # End Whenever generated tasks for: /Users/app/config/schedule.rb

And nothing happened. :confused: I'm using a Mac OSX (10.8.3 version), Rails 3.2.13 and Ruby 2.0.0.

Your answer left me a doubt... This means that to use whenever in production environment I need to install capistrano gem, right?

Another question... If I create a new cron or just update the time that each cron will run, I need execute this command again?

Thank you so much @javan!

Check your log/cron.log file, I suspect it's being written to every 2 minutes.

If you're using Capistrano to deploy your application, Whenever has built-in tasks to write your crontab. They're not required though, it can always be used as a command line tool.

This is the error:

# log/error.log
/Library/Ruby/Site/1.8/rubygems/core_ext/kernel_require.rb:45:in `gem_original_require': no such file to load -- bundler/setup (LoadError)
    from /Library/Ruby/Site/1.8/rubygems/core_ext/kernel_require.rb:45:in `require'
    from /Users/app/config/boot.rb:6
    from script/rails:5:in `require'
    from script/rails:5

And this on my boot.rb

# config/boot.rb at line 6
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])

@fabianoalmeida in your local crontab, instead of

* * * * * /bin/bash -l -c 'cd /Users/app && script/rails runner -e development '\''Cron.get_categories'\'' >> log/cron.log 2>> log/error.log'

run the command with bundle exec:

* * * * * /bin/bash -l -c 'cd /Users/app && /your-full-path-to/bundle exec script/rails runner -e development '\''Cron.get_categories'\'' >> log/cron.log 2>> log/error.log'

... to find out your full path to bundle, just run which unix command like the following:

$ which bundle
/home/lsoave/.rbenv/shims/bundle

Was this page helpful?
0 / 5 - 0 ratings