Search code examples
ruby-on-railssidekiq

Do delayed Sidekiq Mailer methods execute when the job is processed?


I have a typical ActionMailer with a method specifying an email delivery.

def some_email(user_id)
@user = User.find(user_id)

  if @user.eligible_for_email?
    mail(to: @user.email, from: "[email protected]", subject: "The Subject")
    @user.email_sent = Date.today
    @user.save
  end
end

I want to delay the sending of this using Sidekiq so I use:

Mailer.delay_for(2.days).some_email(user.id)

The eligible_for_email method:

def eligible_for_email?
    !unsubscribed? && email_sent.nil?
end

In the meantime, the user could have unsubscribed, which is why there is a method in the User model called eligible_for_email? which I can use to conditionally send the email - but obviously this condition needs to be tested just before the email is sent, not when the job is scheduled.

So the problem is that when I use Sidekiq to process this, the conditional logic doesn't seem to be run when the job is done.

Does Sidekiq work by executing the some_email method on runtime and then queuing the resulting email to be sent out two days later, thereby negating my conditional code?


Solution

  • Your understanding is 100% correct and that's exactly what you want to do.

    I'd guess you aren't restarting Sidekiq to pick up your code changes. Sidekiq does not auto-reload changed code like Rails does.