Search code examples
ruby-on-railsherokuscheduled-tasks

Heroku/Rails: Task scheduler is working but does nothing


I have an app running on heroku and once a day it runs taks called every_day. Task scheduler is working fine but it does nothing to my database. I also tried to run it manually and it's the same result. I think that problem is with my code and I will be thankful if someone could tell me what is wrong. This code of task:

task :every_day => :environment do
  unactive_auction = Auction.where(ended: false).where('created_at < ?', 14.days.ago)
  unactive_auction.update_all(:ended => true)
end

When I run it manually I get this:

, [2017-06-07T07:08:26.348223 #4] DEBUG -- :   Auction Load (1.7ms)  SELECT  "auctions".* FROM "auctions" ORDER BY "auctions"."id" ASC LIMIT $1  [["LIMIT", 1000]]
D, [2017-06-07T07:08:26.677182 #4] DEBUG -- :   SQL (1.3ms)  UPDATE "auctions" SET "ended" = 't' WHERE "auctions"."ended" = $1 AND (created_at < '2017-05-24 07:08:26.674476')  [["ended", false]]

I have no idea what I'm doing wrong. It looks like it's working but... But it does nothing.


Solution

  • Are you sure there are objects satisfying your query parameters ? Run in the rails console:

     unactive_auction = Auction.where("ended is ? and created_at < ?", false, 14.days.ago)
    

    And see if it returns anything before running the task.