~singpolyma/jmp-pay

ref: b99296beac9c4e18b0d307805c49badb3b9571fd jmp-pay/lib/blather_notify.rb -rw-r--r-- 611 bytes
b99296beStephen Paul Weber Fix intermittent notification non-delivery 1 year, 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
36
# 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

		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