Please note that when you call a mailer method that DEPENDS on a record ID, use this style:
UserMailer.delay_for(X.seconds).the_mailer_method(*args)
instead of this style
UserMailer.the_mailer_method(*args).deliver_later
This is Sidekiq know issue.
Since Sidekiq is super fast at executing jobs, it can't find the record when it try to execute the job because the transaction is not finish yet.
Pseudo code of what actually happens:
# start transaction
# some stuff
# another task, create a record with ID 1
# *call sidekiq using .deliver_later*
# another boring stuffs
# oh shit, another stuff
# end transaction
In above example, Sidekiq straight away executes the job after we call .deliver_later
. Since the transaction block is not finish yet, it will throw error in Sidekiq log says can't find TheModel with id=1
.
I found using delay_for()
solve the problem. 3-5 secons would work.
Transaction = the starting point is the def XXX
and the end point is where you close the def
using end
keyword.
Example:
def do_something # start transaction
# something awesome for the world
end # end of transaction