I am trying to configure a daemon for my Rails 3.1 application running on a Unbuntu server. Just something simple:
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
But when I get to BlogPost.count, I get the following error:
/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
Any idea why my script cannot connect to MySQL? If I put the code BlogPost.countbefore Daemons.run_proc, I can just connect to MySQL.
---------- EDIT: SOLVING! ------------
For anyone interested in a solution, I had to do three things to get this to work:
- Enable
bundle execwhen you start / stop my demon: RAILS_ENV=production bundle exec myscript.rb start. - At this point, I stopped getting the MySQL2 error, but started getting a log error. The solution was to add the following code to the block
run_proc:ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new('/path/to/log/file.log') - , Rails
run_proc script.
script :
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