-
Notifications
You must be signed in to change notification settings - Fork 1
ActiveRecord
bnorton edited this page Feb 17, 2013
·
4 revisions
Processing asynchronous messages for ActiveRecord instances presents a problem for most message queues.
MicroQ handles this by setting up a MicroQ::Proxy::Instance with a Custom Loader. This allows you application to process messages on ActiveRecord instances without worry and without serializing the instance into the message payload. The custom Loader instead stores the method and arguments to re-hydrate that instance.
# When a create triggers an external request
class Message < ActiveRecord::Base
after_create -> { async.publish }
def publish
Rails.logger.info("Publishing Message id: #{id}")
api.update(body, :image => image)
end
end
user = account.users.find(params[:id])
user.messages.create!(params.slice(:body, :image))
#=> #<Message id: 123, body: "body", image: "image.png">The after create pushes a message into the queue that looks like:
{:class => 'Message', :method => 'publish', :loader => {:method => 'find', :args => [123]}}
When the message is removed the queue for processing, you'll see something like:
SELECT "messages".* FROM "messages" WHERE "messages"."id" = 123 LIMIT 1
Publishing Message id: 123