0

我正在使用延迟作业(3.0.1)和延迟作业活动记录(0.4.3)在触发此操作时基于另一个操作发送电子邮件

Delayed::Job.enqueue(MailingJob.new(@auction.id) , 0 , date)

这是邮寄工作

class MailingJob < Struct.new(:auction_id)
   def perform
     sql = "select a.id , u.fname , u.lname , u.email , p.name
            from auctions a
            join products p on a.product_id = p.id
            join auction_followers af on a.id = af.auction_id
            join users u on af.user_id = u.id
            where a.id = #{auction_id}"
     result = ActiveRecord::Base.connection.execute(sql)
     result.each(:as => :hash) do |row|
       AuctionMailer.delay.auction_start(row)
     end
   end
 end

这是 AuctionMailer

 class AuctionMailer < ActionMailer::Base
      default from: "noreply@yabalashdeals.com"
      def auction_start(data)
         @shared = data
         mail(:to => data['email'], :subject => "Yabalash Auction started")
      end
 end

当我运行 rake jobs:work 时,我得到 2 个以 4.6510 j/s 的速度处理的工作,0 个失败......但没有发送电子邮件我做了一个测试功能来检查拍卖邮件,它工作正常

这是没有错误的日志

日志/生产.log

Sent mail to hesham.kanany@gmail.com (99ms)
Rendered text template (0.0ms)
Completed 200 OK in 288ms (Views: 1.1ms | ActiveRecord: 1.3ms)

日志/delayed_job.log

MaillingJob completed after 0.0035
1 jobs processed at 93.1503 j/s, 0 failed ...

完成 抱歉没有发布这个问题的解决方案我想出了.deliver()如果你使用enqueue()必须调用它......现在电子邮件发送完美

4

1 回答 1

0

在我看来,您的延迟工作代码是错误的。尝试将其移至可以从控制台调用的方法中。

具体来说,您可能想要删除 :as => :hash。

def perform
 sql = "select a.id , u.fname , u.lname , u.email , p.name
        from auctions a
        join products p on a.product_id = p.id
        join auction_followers af on a.id = af.auction_id
        join users u on af.user_id = u.id
        where a.id = #{auction_id}"
 result = ActiveRecord::Base.connection.execute(sql)
 result.each do |row|
   AuctionMailer.delay.auction_start(row)
 end

结尾

于 2013-03-11T16:17:42.727 回答