-1

Scenario: a gem called 'whenever' translates ruby code to crontab commands and you write that code in a file called schedule.rb which once run makes the translation to the crontab file.

I have a snippet in my schedule.rb which would execute the method located in an Active Record model, every 15 minutes and then would write me a log in a tasks file saying "done" but nothing happens ever if I let the whenever do something.

The model, controller and view work fine when I manually run them on the browser, but if I leave it up to the automated mode, it won't do anything.

So, for the schedule.rb

set :output, "#{path}/tareas.log"

every 15.minutes do

 rake "article.fetcharticles"
 command "echo'done'"

end

which translated into the crontab file as:

0,15,30,45 * * * * /bin/bash -l -c 'cd/root/aggregrator && RAILS_ENV=development bundle exec rake article.fetcharticles --silent >> /root/aggregrator/tareas.log

The fetcharticles method is nothing but a def within the model (article) with a curl like command which works fine, as I said, when I run it manually from firefox.

Is there anything left I should have done?

So, what other posts said was that 'whenever' did not work on development env, but in production. Well, that post was from 2011 so I hope that 'whenever' has fixed that by now, because if I try to change to server .-e production I get errors about secret key configurations etc and it won't start, so I am still on development mode.

and then another one wrote:

Are you using RVM? If so, Whenever generates a crontab entry like this (yes exactly like mine)

and his solution was:

To make this work, you need to add an appropriate .rvmrc file to your project's root folder.

but, how do I do that?

his post was here but did not get an approved answer.

Whenever cron job is not working in rails 3

UPDATE:

The problem very much seems to point in the direction of not having the needed files, maybe has something to do with this rmv issue reported, but seeing the logs of errors in my tareas.log file I see, (show a snippet)

/usr/local/rvm/gems/ruby-2.2.0@global/gems/bundler-1.9.1/lib/bundler/spec_set.rb:92:in `block in materialize': Could not find i18n-0.7.0 in any of the sources (Bundler::GemNotFound)
   from /usr/local/rvm/gems/ruby-2.2.0@global/gems/bundler-1.9.1/lib/bundler/spec_set.rb:85:in `map!'
   from /usr/local/rvm/gems/ruby-2.2.0@global/gems/bundler-1.9.1/lib/bundler/spec_set.rb:85:in `materialize'
   from /usr/local/rvm/gems/ruby-2.2.0@global/gems/bundler-1.9.1/lib/bundler/definition.rb:132:in `specs'
   from /usr/local/rvm/gems/ruby-2.2.0@global/gems/bundler-1.9.1/lib/bundler/definition.rb:177:in `specs_for'
   from /usr/local/rvm/gems/ruby-2.2.0@global/gems/bundler-1.9.1/lib/bundler/definition.rb:166:in `requested_specs'
   from /usr/local/rvm/gems/ruby-2.2.0@global/gems/bundler-1.9.1/lib/bundler/environment.rb:18:in `requested_specs'
   from /usr/local/rvm/gems/ruby-2.2.0@global/gems/bundler-1.9.1/lib/bundler/runtime.rb:13:in `setup'
   from /usr/local/rvm/gems/ruby-2.2.0@global/gems/bundler-1.9.1/lib/bundler.rb:122:in `setup'
   from /usr/local/rvm/gems/ruby-2.2.0@global/gems/bundler-1.9.1/lib/bundler/setup.rb:18:in `<top (required)>'
   from /usr/local/rvm/rubies/ruby-2.2.0/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
   from /usr/local/rvm/rubies/ruby-2.2.0/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
   from /root/aggregrator/config/boot.rb:3:in `<top (required)>'
   from bin/rails:7:in `require_relative'

None of the solutions provided addressed the core of the problem, as the problem is in missing files exactly as I pointed out when I wrote the initial post

Community
  • 1
  • 1
Carlos
  • 1
  • 1
  • Can you try putting a space between `echo` and `'done'`? I tried `echo'done'` in my terminal and it gave me `bash: echodone: command not found`. However `echo 'done'` works fine. – Alex Pan May 05 '15 at 14:31
  • I'm just using terminal. Try running the command in double quotes in terminal and you'll get that error. However, with the space, it works fine. – Alex Pan May 05 '15 at 14:57

2 Answers2

1

Did you make a rake task called article.fetcharticles? If not, then you are using whenever the wrong way. You're specifying a rake task, when you should be specifying a method to run, like so:

runner "Article.fetcharticles"

instead of your

rake "article.fetcharticles"
Dan Herman
  • 1,395
  • 1
  • 15
  • 26
  • I have changed it to the runner version as indicated and also capitalized the A article. so far no changes, and the 'done' word is not written to the log file either. – Carlos May 05 '15 at 14:55
1

You have to actually install the crontab yourself if you want it to work locally. The comments that whenever doesn't work in development are not problems with whenever; whenever isn't intended to work in your development environment. The idea is that it's supposed to be for setting up cron entries on your server which would typically be a staging or production server. This would normally be done as part of your deploy process; typically using capistrano.

So, to get it working locally you'd have to install this cron configuration on your local machine. You can probably do that by generating the file and copying it to /etc/cron.d/. However, bear in mind that it's usually better to run tasks manually in your development environment.

Lastly if you're trying to debug why a cron entry may be failing it's worth trying to run the command manually anyway. For example does:

/bin/bash -l -c 'cd/root/aggregrator && RAILS_ENV=development bundle exec rake article.fetcharticles --silent >> /root/aggregrator/tareas.log

actually run successfully. I think you'll get an error straight away from cd/roo/aggregator generating no such file or directory.

Shadwell
  • 34,314
  • 14
  • 94
  • 99