ruby-on-rails - ActiveRecord 任务在错误的环境中执行,无论何时
问题描述
我必须在我的 ActiveRecord 数据库中运行涉及一些数据清理的 Cron 任务。我正在使用每当宝石。这是代码:
日程安排.rb
every 1.hour do
rake 'notifications:clear'
end
通知.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'
推荐阅读
- python - 如何将 Jacobian 输入到 scikits.odes.odeint
- django - 如何为我在谷歌云平台上运行的网站设置免费 SSL 证书。(视窗 10)
- html - 垂直填充但根据内容保持宽度
- holochain - Holochain:如何使用生产指挥查询实例?
- javascript - 如何在 p5.js 中使用 dist() 计算旋转?
- flutter - 耀斑和颤振问题
- amazon-web-services - AWS SAM - 如何将参数传递给 Lambda 函数
- android - 带有 WebView 的 Android 弹出登录屏幕
- c# - 使用 SQL 选择结果写入 C# DataTable 时出现转换失败错误
- r - 更改数据帧结构(两个数据帧为一个)