Created
January 8, 2018 09:16
-
-
Save repomaa/2a671afe0e263b751fb2165d40bb67b8 to your computer and use it in GitHub Desktop.
This file contains 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
require 'celluloid/current' | |
class Receiver | |
include Celluloid | |
include Celluloid::Notifications | |
def initialize(blocking = true) | |
@queue = Queue.new | |
subscribe('deadlock', :got_notified) | |
blocking ? async.work_blocking : async.work_non_blocking | |
end | |
def work_non_blocking | |
puts 'checking queue' | |
puts @queue.pop(true) | |
rescue ThreadError | |
puts 'nothing in queue, retrying' | |
sleep 1 | |
retry | |
end | |
def work_blocking | |
puts 'checking queue' | |
puts @queue.pop | |
end | |
def got_notified(_topic, payload) | |
puts 'got notified' | |
@queue << payload | |
end | |
end | |
class Sender | |
include Celluloid | |
include Celluloid::Notifications | |
def initialize | |
async.work | |
end | |
def work | |
sleep 2 | |
publish('deadlock', 'foo') | |
end | |
end | |
Receiver.new(false) | |
Sender.new | |
sleep |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment