Chunked event publishing - design overview
#enqueue_event(payload, target queue name): stores messages to be sent in a simple array
#publish()send everything currently enqueued to external queue system (here: RabbitMQ); if the
publishfails for any reason, messages are kept in memory for later retry (along with grumbling in logs, etc).
#clear_events_to_publish!is useful in rollback scenarios
.thread_singleton_instanceaccessor with lazy initialization, like
ActiveRecord::Base.connection. This allows for easy setup, and persistent state across requests (should flushing fail we can retry on next request on the same thread). The
thread_singleton_instancealso easy replacement with a simple test implementation, think
ActionMailer. Sending actual messages in test is best restricted to a few integration test scenarios.