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)