ActiveRecord 任务在错误的环境中执行,无论何时

ActiveRecord task executing in wrong environment with Whenever

我必须 运行 一项涉及 ActiveRecord 数据库中一些数据清理的 Cron 任务。我正在使用 Whenever gem。这是代码:

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

运行 这给了我以下错误:

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

我在开发环境中。这是我的 database.yml 文件:

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'] %>

关于为什么我的 ActiveRecord 指令似乎连接到我的生产环境有什么想法吗?提前致谢!

使用以下内容更新您的代码:-

schedule.rb文件中:-

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

最后执行了这条命令:-

whenever --update-crontab

清除现有的 cron 作业。

crontab -r

使用环境更新 cronjob。

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