3

I am trying to make a task run every hour using the Whenever gem.

#check_price.rake
namespace :check_price do
  desc "TODO"
  task check_now: :environment do
    price_sync = PriceSyncLibrary.new
    Product.all.each do |product|
      product.price.push(price_sync.get_price_from_link(product.flipkart_link))
      product.time.push(Time.now)
      product.save!
    end
  end  
end

This is my schedule.rb file

every 1.minute do
  rake "check_price:check_now", environment: 'development' 
end

When I run

rake check_price:check_now

in the terminal, I am getting the result I want, but the same rake task is not running every minute as specified in the schedule.rb file. Am I missing something?

When I run

crontab -l

in the console, I get this result.

# Begin Whenever generated tasks for: /home/raghav/workspace/apps/Shotgun/config/schedule.rb
* * * * * /bin/bash -l -c 'cd /home/raghav/workspace/apps/Shotgun && RAILS_ENV=production bundle exec rake check_price:check_now --silent'

# End Whenever generated tasks for: /home/raghav/workspace/apps/Shotgun/config/schedule.rb

# Begin Whenever generated tasks for: Shotgun
* * * * * /bin/bash -l -c 'cd /home/raghav/workspace/apps/Shotgun && RAILS_ENV=production bundle exec rake check_price:check_now --silent'

# End Whenever generated tasks for: Shotgun
raghav_vc
  • 65
  • 7

1 Answers1

1

In your schedule.rb file, your environment is set to development and in the response of crontab -l command the environment is set to production, You can set the environment like this,

set :environment, "development"
set :output, {:error => "log/cron_error_log.log", :standard => "log/cron_log.log"}

every 1.minute do
  rake "check_price:check_now"
end
Bijal Gajjar
  • 47
  • 1
  • 6