Created
November 10, 2017 08:09
-
-
Save mvasin/2b556d1a20eb126385051c37ce20c370 to your computer and use it in GitHub Desktop.
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
# PubSub with a microservice: send a message to a fleet of workers, | |
# let one of workers pick the task and send the result message directly to the requester. | |
# the manager | |
# pub.rb | |
require 'redis' | |
trap(:INT) { puts 'Bye!'; exit } | |
r = Redis.new | |
loop do | |
input = gets.strip | |
r.rpush 'requests', input | |
r.subscribe input do |on| | |
on.message do |c, m| | |
puts "microservice said: #{m}" | |
r.unsubscribe | |
end | |
end | |
end | |
# the microservice | |
# sub.rb | |
require 'redis' | |
trap(:INT) { puts 'Bye!'; exit } | |
r = Redis.new | |
loop do | |
message = r.blpop('requests', 0).last | |
r.publish message, "your message '#{message}' has been processed" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment