Skip to content

Instantly share code, notes, and snippets.

@eduardopoleo
Last active February 16, 2018 13:57
Show Gist options
  • Save eduardopoleo/59d1796b4b1fe9b18e322614e8cb361e to your computer and use it in GitHub Desktop.
Save eduardopoleo/59d1796b4b1fe9b18e322614e8cb361e to your computer and use it in GitHub Desktop.
def produce_all(connection)
count = Outbox::Adapters::General::Messages.count
batches = (count / batch_size.to_f).ceil
batches.times do
messages = Outbox::Adapters::General.order(:id).limit(batch_size).to_a
grouped_messages = group_by_model_type(messages)
while messages_to_process?(grouped_messages)
sent_messages = send_messages!(grouped_messages, connection)
confirm_published!(sent_messages, connection) if sent_messages
remove_empty_lists(grouped_messages)
end
end
end
def messages_to_process?(messages)
messages.size != 0
end
def send_messages!(grouped_messages, connection)
grouped_messages.each_with_object([]) do |messages, sent_messages|
message = messages.delete_at(0)
publish(connection, message)
sent_messages << message
end
end
def remove_empty_lists(messages)
grouped_messages.reject! { |m| m.size == 0 }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment