Last active
July 28, 2020 10:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.