user2490003
user2490003

Reputation: 11920

Using `delay` in Sidekiq with dynamic class/method names

Let's say I have some internal business logic that determines what mailer needs to be sent.

The output of this business logic is a Hash in the following format -

{ mailer_class: SomeMailer, mailer_method: "foo_email" }
{ mailer_class: OtherMailer, mailer_method: "bar_email" }
# etc...

I need to call the appropriate mailer based on the info above so I try something like this with Sidekiq's built in delay -

data = { mailer_class: ..., mailer_method: "..." } 
data[:mailer_class].delay.send(mailer[:method])

This results in Sidekiq queueing up the send method which will eventually be called on my mailer.

Functionally it might work correctly, because that class after all will receive the appropriate method. But it feels a bit dirty and it interrupts other processes that watch the sidekiq queue because they expect to see a mailer method name but find :send instead.

Is there a good way around this or am I stuck modifying the rest of my application logic to work with this?

Thanks!

Upvotes: 0

Views: 206

Answers (1)

Mike Perham
Mike Perham

Reputation: 22228

Why not pass that Hash to a Sidekiq Worker which knows how to send emails with that class/method combo?

def perform(hash)
  hash['mailer_class'].constantize.send(hash['mailer_method'])
end

Upvotes: 1

Related Questions