1

I would like to have a task that simply substracts a certain value from my user model at every 5 minutes. My code:

schedule.rb

every 5.minutes do
 runner 'Site::SubstractSomething.execute'
end

app/jobs/SubstractSomething.rb

module Site
 class SubstractSomething
  def initialize
  end

  def execute
   @users = ::User.all
   @users.each { |user| user.update_heat }
  end
 end
end

method inside user model:

  def update_heat
   self.heat -= 10
   self.save
  end

then I ran:

crontab -r 

whenever --update-crontab --set environment='development'

EDIT: I have taken out the job from the namespace and seems that it did the trick.Thanks for the help

LogofaT
  • 289
  • 1
  • 3
  • 17

1 Answers1

2

You will have to require 'SubstractSomething' in your schedule.rb and make sure that your $LOAD_PATH includes the directory it is situated in. See this question for some possibilities on how to achieve this.

Community
  • 1
  • 1
Patru
  • 4,481
  • 2
  • 32
  • 42
  • I have added it to the load_path and still have the same error. But accesing it from the controller like: Site::SubstractSomething.new().execute , now works. Why does it fail from crontab command.. – LogofaT Jul 08 '14 at 07:35
  • @LogofaT I vaguely remember, that `crontab` shells have a different environment, you should print out your `$LOAD_PATH` before requiring your script. If its directory is in the path it will be found. – Patru Jul 08 '14 at 13:29