【问题标题】:ActionMailer send email in background, queue_classicActionMailer 在后台发送电子邮件,queue_classic
【发布时间】:2014-03-14 04:08:52
【问题描述】:

我正在使用queue_classic
如何使用 queue_classic 发送以下电子邮件?

Notifier.welcome(david).deliver # sends the email

更新

我在 User 类中添加了一个新的类方法

def.say_hello
  puts "hello!"
  Notifier.welcome(@david).deliver
end

QC.enqueue("puts", "hello world") 工作正常,
QC.enqueue("User.say_hello") 不发送电子邮件"
我能做什么?

【问题讨论】:

    标签: ruby-on-rails backgroundworker actionmailer queue-classic


    【解决方案1】:

    看起来 queue_classic 的 enqueue 方法需要两个参数:

    1. 调用方法
    2. 传递给此方法的参数

    因此,您需要编写一个可以使用上述两项调用的方法来传递您的电子邮件。

    module QueueNotifier
      def self.send_welcome_email_to_user(id)
        user = User.find(id)
        Notifier.welcome(user).deliver
      end
    end
    

    然后,为了将这些发送事件排入队列,您需要将方法名称 (QueueNotifier.send_email_to_user) 和参数(david 的用户 ID 或您要发送给的用户)排队。

    QC.enqueue("QueueNotifier.send_welcome_email_to_user", 14)
    

    【讨论】:

    • 您的新方法不起作用,因为您没有将可用于查找用户的参数排入队列。当队列运行“Mailer.say_hello”时,它不知道@david是什么。
    • 你可以在我的 enqueue 方法的最后一行看到我在 id 中为 @david (或任何你想发送给的用户)传递。入队方法 (QueueNotifier.send_welcome_email_to_user) 然后在调用时查找用户,并将其发送给该用户。您需要将 id 作为参数传入。
    • @david 是实例对象,它已被声明,我尝试从 'class Notifier
    • 乐于提供帮助。我担心实例变量的原因是,当您排队执行操作时,您实际上会丢失所有实例变量。这是因为当队列弹出这个作业并运行它时,它在一个不同的会话中,需要设置自己的实例变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 2014-12-31
    • 2011-08-11
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    相关资源
    最近更新 更多