0

I wrote a cronjob but it won't start working even though I updated my schedule.rb. I used javan/whenever. I am using:

ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.3.0]

When I crontab -l:

# Begin Whenever generated tasks for: company_list
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
* * * * * /bin/bash -l -c 'cd /Users/-/rails_projects/company_list && script/rails runner -e production '\''Company.update_all_likes'\'' >> /Users/-/rails_projects/company_list/log/cron.log 2>&1'
# End Whenever generated tasks for: company_list

This is my schedule.rb

set :output, "#{path}/log/cron.log"
env :PATH, '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'


every 1.minute do
  runner "Company.update_all_likes"
end

Company Model:

def update_all_likes
    $i = 0
    while $i < Company.size  do
        uri = URI("http://graph.facebook.com/" + Company.find[$i].fbid)
        data = Net::HTTP.get(uri)
        update_attribute(:likes,JSON.parse(data)['likes'])
        $i +=1
    end     
end

Error log (updated 2):

/Users/=/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.13/lib/rails/commands/runner.rb:53:in `eval': Could not find table 'companies' (ActiveRecord::StatementInvalid)
from /Users/=/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:346:in `columns'
from /Users/=/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.13/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
from /Users/=/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.13/lib/active_record/model_schema.rb:228:in `yield'
from /Users/=/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.13/lib/active_record/model_schema.rb:228:in `default'
from /Users/=/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.13/lib/active_record/model_schema.rb:228:in `columns'
from /Users/=/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.13/lib/active_record/model_schema.rb:248:in `column_names'
from /Users/=/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.13/lib/active_record/relation/calculations.rb:221:in `aggregate_column'
shoujo_sm
  • 3,173
  • 4
  • 37
  • 60

1 Answers1

1

It looks like there is an issue with your production.rb file, line 69. Any idea what that is?

Also, in order to start the cronjob written using whenever, you need to run

whenever --update-crontab uniquejobname

where uniquejobname is some identifier for the cronjob

bmac151
  • 505
  • 3
  • 8
  • I already declared that. I should start cronjob every time by using "whenever --update-crontab uniquejobname" command? – shoujo_sm Aug 03 '13 at 01:54
  • No, you only have to run it once and it sets up your cronjob file for you (note that you do have to run it again if you update the schedule.rb at all). It sounds like your cron environment is not running with the same environment as your app. Your cron says the job is for production environment, is this where you are trying to run it? – bmac151 Aug 03 '13 at 03:11
  • I need to test it locally so I guessed I need it in development too? I will later need it in Heroku for production. I not sure where can I check this. @bmac151 – shoujo_sm Aug 03 '13 at 03:18
  • It's production by default, you have to set it to be development. Try adding environment: 'development' like so: runner "Company.update_all_likes", environment: 'development' Remember to rerun the whenever command to re-generate the cronjob. It should say development instead of production when you crontab. – bmac151 Aug 03 '13 at 03:28
  • Thanks! It solved! Just one curious question, so do I need to reset it back to production when I need to deploy my application to heroku? Thanks again! – shoujo_sm Aug 03 '13 at 03:44
  • Yes, just take the environment variable out of the runner and you should be good to go. Just make sure to run the whenever command on heroku as well so the cronjob gets set. – bmac151 Aug 03 '13 at 12:55