Skip to content

Instantly share code, notes, and snippets.

@zulhfreelancer
Last active December 19, 2016 04:48
Show Gist options
  • Save zulhfreelancer/80dc8557987c6bcb19c69b223210703d to your computer and use it in GitHub Desktop.
Save zulhfreelancer/80dc8557987c6bcb19c69b223210703d to your computer and use it in GitHub Desktop.
"Can't find ModelName with ID=12345" errors with Sidekiq - How To Solve?

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment