2

我正在使用延迟作业在 Heroku 上发送电子邮件,并且需要能够执行各种钩子后处理。下面的代码在开发环境中运行良好,但在 Heroku 上运行时无法执行。

class Notification < ActiveRecord::Base

  # Handler for Delayed Notifications Hooks
  #- takes in a Delayed::Job object and an optional exception
  def self.processed!(job,exception=nil)
     pay = job.payload_object.args
     # ... do something with the payload object here
  end

  class << self
    def deliver(method,to,ref_obj)
      to.each do |email|
        n = Notification.new(:method => method,:to => email,:delivery=>'email')
        n.obj = ref_obj
        n.save
        # This is where we send the actual email via ActionMailer
        Email.send(method, n).deliver
      end
    end
    handle_asynchronously :deliver

    # Failure handler for Delayed::Job.  
    # Marks our Notification Object as failed
    def error(job,exception)
      Notification.processed!(job,exception)
    end 
    # Success handler for Delayed::Job.  
    # Marks our Notification Object as sent
    def success(job)
      Notification.processed!(job)
    end
  end
end

这就是拨打电话的方式:

user = User.find()   # Fetch a user object
Notification.deliver('joined','someone@email.com',user)

电子邮件通过 Email.joined 正确发送,延迟作业记录已成功从数据库中删除,但延迟作业挂钩success从未被调用。

同样,这在开发中完美运行,但在部署到 Heroku 时无法运行。

4

0 回答 0