~singpolyma/jmp-pay

ref: e29daf4c9cde4aa8cb4cf3c6fab88704b6851a8b jmp-pay/lib/blather_notify.rb -rw-r--r-- 633 bytes
e29daf4cStephen Paul Weber Do not wait to exit until after we are ready 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

require "blather/client/dsl"
require "timeout"

module BlatherNotify
	extend Blather::DSL

	@ready = Queue.new

	when_ready { @ready << :ready }

	def self.start(jid, password)
		# workqueue_count MUST be 0 or else Blather uses threads!
		setup(jid, password, nil, nil, nil, nil, workqueue_count: 0)

		EM.error_handler { |e| warn e.message }
		@thread = Thread.new do
			EM.run do
				client.run
			end
		end

		Timeout.timeout(30) { @ready.pop }
		at_exit { wait_then_exit }

	end

	def self.wait_then_exit
		disconnected { EM.stop }
		EM.add_timer(30) { EM.stop }
		shutdown
		@thread.join
	end
end