我应该如何使用sidekiq工作人员



我有一个应用程序,可以在其中创建发票,将其呈现为pdf并发送给客户。我的邮件中有两个动作:

class InvoiceMailer < ActionMailer::Base
  default from: "from@example.com"
  def send_invoice_reminder(invoice)
    @invoice = invoice
    attach_invoice
    mail :subject => "Invoice reminder", :to => invoice.customer.email
  end
  def send_invoice(invoice)
    @invoice = invoice
    attach_invoice
    mail :subject => "Your Invoice", :to => invoice.customer.email
  end
  protected
  def attach_invoice
    attachments["invoice.pdf"] = WickedPdf.new.pdf_from_string(
      render_to_string(:pdf => "invoice",:template => 'admin/invoices/show.pdf.erb')
    )
  end
end

现在我想通过Sidkiq的工人发送这个。但我有疑问。我是否需要两个sidekiq工人:

  • 一个发送发票电子邮件

  • 第二个发送提醒

或者一个工人就足够了?

我认为你可以用一个工人来完成它,因为的这两项任务基本上都有相同的工作

它看起来像:

class InvoiceMailer < ActionMailer::Base
  default from: "from@example.com"
  def send_invoice(invoice, subject)
    @invoice = invoice
    attachments["invoice.pdf"] = pdf
    mail subject: subject, to: invoice.customer.email
  end
  private
  def pdf
    WickedPdf.new.pdf_from_string render_to_string(
        pdf: "invoice",
        template: 'admin/invoices/show.pdf.erb'
    )
  end
end
class InvoceEmailSender
  include Sidekiq::Worker
  def perform(invoice, subject)
    InvoiceMailer.send_invoice(invoice, subject).deliver
  end
end
InvoiceEmailSender.perform_async invoice, 'Your Invoice'
InvoiceEmailSender.perform_async invoice, 'Invoice Reminder'

最新更新