ruby-on-rails – 无法在heroku上排队不响应perform – delayed_job的项目

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 无法在heroku上排队不响应perform – delayed_job的项目前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在heroku上使用delayed_job,我收到以下错误
Cannot enqueue items which do not respond to perform

我正在使用插件http://github.com/pedro/delayed_job

我正在使用以下cron rake任务(cron.rake):

task :cron => :environment do
require 'heroku'    
    puts "starting the cron job at #{Date.today}"

    heroku = Heroku::Client.new(ENV['HEROKU_USER'],ENV['HEROKU_PASS'])
    heroku.set_workers(ENV['HEROKU_APP'],1)

    Contact.all.each do |contact|
      email = contact.email_today #email_today is a contact method returning email object if <= today

      unless contact.email_today == "none"
        puts contact.first_name
        puts email.days
        puts contact.date_entered
        puts email.substituted_subject(contact,contact.colleagues)

        # create the Contact Email object that gets created and sent

        contact_email = ContactEmail.new
        contact_email.contact_id = contact.id
        contact_email.email_id = email.id

        contact_email.subject = email.substituted_subject(contact,contact.colleagues)
        contact_email.date_sent = Date.today
        contact_email.date_created = Date.today

        contact_email.body = email.substituted_message(contact,contact.colleagues)

        contact_email.status = "sent" 

        Delayed::Job.enqueue OutboundMailer.deliver_campaign_email(contact,contact_email)

        contact_email.save #now save the record
        puts "save contact_email:"
        puts contact_email.inspect

      end #end unless

    end #end do

    heroku.set_workers(ENV['HEROKU_APP'],0)
    puts "set heroku workers to 0"

end

这是我正在使用的邮件

class OutboundMailer < Postage::Mailer 

  def campaign_email(contact,email)
    subject    email.subject
    recipients contact.email
    from       'Me <me@me.com>'
    sent_on    Date.today

    body       :email => email
  end

问题:为什么我收到错误,我该怎么做才能解决

解决方法

一个作业通常是一个带有“执行”方法的ruby对象,因此将邮件发送无效,您将不得不创建如下所示的作业对象,
 class SomeMailJob < Struct.new(:contact,:contact_email) 
   def perform
     OutboundMailer.deliver_campaign_email(contact,contact_email)
   end
 end

创建一个文件some_mail_job.rb并将其放在/ lib中

在上面的代码中,用en替换enqueue语句

Delayed::Job.enqueue SomeMailJob.new(contact,contact_email)

原文链接:https://www.f2er.com/ruby/268319.html

猜你在找的Ruby相关文章