Afra Afra - 3 months ago 18
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

generate
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

email.begin_transmission!
end

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


Thank you

Answer

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

email.begin_transmission!
email.mark_as_sent!

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.

Comments