Skip to content

Instantly share code, notes, and snippets.

@harsh183
Last active July 28, 2020 10:54
Show Gist options
  • Save harsh183/87ee406fd88c753ddc18a1eba0f7791e to your computer and use it in GitHub Desktop.
Save harsh183/87ee406fd88c753ddc18a1eba0f7791e to your computer and use it in GitHub Desktop.
Single chain rabbitMQ for distributed ruby task (here squaring and then cubing the number) - bit of copy paste but you get it
FROM rabbitmq:3.7.7-alpine
run rabbitmq-plugins enable --offline rabbitmq_web_stomp
run \
echo 'loopback_users.guest = false' >> /etc/rabbitmq/rabbitmq.conf && \
echo 'web_stomp.ws_frame = binary' >> /etc/rabbitmq/rabbitmq.conf
EXPOSE 15674
# This file sends the first push out to the rabbitMQ (Message Queue) that we have set up
require 'bundler/setup'
require 'bunny'
require 'json'
# Relying on default ports
# Refer to bash file start-rabbit.sh
# Bunny is our Ruby RabbitMQ adapter
amqp_conn = Bunny.new
amqp_conn.start
# Channel to be only in one thread, and one per thread
channel = amqp_conn.create_channel
# Publish into the queue
queue1 = channel.queue('queue1')
# Right now we fire it 50 times, but try out as much as you like to demo the load testing
50.times do |n|
queue1.publish(n.to_s, routing_key: queue1.name)
end
#!/usr/bin/env bash
NAME=rabbitmq
docker rm -f $NAME
docker run \
-d \
--hostname $NAME \
--name $NAME \
-p 5672:5672 \
-p 15671:15671 \
-p 15672:15672 \
-p 15674:15674 \
-p 15670:15670 \
-p 61613:61613 \
rabbit
# This file listens to the first queue, performs an operation on it and writes onto another queue
require 'bundler/setup'
require 'bunny'
require 'json'
def square(x)
x*x
end
# Relying on default ports
# Refer to bash file start-rabbit.sh
# Connection is the AMQP connection (wrapper)
amqp_conn = Bunny.new
amqp_conn.start
# Within one connection there can be many communications via channels
# Channel is not threadsafe, don't share - common mistake
channel = amqp_conn.create_channel
# Subscribe to our queue
queue1 = channel.queue('queue1')
queue2 = channel.queue('queue2')
# Perform some action by reading from one queue and writing to another
# Every queue does load balancing pretty well out of the box
queue1.subscribe(block: true) do |_delivery_info, _metadata, payload|
number = payload.to_f
result = square number
p "#{number} becomes #{result}"
queue2.publish(result.to_s, routing_key: queue2.name)
end
# Likewise, this takes in from the second queue and writes to the third.
# Now the web browser can listen onto the 3rd one, or it can go into
# other processes, either way it's pretty modular.
require 'bundler/setup'
require 'bunny'
require 'json'
def cube(x)
x*x*x
end
# Relying on default ports
# Refer to bash file start-rabbit.sh
# Connection is the AMQP connection (wrapper)
amqp_conn = Bunny.new
amqp_conn.start
# Within one connection there can be many communications via channels
# Channel is not threadsafe, don't share - common mistake
channel = amqp_conn.create_channel
# Subscribe to our queue
queue2 = channel.queue('queue2')
queue3 = channel.queue('queue3')
# Perform some action by reading from one queue and writing to another
queue2.subscribe(block: true) do |_delivery_info, _metadata, payload|
number = payload.to_f
result = cube number
p "#{number} becomes #{result}"
queue3.publish(result.to_s, routing_key: queue3.name)
end
@harsh183
Copy link
Author

docker stop $(docker ps -a -q) 
docker rm $(docker ps -a -q)

To take care of docker containers that are lingering around.

@harsh183
Copy link
Author

Also OMG it took me 3 months to figure this out but I was missing

docker build -t rabbit .

Note the name and dot.

@harsh183
Copy link
Author

MIT License

Copyright 2020 Harsh Deep

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment