Skip to content

Instantly share code, notes, and snippets.

@mvasin
Created November 10, 2017 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvasin/2b556d1a20eb126385051c37ce20c370 to your computer and use it in GitHub Desktop.
Save mvasin/2b556d1a20eb126385051c37ce20c370 to your computer and use it in GitHub Desktop.
# 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