~singpolyma/jmp-pay

ref: 6b4d0a87bf73edfa1fe40868ca915d3152c7ffef jmp-pay/lib/blather_notify.rb -rw-r--r-- 675 bytes
6b4d0a87Stephen Paul Weber Update rubocop 8 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
# 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
		exit 2
	end

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