2010-12-02 7 views
5

Estoy tratando de enviar correo en una aplicación de Rails 3 usando el trabajo retrasado de collectiveidea. Si trato de enviar correos regularmente (.deliver) funciona bien, pero tan pronto como cambio al trabajo retrasado, las cosas se desmoronan.Rieles: Absolutamente perplejo con el trabajo retrasado. No recibo argumentos en ningún lado

El error estándar de recibo en la tabla delayed_job MySQL es:

{método no definido `nombre' para nula: NilClass ...

donde 'nombre' es el primer argumento en la vista de correo (@ contact.name). Esto funciona bien si retiro el trabajo retrasado.

Si quito todas las referencias a @contact en la vista, consigo

{Un remitente (Return-Path, remitente o De) requerido para enviar un mensaje

En pocas palabras , no parece entender ningún argumento en absoluto.

Todo el código correspondiente a continuación - si alguien tiene alguna sugerencia para este sería muy apreciada

CONTROLADOR

def sendmail 
    @contact = Contact.new(params[:contact]) 
    if @contact.save 
     ContactMailer.delay.contact_mail(@contact) 
     flash[:notice] = 'Your message has been successfully sent' 
     redirect_to root_path 
    else 
     render :action => 'index' 
    end 
    end 

Mailer:

class ContactMailer < ActionMailer::Base 
    default :from => "[email protected]" 

    def contact_mail(contact) 
    @contact = contact 
    mail(:to => '[email protected]', :subject => 'Contact Form Query', :from => '[email protected]', :content_type => 'text/plain') 
    end 
    handle_asynchronously :contact_mail, :run_at => Proc.new { 2.seconds.from_now } 
end 

Mailer VEA:

You have received a new query: 
----------------------------------- 

<%= @contact.name %> 

<%= @contact.business %> 

<%= @contact.phone %> 

----------------------- 
<%= @contact.message %> 
----------------------- 

inicializador:

Delayed::Worker.destroy_failed_jobs = false 
Delayed::Worker.max_run_time = 5.weeks 

Y, por último, el mensaje de error completo:

{undefined method `name' for nil:NilClass 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/whiny_nil.rb:48:in `method_missing' 

/Users/john/Websites/Rails/InDevelopment/connectted/app/views/contact_mailer/contact_mail.html.erb:4:in `_app_views_contact_mailer_contact_mail_html_erb___3386534441642202773_2166008980__4301703149293725172' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/template.rb:135:in `block in render' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/notifications.rb:54:in `instrument' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/template.rb:127:in `render' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:59:in `block in _render_template' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/notifications.rb:52:in `block in instrument' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0/lib/active_support/notifications.rb:52:in `instrument' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:56:in `_render_template' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/action_view/render/rendering.rb:26:in `render' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:114:in `_render_template' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:108:in `render_to_body' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:101:in `render_to_string' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:92:in `render' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/deprecated_api.rb:111:in `render' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:210:in `block in create_parts' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:208:in `each' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:208:in `create_parts' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/deprecated_api.rb:143:in `create_parts' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:77:in `process' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:446:in `process' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:441:in `initialize' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:425:in `new' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:425:in `method_missing' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/performable_mailer.rb:6:in `perform' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/backend/base.rb:83:in `invoke_job' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:119:in `block (2 levels) in run' 

/Users/john/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/timeout.rb:57:in `timeout' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed 
/worker.rb:119:in `block in run' 

/Users/john/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:118:in `run' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:176:in `reserve_and_run_one_job' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:103:in `block in work_off' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:102:in `times' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:102:in `work_off' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:77:in `block (2 levels) in start' 

/Users/john/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:76:in `block in start' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:73:in `loop' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/worker.rb:73:in `start' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:100:in `run' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:79:in `block in run_process' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/application.rb:250:in `call' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/application.rb:250:in `block in start_proc' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/daemonize.rb:199:in `call' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/daemonize.rb:199:in `call_as_daemon' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/application.rb:254:in `start_proc' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/application.rb:294:in `start' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/controller.rb:70:in `run' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons.rb:193:in `block in run_proc' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in `call' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in `catch_exceptions' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/gems/daemons-1.1.0/lib/daemons.rb:192:in `run_proc' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:78:in `run_process' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:72:in `block in daemonize' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:70:in `times' 

/Users/john/.rvm/gems/ruby-1.9.2-p0/bundler/gems/delayed_job-c933ffcd776a/lib/delayed/command.rb:70:in `daemonize' 
script/delayed_job:5:in `<main>' | 
+1

Estoy asumiendo ninguna de las respuestas a continuación resuelve su problema? ¿Alguna vez resolviste esto? Estoy teniendo un problema similar, excepto que en mi caso mis trabajos están "teniendo éxito", pero en realidad no está sucediendo nada. Agregué el registro a la primera línea y no obtuve nada. Sospecho que está relacionado con a) rvm (mi aplicación usa 1.9.2, mi sistema ruby ​​es 1.8.7) o b) mongoid de alguna manera (aunque está encontrando los trabajos bien). Voy a investigar la cosa de rvm, aunque realmente no estoy seguro de cómo hacer que el daemon corra bajo 1.9.2 en este punto. – Brad

+0

Hola Brad: lamentablemente no se ha encontrado ninguna respuesta. Creo que recurrí a programar manualmente los correos del servidor de forma manual, lo cual no es realmente una solución ideal. – PlankTon

+0

Bummer. No lo he descubierto tampoco. En mi caso, si creo el trabajo en la consola de rails y luego llamo "Delayed :: Worker.new.start", funcionará. Pero si ejecuto "script/delayyed_job start" en el directorio raíz de la aplicación en otro terminal, no obtengo absolutamente nada. Encuentra los trabajos, el registro dice "1 trabajo encontrado, 0 falló", pero no pasó nada. No tengo ni idea de cómo depurar esto, ya que el trabajador comienza en otro proceso en ese caso, pero de alguna manera debe estar relacionado con ese proceso que se ejecuta en un entorno diferente. – Brad

Respuesta

11

el problema es con el código en su programa de correo de tomar el objeto en lugar de los objetos Identificación:

def contact_mail(contact) 
    @contact = contact 
    mail(:to => '[email protected]', :subject => 'Contact Form Query', :from => '[email protected]', :content_type => 'text/plain') 
end 

Usted debe cambiar para que esto:

def contact_mail(contact_id) 
    @contact = Contact.find(contact_id) 
    mail(:to => '[email protected]', :subject => 'Contact Form Query', :from => '[email protected]', :content_type => 'text/plain') 
end 

Nunca pasan los objetos reales a su remitente. Simplemente pase los id y recupérelos al procesarlos.

0

¿Por qué no funciona? No estoy seguro, me imagino que tiene que ver con algún vudú de ActionMailer.

¿Cómo funciona? Resuma las piezas de trabajo retrasadas en otra clase. Haga que el método que se demora no haga más que enviarles un correo electrónico; lo que significa que la presentación del correo electrónico se realiza en tiempo real, pero el envío se retrasa. Esto debería evitar su problema y lograr el envío asincrónico de correos electrónicos.

0

Oye, esto podría estar lejos, pero no está enviando ningún parámetro a su CORREO. Aquí hay una copia de mi referid_email.rb Guardo en mi lib/dir. Lo llamo con delayed_job:

#my controller 
Delayed::Job.enqueue ReferredEmail.new(params[:subject], params[:editor1]) 

#Reffered_Email.rb 

class ReferredEmail < Struct.new(:subject, :editor1) 
    def perform 
    (CardReferral.all.map(&:email) - CardSignup.all.map(&:email)).each do |cf| 
     @cf = CardReferral.find_by_email(cf) 
     Notifier.deliver_referred_magic_email(User.find(@cf.user_id), @cf.email, @cf.name, @cf.message, subject, editor1) 
    end 
    end 
end 
0

Parece que están utilizando tanto:

ContactMailer.delay.contact_mail(@contact) 

y

handle_asynchronously :contact_mail, :run_at => Proc.new { 2.seconds.from_now } 

que sólo se debe hacer una o la otra. Intente quitar el handle_asynchronously y vea si eso soluciona el problema.

1

Tuve el mismo problema y lo solucioné moviendo todos los atributos a variables de instancia. Así, en la vista:

<%= @contact.name %> 
<%= @contact.business %> 
<%= @contact.phone %> 

se convierte en:

<%= @name %> 
<%= @business %> 
<%= @phone %> 

y Acabo de mirar las variables de instancia en el método de anuncio publicitario. Así, en el programa de correo:

def contact_mail(contact) 
    @contact = contact 
    mail(:to => '[email protected]', :subject => 'Contact Form Query', :from => '[email protected]', :content_type => 'text/plain') 
end 

se convierte en:

def contact_mail(contact) 
    @name = contact.name 
    @business = contact.business 
    @phone = contact.phone 
    mail(:to => '[email protected]', :subject => 'Contact Form Query', :from => '[email protected]', :content_type => 'text/plain') 
end 
Cuestiones relacionadas