ruby on rails - Resque.enqueue falla en la segunda ejecución
ruby-on-rails redis (1)
Estoy tratando de portar una aplicación de Rails 3.0.3 a Rails 3.1rc ... No creo que me haya perdido nada, en términos de configuración. El proceso funciona perfectamente en Rails 3.0.X y no en 3.1rc.
En la consola, hago:
Resque.enqueue(EncodeSong, Song.find(20).id, Song.find(20).unencoded_url)
Todo funciona hasta ahora. Resque-web no reporta trabajos fallidos. Y, obtengo las dos ''entradas'' del módulo EncodeSong.
Sin embargo, si ejecuta Resque.enqueue (EncodeSong, Song.find (20) .id, Song.find (20) .unencoded_url) por segunda vez, se mostrará el siguiente error en resque-web (a continuación). Para que el error desaparezca, tendría que cerrar el proceso que se está ejecutando: QUEUE = * rake environment resque: work y volver a ejecutarlo en la ventana de la consola. Pero el problema vuelve después de intentar Resque.enqueue () después de la primera vez.
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 <top (required)>''
/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 <top (required)>''
/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 `<top (required)>''
/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 `<main>''
Aquí está el resto de mi código relevante:
/config/initializers/resque.rb
require ''resque''
uri = URI.parse(APP_CONFIG[''redis_to_go_url''])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
# Load all jobs at /app/jobs
Dir["#{Rails.root}/app/jobs/*.rb"].each { |file| require file }
/app/jobs/encode_song.rb
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
lib / tasks / resque.rake
require ''resque/tasks''
task "resque:setup" => :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" => "resque:work"
No muy seguro, pero puede estar algo relacionado con este issue . Mi conjetura es que el proceso de resque maestro establece la conexión a la base de datos, mientras se cargan las clases de aplicaciones de rieles, modelos, etc., y esa conexión se corrompe en el proceso de bifurcación () (¿en la salida?).
Cualquier ayuda / dirección será apreciada.
EDITAR:
Si elimino el siguiente bloque de lib / tasks / resque.rake :
Resque.after_fork do |job|
ActiveRecord::Base.establish_connection
end
Y en la consola, ejecute Resque.enqueue (EncodeSong, Song.find (20) .id, Song.find (20) .unencoded_url)
Recibo un nuevo error (en la consola donde se ejecutó QUEUE = * rake environment resque: se ejecutó el trabajo):
Error PGError: ERROR: prepared statement "a3" already exists
: SELECT "songs".* FROM "songs" WHERE "songs"."id" = $1 LIMIT 1
Parece que esto puede ser un error con el adaptador? Podría estar equivocado aquí. ¿Tus pensamientos?
Esto me lo ha resuelto:
require ''resque/tasks''
task "resque:setup" => :environment do
ENV[''QUEUE''] = ''*''
Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection }
end
Fuente: comentario carhartl sobre Github