Afra Afra - 1 year ago 89
Ruby Question

Sidekiq Worker Pattern

I have Sidekiq up and running on a RoR application. All works great and jobs are queued up.

I have seen a lot of patterns as far as queuing up jobs but am curios about this pattern which I see in production and works well. It is idempotent, can work with multiple Sidekiq workers and the

method does the magic....

Any opinions on whether this is a good pattern to use...?

class MailerDeamon
include Sidekiq::Worker

def perform(id)
# idempotent
email = Email::Message.find_by(id: id)
return unless email
return unless email.sent? # or something that marks it has been sent


def self.generate
Email::Message.triggered(60).ids.each do |id|

Thank you

Answer Source

That's a good pattern to use. Something like this:


The only problem that might occur is if your database dies after the email is sent but before it can be marked as sent. In that case, you can get a double delivery but that should be exceedingly rare.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download