Skip to content

Instantly share code, notes, and snippets.

@kirillshevch
Last active December 28, 2020 11:29
Show Gist options
  • Save kirillshevch/177c81e8887cb9a32097ed19fef2626f to your computer and use it in GitHub Desktop.
Save kirillshevch/177c81e8887cb9a32097ed19fef2626f to your computer and use it in GitHub Desktop.
class App
attr_reader :queue
def initialize
@queue = []
end
def call
puts "App PID: #{Process.pid}"
publish
subscribe
rescue SignalException => e
puts "Received Signal #{e}"
puts 'Process the remaining messages:'
queue.each { |m| puts m }
exit
end
private
def subscribe
loop do
puts 'Pop: ' + queue.pop.to_s
sleep(2)
end
end
def publish
Thread.new do
loop do
number = rand(0..9)
queue.push(number)
puts 'Queue: ' + queue.to_s
sleep(1)
end
end
end
end
App.new.call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment