0

I have problem with sending recurring mails with Sidekiq and Sidetiq. I'v tried almost everything and I didn't find the solution. I have Sidekiq worker which looks like this:

class InvoiceEmailSender
  include Sidekiq::Worker
  include Sidetiq::Schedulable

  recurrence {minutely(2)}
  def perform(invoice_id, action)
    @invoice = Invoice.find(invoice_id.to_i)
    if action == "invoice"
      send_invoice
    else
      send_reminder
    end
  end

  private
  def send_invoice
    if @invoice.delivery_date == Date.today
      InvoiceMailer.delay.send_invoice(@invoice)
    else
      InvoiceMailer.delay_for(@invoice.delivery_date.to_time).send_invoice(@invoice)
    end
  end

  def send_reminder
   InvoiceMailer.delay.send_invoice_reminder(@invoice) unless @invoice.paid?
  end
end

End in controller I use it in this way:

InvoiceEmailSender.perform_async(@invoice.id, "invoice")

And when I try to send this emails I have the following error in sidekiq console:

2014-08-26T05:36:01.107Z 4664 TID-otcc5idts WARN: {"retry"=>true, "queue"=>"default", "class"=>"InvoiceEmailSender", "args"=>[1409031120.0, 1409031240.0], "jid"=>"06dc732831c24e1a6f78d929", "enqueued_at"=>1409031120.7438812, "error_message"=>"Couldn't find Invoice with 'id'=1409031120", "error_class"=>"ActiveRecord::RecordNotFound", "failed_at"=>1409031249.1003482, "retry_count"=>2, "retried_at"=>1409031361.1066737}
2014-08-26T05:36:01.107Z 4664 TID-otcc5idts WARN: Couldn't find Invoice with 'id'=1409031120
2014-08-26T05:36:01.107Z 4664 TID-otcc5idts WARN: /home/mateusz/.rvm/gems/ruby-2.0.0-p0@rails4/gems/activerecord-4.1.2/lib/active_record/relation/finder_methods.rb:320:in `raise_record_not_found_exception!'

In sideiq web monitor in scheduled tab it looks like this: enter image description here

Please help because I have not idea what is going on...

infused
  • 24,000
  • 13
  • 68
  • 78
Mateusz Urbański
  • 7,352
  • 15
  • 68
  • 133

1 Answers1

2

The data passed in looks like epoch timestamps, turns out Sidetiq passes the last and current times as the 2 parameters to your worker, according to the documentation.

I'm not sure how you go about having custom parameters with a scheduled worker, you'll probably need a different strategy like instead of trying to create more scheduled workers, just have 1 (or two, since it looks like you made this class do 2 jobs) scheduled worker(s) that processes a list of work to do every so often.

Nick Veys
  • 23,458
  • 4
  • 47
  • 64
  • Thank you very much for reply but I still don't know how to solve this problem. Could you give some more tips? – Mateusz Urbański Aug 26 '14 at 06:31
  • 2
    Well if you can't use parameters, you need to have the work these workers need to do in a known place, like your database. They wake up to do their work, query what work needs to be done, do it, and go back to sleep. In your controller, instead of `InvoiceEmailSender.perform_async(@invoice.id, "invoice")`, you could `InvoiceEmailTask.create(@invoice.id)`. It could be even simpler if you just use the existing `Invoice` object, not sure, I don't know your application. Good luck! – Nick Veys Aug 26 '14 at 06:34