我该如何使用sidekiq工人?

时间:2014-08-25 09:27:44

标签: ruby-on-rails 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工人:

  • 一个发送发票电子邮件

  • 第二次发送提醒

或者也许一个工人就足够了?

1 个答案:

答案 0 :(得分:2)

我认为你可以使用一名工作人员,因为这两项任务的工作大致相同

它看起来像:

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'