Skip to content

Instantly share code, notes, and snippets.

@kirillshevch
Last active December 22, 2020 07:42
Show Gist options
  • Save kirillshevch/d87f1a5f1de7bd5d7a794071da3a93e8 to your computer and use it in GitHub Desktop.
Save kirillshevch/d87f1a5f1de7bd5d7a794071da3a93e8 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
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