我必须运行一个 Cron 任务,涉及在我的 ActiveRecord 数据库中进行一些数据清理。我正在使用每当宝石。这是代码:
时间表.rb
every 1.hour do
rake 'notifications:clear'
end
通知.耙
namespace :notifications do
task clear: :environment do
Rpush::Notification.delete_all
end
end
运行它会给我以下错误:
rake aborted!
ActiveRecord::NoDatabaseError: FATAL: role "user_prod" does not exist
我在开发环境中。这是我的数据库.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'] %>
关于为什么我的活动记录指令似乎连接到我的生产环境的任何想法?提前感谢!
用下面更新你的代码:-
在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'