在我的Rails应用程序中,每当gem运行Sideqik任务时我都会使用它。当我在rails控制台中手动运行该命令时,它可以工作。但是每当cron因以下错误而失败时:它会在日志中不断重复。我怎样才能解决这个问题?有什么问题?
/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>'
答案 0 :(得分:6)
您遇到此错误,因为未正确配置生产设置以连接到您的数据库。当您从Rails控制台运行命令时,您将连接到开发环境,但您的cron作业正在生产环境中运行。
Rails有3个内置环境。主要区别在于每个都有自己的数据库,但也存在其他差异。
大多数情况下,这些是通过config/environments
中的文件配置的。数据库连接在config/database.yml
中配置 - 您可以在其中找到development
,production
和test
的YAML密钥。 Rails Guide包含有关配置应用程序的更多详细信息。
您可以通过Rails.env
找出您正在运行的环境。例如:
user@foo $ rails c
Loading development environment (Rails 4.1.1)
irb(main):001:0> Rails.env
"development"
您可以通过RAILS_ENV
shell环境变量强制命令在给定环境中运行:
user@foo $ RAILS_ENV=production rails c
Loading production environment (Rails 4.1.1)
irb(main):001:0> Rails.env
"production"
要解决您的问题,您有两种选择:
强制您的cron命令在开发环境中运行。听起来您的应用程序正在开发环境中运行(因为它可以在控制台中运行,并且您没有配置生产数据库)。因此,强制cron作业在开发环境中运行应该解决这个问题。您可以通过设置我们在上面看到的RAILS_ENV
变量直接在您的cronjob中设置它,但无论何时只允许specify the environment on the commandline或在schedule.rb
内:
set :environment, "development"
修复您的应用以在生产中投放。如果您的应用程序是活动的,它确实应该在生产环境中运行。它会更快,更安全 - 并且在开发新功能时让你搞砸了,而不会有破坏用户使用的东西的风险。您必须设置数据库,并且必须在生产中测试您的应用程序。这样做的最佳方式因平台而异; this Stack Overflow question对各种选项有一个很好的总结。