Last active
December 22, 2020 07:42
-
-
Save kirillshevch/d87f1a5f1de7bd5d7a794071da3a93e8 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 | |
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 = App.new | |
Signal.trap('TERM') do | |
puts 'Received Signal' | |
puts 'Process the remaining messages:' | |
app.queue.each { |m| puts m } | |
exit | |
end | |
app.call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment