KawaLo
KawaLo

Reputation: 2284

ActiveRecord task executing in wrong environment with Whenever

I have to run a Cron task involving some data cleaning in my ActiveRecord database. I am using Whenever gem. Here is the code :

schedule.rb

every 1.hour do
  rake 'notifications:clear'
end

notifications.rake

namespace :notifications do
  task clear: :environment do
    Rpush::Notification.delete_all
  end
end

Running this gives me the following error:

rake aborted!
ActiveRecord::NoDatabaseError: FATAL:  role "user_prod" does not exist

I am in development environment. Here is my database.yml file :

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: development_database

test:
  <<: *default
  database: test_database

staging:
  <<: *default
  database: staging_database
  username: user_staging
  password: <%= ENV['DATABASE_PASSWORD'] %>


production:
  <<: *default
  database: production_database
  username: user_prod
  password: <%= ENV['DATABASE_PASSWORD'] %>

Any ideas on why my ActiveRecord instruction seems to connect to my production environment ? Thanks in advance !

Upvotes: 1

Views: 80

Answers (1)

code_aks
code_aks

Reputation: 2074

Update Your code with below :-

In schedule.rb file :-

every 1.hour do
  rake 'notifications:clear', :environment => "development"
end

And finally executed this command :-

whenever --update-crontab

OR

Clear existing cron jobs.

crontab -r

Update cronjob with the environment.

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

Upvotes: 1

Related Questions