Sidekiq not finding id even after transaction

Viewed 32

In the code example below, when the publish (:message_created) is received by the subscriber (a sidekiq worker) the first time it is run it cannot find the id.

class ChatRoom < ApplicationRecord
  #... 
  def add_message(args)
    #...

    message = self.messages.create!(
      #...
    )

    publish(:message_created,
      message_id: message.id,
      notify: notify
    )
  end
end


class Messages::Notify < ApplicationSubscriber
  include Wisper::Publisher
  def message_created(args)
    internal_note = InternalNote.find(args[:message_id])
    #...
  end
end

I'm aware that this is noted in the docs (Sidekiq wiki)

The subscriber receives the id and on the re-run of the job it works. When forcing the create! to be surrounded by a transaction block it still has the same issue:

class Message < ApplicationRecord
  def create!(args)
    self.transaction do
      super(args)
    end
  end
end

We are unable to move the publish to the after_commit inside the message class as we require some additional info inside the ChatRoom class

(I know it works eventually :) but our error logging is spammed so I would like to understand and solve this issue)

1 Answers

As per the suggestion in the comments, the answer was that the item calling add_message was wrapped in a transaction

As per the rails docs:

transaction calls can be nested. By default, this makes all database statements in the nested transaction block become part of the parent transaction

So the publish would fire off but the parent transaction was still locked.

Related