Tôi đang cố thiết lập một daemon cho ứng dụng Rails 3.1 chạy trên máy chủ Unbuntu. Chỉ cần một cái gì đó đơn giản như thế này:Rails 3.1 + Đá quý Daemons sẽ không cho phép tôi truy cập vào cơ sở dữ liệu của tôi
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
Nhưng khi tôi nhận được để BlogPost.count
, tôi nhận được lỗi sau:
/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
Bất cứ ý tưởng tại sao kịch bản của tôi không thể kết nối với MySQL? Nếu tôi đặt mã BlogPost.count
trước số Daemons.run_proc
, tôi có thể kết nối với MySQL tốt.
---------- CHỈNH SỬA: GIẢI PHÁP! ------------
Đối với bất cứ ai tò mò như các giải pháp, tôi đã phải làm ba việc để có được điều này để làm việc:
- Bao gồm
bundle exec
khi khởi động/stoping daemon của tôi :RAILS_ENV=production bundle exec myscript.rb start
. - Tại thời điểm này, tôi đã ngừng nhận lỗi MySQL2, nhưng bắt đầu nhận được lỗi nhật ký. Giải pháp là thêm mã sau vào khối
run_proc
:ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new('/path/to/log/file.log')
- Sau đó, tôi gặp lỗi thứ ba và giải pháp là yêu cầu môi trường Rails bên trong khối
run_proc
thay vì trước đó trong tập lệnh.
kịch bản cuối cùng của tôi trông như thế này:
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
bạn có bắt đầu daemon với 'bundle exec my_script_ctl start' không? – forker
Hmm, không có tôi đang làm 'RAILS_ENV = kịch bản ruby sản xuất/myscript_control.rb start'. Tôi vừa thử làm theo cách của bạn và giờ tôi gặp phải một lỗi khác: /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.0/lib/active_support/buffered_logger.rb:109:in 'write ': luồng đóng (IOError) – NudeCanalTroll
Vì vậy, tôi đã làm theo lời khuyên ở đây (http://stackoverflow.com/questions/5809678/rails-3-daemons-gem-exception-when-querying-model) để loại bỏ lỗi đăng nhập và tôi gặp lỗi mới cho biết một số bảng không tồn tại trong cơ sở dữ liệu phát triển của tôi. Nhưng tôi đang cố gắng để chạy điều này về sản xuất, vì vậy tôi đã thêm 'RAILS_ENV = production' trước' bundle exec', và bây giờ chúng ta quay lại lỗi ban đầu của "MySQL server đã biến mất". : ( – NudeCanalTroll