~singpolyma/jmp-pay

ref: 56432a92df528a2f1b465b2c32a8810ca67ade01 jmp-pay/lib/blather_notify.rb -rw-r--r-- 695 bytes
56432a92Stephen Paul Weber Show backtrace and bail out hard on panic 6 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
36
37
38
39
40
41
# 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(&method(:panic))

		@thread = Thread.new {
			EM.run do
				client.run
			end
		}

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

	def self.panic(e)
		warn e.message
		warn e.backtrace
		exit! 2
	end

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