Last active
December 28, 2020 11:29
-
-
Save kirillshevch/177c81e8887cb9a32097ed19fef2626f 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
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