我正在尝试为在Ubuntu服务器上运行的Rails 3.1应用程序设置一个守护进程。就像这样简单:
require File.expand_path('../../config/environment', __FILE__)
require 'rubygems'
require 'daemons'
Daemons.run_proc('my_script') do
loop do
puts BlogPost.count
sleep(5)
end
end
但是当我到达BlogPost.count
时,我收到以下错误:
/usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query': Mysql2::Error: MySQL server has gone away: SHOW FIELDS FROM `blog_posts` (ActiveRecord::StatementInvalid)
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `execute'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `log'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `execute'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:473:in `columns'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `initialize'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:185:in `with_connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:92:in `initialize'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:706:in `call'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:706:in `default'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:706:in `[]'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:706:in `columns'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:722:in `column_names'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/calculations.rb:192:in `aggregate_column'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/calculations.rb:213:in `execute_simple_calculation'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/calculations.rb:187:in `perform_calculation'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/calculations.rb:155:in `calculate'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/relation/calculations.rb:58:in `count'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:445:in `__send__'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.1.0/lib/active_record/base.rb:445:in `count'
from script/background_job_processor_control.rb:22
知道为什么我的脚本无法连接MySQL吗?如果我将BlogPost.count
代码放在Daemons.run_proc
之前,我可以很好地连接到MySQL。
----------编辑:解决了! ------------
对于任何对解决方案充满好奇的人,我必须做三件事才能让它发挥作用:
bundle exec
:RAILS_ENV=production bundle exec myscript.rb start
。run_proc
块中添加以下代码:ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new('/path/to/log/file.log')
run_proc
块内的Rails环境而不是脚本中的早期。我的最终脚本如下所示:
require 'rubygems'
require 'daemons'
Daemons.run_proc('my_script') do
require File.expand_path('../../config/environment', __FILE__)
ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new('/path/to/log/file.log')
loop do
puts BlogPost.count
sleep(5)
end
end
答案 0 :(得分:1)
要正确预加载脚本的Rails环境(依赖项),请按以下步骤启动它:
bundle exec my_script_ctl start
答案 1 :(得分:1)
我不知道为什么,但是当我在守护脚本之前断开数据库连接时,它会起作用,例如:
# app env
ENV['RAILS_ENV'] = 'production'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
# disconnect first
ActiveRecord::Base.connection.disconnect!
# Become a daemon
Daemons.daemonize
# connect again
ActiveRecord::Base.establish_connection ENV['RAILS_ENV']
# ... do what you need
希望这有帮助。