-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
68 lines (57 loc) · 1.71 KB
/
index.mjs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// import tx2 from 'tx2'
// const counter = tx2.counter({
// name: 'Historic Requests'
// })
import {send, currentCalls} from './send.mjs'
import {redis, channel as redisChannel} from './redis.mjs'
import {apiserver} from './apiserver.mjs'
import debug from 'debug'
const log = debug('xummproxy')
process.on('unhandledRejection', e => {
console.log('An unhandledRejection occurred', e)
})
apiserver.start()
let processing = true
process.on('SIGINT', function () {
apiserver.close()
if (processing) {
log('Graceful Stop, unsubscribing from Redis and cleaning up...')
// redis.unsubscribe(redisChannel)
setInterval(function () {
log('Waiting for current calls to finish...', currentCalls)
if (currentCalls < 1) {
log('Ended. Shutting down in 3 sec.')
setTimeout(function () {
process.exit(0)
}, 3000)
}
}, 3000)
}
processing = false
return false
})
while (processing) {
log('BLPOP')
const [channel, message] = await redis.blpop(redisChannel, 0)
log('REDIS message received', { channel, message })
if (redisChannel === channel) {
try {
const data = JSON.parse(message)
if (typeof data.url === 'string' && typeof data.payload === 'string') {
if (typeof data.data === 'object' && data) {
log('Valid REDIS message, process...', data)
// if (processing) {
// counter.inc()
// }
send(data.url, data.data, data.payload, data?.secret || '')
} else {
log('Invalid message, missing data object')
}
} else {
log('Invalid message, missing url/payload')
}
} catch (e) {
log('Error decoding REDIS message', e.message)
}
}
}