Skip to content

Instantly share code, notes, and snippets.

@repomaa
Created January 8, 2018 09:16
Show Gist options
  • Save repomaa/2a671afe0e263b751fb2165d40bb67b8 to your computer and use it in GitHub Desktop.
Save repomaa/2a671afe0e263b751fb2165d40bb67b8 to your computer and use it in GitHub Desktop.
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