title: Rails 3.1 + Daemons gem won't let me access my database
body: <p>I'm trying to setup a daemon for my Rails 3.1 app running on an Ubuntu server. Just something simple like this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>But when I get to <code>BlogPost.count</code>, I get the following error:</p>
  
  <pre><code>/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
  </code></pre>
  
  <p>Any idea why my script can't connect to MySQL? If I put the <code>BlogPost.count</code> code before the <code>Daemons.run_proc</code>, I can connect to MySQL just fine.</p>
  
  <p>---------- EDIT: SOLVED! ------------</p>
  
  <p>For anyone curious as to the solution, I had to do three things to get this to work:</p>
  
  <ol>
  <li>Include <code>bundle exec</code> when starting/stopping my daemon: <code>RAILS_ENV=production bundle exec myscript.rb start</code>.</li>
  <li>At this point I stopped getting the MySQL2 error, but started getting a logger error. The solution was to add the following code the <code>run_proc</code> block: <code>ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new('/path/to/log/file.log')</code></li>
  <li>Then I got a third error, and the solution was to require the Rails environment inside the <code>run_proc</code> block instead of earlier in the script.</li>
  </ol>
  
  <p>My final script looks like this:</p>
  
  <pre><code>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
  </code></pre>