每当 Rails 中的 cron 作业因“生产"数据库未配置错误而失败时 [英] Whenever cron job in Rails fails with 'production' database is not configured error

查看:31
本文介绍了每当 Rails 中的 cron 作业因“生产"数据库未配置错误而失败时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Rails 应用程序中,我使用每当 gem 运行 Sideqik 任务.当我在 rails 控制台中手动运行该命令时,它可以工作.但是每当 cron 失败并出现以下错误:它在日志中不断重复.我怎样才能解决这个问题?有什么问题?

In my Rails app I use whenever gem to run a Sideqik task. When I run that command manually in the rails console it works. But the Whenever cron fails with the following error : It keeps repeating in the log. How can I fix this? What is the problem?

/home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_connection': 'production' database is not configured. Available: ["development", "test"] (ActiveRecord::AdapterNotSpecified)
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:211:in `resolve_connection'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:139:in `resolve'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:169:in `spec'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:50:in `establish_connection'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:27:in `each'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.0/lib/active_record/railtie.rb:116:in `block in <class:Railtie>'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `run'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `each'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `call'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/railties-4.2.0/lib/rails/application.rb:352:in `initialize!'
    from /home/pubudu/Projects/istockseller/config/environment.rb:5:in `<top (required)>'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:92:in `preload'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:143:in `serve'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:131:in `block in run'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:125:in `loop'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application.rb:125:in `run'
    from /home/pubudu/.rvm/gems/ruby-2.1.5/gems/spring-1.2.0/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'

推荐答案

您遇到此错误是因为您的生产设置未正确配置以连接到您的数据库.当您从 Rails 控制台运行命令时,您正在连接到开发环境,但您的 cron 作业正在生产环境中运行.

You're encountering this error because your production settings aren't configured correctly to connect to your database. When you run the command from the Rails console, you're connecting to the development environment, but your cron job is being run in the production environment.

Rails 有 3 个内置环境.主要区别在于每个都有自己的数据库,但也有其他区别.

Rails has 3 built-in environments. The main difference is that each has their own database, but there are other differences too.

  • 生产,用于您的代码实时运行.此环境会进行大量缓存,并且不会向用户显示开发错误消息.
  • 开发,用于构建您的应用程序.这是默认环境,也是您花费大部分时间的环境.
  • 测试,用于运行自动化测试.这是分开的主要原因是你的测试套件可以清空 &在每次测试运行时重新创建数据库.这使您的测试更具可重现性,并防止意外的测试运行破坏您的开发数据库.
  • Production, for when your code is running live. This environment does a lot of caching, and doesn't show development error messages to the user.
  • Development, for building your application. This is the default environment, and is the environment that you spend most of your time in.
  • Test, for running automated tests. The main reason this is separate is so your test suite can empty & recreate the database on each test run. This makes your tests more reproducible, and stops an accidental test run from destroying your development DB.

大多数情况下,这些是通过 config/environments 中的文件配置的.数据库连接在 config/database.yml 中配置 - 您将找到用于 developmentproductiontest<的 YAML 密钥/code> 在那里.Rails 指南 提供了有关配置应用程序的更多详细信息.

Mostly, these are configured via files in config/environments. The database connections are configured in config/database.yml - you'll find YAML keys for development, production, and test in there. The Rails Guide has more details about configuring applications.

您可以通过 Rails.env 找出您正在运行的环境.例如:

You can find out which environment you're running in via Rails.env. For instance:

user@foo $ rails c
Loading development environment (Rails 4.1.1)
irb(main):001:0> Rails.env
"development"

并且您可以通过 RAILS_ENV shell 环境变量强制命令在给定环境中运行:

And you can force a command to run in a given environment via a RAILS_ENV shell environment variable:

user@foo $ RAILS_ENV=production rails c
Loading production environment (Rails 4.1.1)
irb(main):001:0> Rails.env
"production"

<小时>

要解决您的问题,您有两种选择:


To fix your problem, you have two choices:

  1. 强制您的 cron 命令在开发环境中运行.听起来您的应用程序正在开发环境中运行(因为它从控制台运行,并且您没有配置生产数据库).因此,强制 cron 作业在开发环境中运行应该可以解决这个问题.你可以通过设置 RAILS_ENV 变量直接在你的 cronjob 中设置它,就像我们上面看到的那样,但是只要让你 在命令行中指定环境 或在您的 schedule.rb 中:

  1. Force your cron command to run in the development environment. It sounds like your app is running in the development environment (because it works from the console, and you have no production database configured). So forcing the cron job to run in the development environment should fix this. You could set it in your cronjob directly by setting the RAILS_ENV variable as we saw above, but whenever lets you specify the environment on the commandline or within your schedule.rb:

set :environment, "development"

  • 修复您的应用以在生产环境中运行.如果您的应用程序处于活动状态,则它确实应该在生产环境中运行.这样会更快、更安全 - 并且让你在开发新功能时把事情搞砸,而不会有破坏用户使用的东西的风险.您必须设置数据库,并且必须在生产中测试您的应用程序.执行此操作的最佳方法因平台而异;这个堆栈溢出问题很好地总结了各种选项.

  • Fix your app to run in production. If your application is live, it really should be running in the production environment. It will be faster and more secure that way - as well as letting you mess things up when developing new features without the risk of destroying something used by your users. You'll have to set up the database, and will have to test your app in production. The best way to do this varies by platform; this Stack Overflow question has a good summary of the various options.

    这篇关于每当 Rails 中的 cron 作业因“生产"数据库未配置错误而失败时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆