Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 + Daemons gem won't let me access my database

I'm trying to setup a daemon for my Rails 3.1 app running on an Ubuntu server. Just something simple like this:

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 can't connect to MySQL? If I put the BlogPost.count code before the Daemons.run_proc, I can connect to MySQL just fine.

---------- EDIT: SOLVED! ------------

For anyone curious as to the solution, I had to do three things to get this to work:

  1. Include bundle exec when starting/stopping my daemon: RAILS_ENV=production bundle exec myscript.rb start.
  2. At this point I stopped getting the MySQL2 error, but started getting a logger error. The solution was to add the following code the run_proc block: ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new('/path/to/log/file.log')
  3. Then I got a third error, and the solution was to require the Rails environment inside the run_proc block instead of earlier in the script.

My final script looks like this:

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
like image 510
NudeCanalTroll Avatar asked Nov 04 '22 03:11

NudeCanalTroll


1 Answers

To properly preload Rails environment (dependencies) for the script, start it as follows:

bundle exec my_script_ctl start
like image 73
forker Avatar answered Nov 07 '22 23:11

forker