~singpolyma/jmp-pay

ref: be1ea4613a86598e7e880b4397671b2ab2f25a63 jmp-pay/lib/blather_notify.rb -rw-r--r-- 633 bytes
be1ea461Stephen Paul Weber If expired with auto top up, NOTIFY for a week before sending expiry notice 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

		at_exit { wait_then_exit }

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

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