From b99296beac9c4e18b0d307805c49badb3b9571fd Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 11 May 2021 08:53:55 -0500 Subject: [PATCH] Fix intermittent notification non-delivery We need to wait until all EventMachine items in flight are complete before we allow the process to terminate or some of them might not happen. So when the process wants to exit, wait until the EM thread is done. Use next_tick to make sure our stop command only happens when the EM queue is next clear. --- lib/blather_notify.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/blather_notify.rb b/lib/blather_notify.rb index b1aaa396b58767871d10b34ef8f7b34920428d84..5d801e954f74622eb4c6c477e4eae3d8cc41ab17 100644 --- a/lib/blather_notify.rb +++ b/lib/blather_notify.rb @@ -15,14 +15,22 @@ module BlatherNotify setup(jid, password, nil, nil, nil, nil, workqueue_count: 0) EM.error_handler { |e| warn e.message } - Thread.new do + @thread = Thread.new do EM.run do client.run end end - at_exit { shutdown } + at_exit { wait_then_exit } Timeout.timeout(30) { @ready.pop } end + + def self.wait_then_exit + EM.next_tick do + shutdown + EM.stop + end + @thread.join + end end