title: Resque.enqueue failing on second run
body: <p>I am trying to port an app from Rails 3.0.3 to Rails 3.1rc... I don't think I've missed out anything, in terms of configuration. The process works perfectly in Rails 3.0.X and not in 3.1rc.</p>
  
  <p>In console, I do:</p>
  
  <pre><code>Resque.enqueue(EncodeSong, Song.find(20).id, Song.find(20).unencoded_url)
  </code></pre>
  
  <p>Everything works so far. Resque-web reports no failed jobs. And, I get the two 'puts' from module EncodeSong. </p>
  
  <p>However, running <strong>Resque.enqueue(EncodeSong, Song.find(20).id, Song.find(20).unencoded_url)</strong> a second time will return the following error in resque-web (below). To make the error go away, I would have to close the process thats running: <strong>QUEUE=* rake environment resque:work</strong> and rerun it in the console window. But the problem comes back after trying to Resque.enqueue() after the first time.</p>
  
  <pre><code>Class
      EncodeSong
  Arguments
  
      20
      "https://bucket_name.s3.amazonaws.com/unencoded/users/1/songs/test.mp3"
  
  Exception
      PGError
  Error
      server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.
  
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/postgresql_adapter.rb:272:in `exec'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/postgresql_adapter.rb:272:in `block in clear_cache!'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/postgresql_adapter.rb:271:in `each_value'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/postgresql_adapter.rb:271:in `clear_cache!'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/postgresql_adapter.rb:299:in `disconnect!'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:191:in `block in disconnect!'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in `each'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:190:in `disconnect!'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activesupport-3.1.0.rc1/lib/active_support/core_ext/module/synchronization.rb:35:in `block in disconnect_with_synchronization!'
      /Users/Chris/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activesupport-3.1.0.rc1/lib/active_support/core_ext/module/synchronization.rb:34:in `disconnect_with_synchronization!'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/abstract/connection_pool.rb:407:in `remove_connection'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/abstract/connection_specification.rb:116:in `remove_connection'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/abstract/connection_specification.rb:79:in `establish_connection'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/abstract/connection_specification.rb:60:in `establish_connection'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/activerecord-3.1.0.rc1/lib/active_record/connection_adapters/abstract/connection_specification.rb:55:in `establish_connection'
      /Users/Chris/Sites/site_name/lib/tasks/resque.rake:17:in `block (2 levels) in &lt;top (required)&gt;'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/resque-1.16.1/lib/resque/worker.rb:355:in `call'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/resque-1.16.1/lib/resque/worker.rb:355:in `run_hook'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/resque-1.16.1/lib/resque/worker.rb:162:in `perform'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/resque-1.16.1/lib/resque/worker.rb:130:in `block in work'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/resque-1.16.1/lib/resque/worker.rb:116:in `loop'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/resque-1.16.1/lib/resque/worker.rb:116:in `work'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/resque-1.16.1/lib/resque/tasks.rb:27:in `block (2 levels) in &lt;top (required)&gt;'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/task.rb:205:in `call'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/task.rb:205:in `block in execute'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/task.rb:200:in `each'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/task.rb:200:in `execute'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
      /Users/Chris/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/task.rb:151:in `invoke_with_call_chain'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/task.rb:144:in `invoke'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/application.rb:112:in `invoke_task'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/application.rb:90:in `block (2 levels) in top_level'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/application.rb:90:in `each'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/application.rb:90:in `block in top_level'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/application.rb:129:in `standard_exception_handling'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/application.rb:84:in `top_level'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/application.rb:62:in `block in run'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/application.rb:129:in `standard_exception_handling'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/lib/rake/application.rb:59:in `run'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/gems/rake-0.9.0/bin/rake:31:in `&lt;top (required)&gt;'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/bin/rake:19:in `load'
      /Users/Chris/.rvm/gems/ruby-1.9.2-p136@railspre/bin/rake:19:in `&lt;main&gt;'
  </code></pre>
  
  <p>Here is the rest of my relevant code:</p>
  
  <p><strong>/config/initializers/resque.rb</strong></p>
  
  <pre><code>require 'resque'
  
  uri = URI.parse(APP_CONFIG['redis_to_go_url'])
  Resque.redis = Redis.new(:host =&gt; uri.host, :port =&gt; uri.port, :password =&gt; uri.password)
  
  # Load all jobs at /app/jobs
  Dir["#{Rails.root}/app/jobs/*.rb"].each { |file| require file }
  </code></pre>
  
  <p><strong>/app/jobs/encode_song.rb</strong></p>
  
  <pre><code>module EncodeSong
    @queue = :encode_song
  
    def self.perform(media_id, s3_file_url)
      begin
        media = Song.find(media_id)
        puts 'foo11111'
        puts media.id
      rescue
        puts "Error #{$!}"
      end
    end
  end
  </code></pre>
  
  <p><strong>lib/tasks/resque.rake</strong></p>
  
  <pre><code>require 'resque/tasks'
  
  task "resque:setup" =&gt; :environment do
    ENV['QUEUE'] = '*'
  
    # ONLY on Heroku, since they are still running PostgreSql 8 on their shared plan.
    # This block of code is not needed on PostgreSql 9, as tested on local environment.
    # Issue: My best guess is that master resque process establishes connection to db,
    # while loading rails app classes, models, etc, and that connection becomes corrupted
    # in fork()ed process (on exit?). Possible fix is to reestablish the connection the AR
    # after a Resque fork.
    Resque.after_fork do |job|
      ActiveRecord::Base.establish_connection
    end
  
  end
  
  desc "Alias for resque:work (To run workers on Heroku)"
  task "jobs:work" =&gt; "resque:work"
  </code></pre>
  
  <p>Not very sure, but it may be somewhat related to this <a href="https://stackoverflow.com/questions/2611747/rails-resque-workers-fail-with-pgerror-server-closed-the-connection-unexpectedly">issue</a>. My guess is that master resque process establishes connection to db, while loading rails app classes, models, etc, and that connection becomes corrupted in fork()ed process (on exit?).</p>
  
  <p>Any help / direction will be appreciated.</p>
  
  <p><strong><em>EDIT:</em></strong></p>
  
  <p>If I remove the following block from <strong>lib/tasks/resque.rake</strong>:</p>
  
  <pre><code>  Resque.after_fork do |job|
      ActiveRecord::Base.establish_connection
    end
  </code></pre>
  
  <p>And in console, run <strong>Resque.enqueue(EncodeSong, Song.find(20).id, Song.find(20).unencoded_url)</strong></p>
  
  <p>I get a new error (in console where QUEUE=* rake environment resque:work was run):</p>
  
  <pre><code>Error PGError: ERROR:  prepared statement "a3" already exists
  : SELECT  "songs".* FROM "songs"  WHERE "songs"."id" = $1 LIMIT 1
  </code></pre>
  
  <p>It seems, this may be a bug with the adapter? Could be wrong here. Your thoughts?</p>