当我在开发环境中运行我的应用程序时,resque正在“测试”环境中运行,但是独角兽正在“开发”下运行。
我做了一些调试,发现当ENV['RAILS_ENV']
调用config/application.rb
时,require 'rails/all'
被设置为“test”,但仅适用于resque进程,而不是独角兽的过程。我不确定这两者之间会有什么不同之处。
以下是我正在使用的版本:(Rails 4.0.0,Ruby 2.0.0-p195,resque 1.25.2,resque-pool 0.3.0)
这是我的Procfile.dev
:
web: bundle exec unicorn -p $PORT -c config/unicorn.rb
resque: env TERM_CHILD=1 RESQUE_TERM_TIMEOUT=10 VVERBOSE=1 bundle exec resque-pool --config config/resque-pool/development.yml
我更新了config/application.rb
以包含:
require File.expand_path('../boot', __FILE__)
puts "before rails/all: #{ENV['RAILS_ENV'].inspect}"
require 'rails/all'
puts "after rails/all: #{ENV['RAILS_ENV'].inspect}"
然后在运行应用程序时:
$ rvmsudo foreman start -p 80 -f Procfile.dev
18:01:56 web.1 | started with pid 11211
18:01:56 resque.1 | started with pid 11212
18:01:57 web.1 | I, [2014-05-24T18:01:57.865524 #11211] INFO -- : Refreshing Gem list
18:01:57 web.1 | before rails/all: nil
18:01:57 resque.1 | resque-pool-manager[flow][11212]: Resque Pool running in development environment
18:01:58 resque.1 | before rails/all: nil
18:01:58 web.1 | after rails/all: nil
18:01:58 resque.1 | after rails/all: "test"
正如您所看到的,web.1
RAILS_ENV在调用require 'rails/all'
之前和之后未定义,但在resque.1
中,它被设置为“test”。
这会导致问题,因为resque worker正在从Web worker连接到不同的数据库。
我可以通过在ENV['RAILS_ENV'] ||= 'development'
之前添加require 'rails/all'
来解决此问题,但我还没有在其他应用中遇到过这种问题,因此我担心会出现更大的配置问题。< / p>
我还在整个应用程序中搜索了“RAILS_ENV”,并且只找到了一个将其设置为测试的实例(在test/test_helper.rb
中),但我已将其更改为将其设置为“development”并没有解决这个问题,因为它显然在rails加载时设置得更早。