@data-fair/lib-node 2.1.3 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/events-queue.js +8 -0
- package/package.json +1 -1
package/events-queue.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { internalError } from '@data-fair/lib-node/observer.js'
|
|
2
2
|
import axios from './axios.js'
|
|
3
|
+
import Debug from 'debug'
|
|
4
|
+
const debug = Debug('events-queue')
|
|
3
5
|
let _options
|
|
4
6
|
const options = () => {
|
|
5
7
|
if (!_options) { throw new Error('events queue was not initialized') }
|
|
@@ -26,11 +28,13 @@ const drain = async () => {
|
|
|
26
28
|
if (eventsQueue.length) {
|
|
27
29
|
const events = eventsQueue
|
|
28
30
|
eventsQueue = []
|
|
31
|
+
debug('drain events', events.length)
|
|
29
32
|
await axios.post(options().eventsUrl + '/api/events', events, { headers: { 'x-secret-key': options().eventsSecret } })
|
|
30
33
|
}
|
|
31
34
|
if (notificationsQueue.length) {
|
|
32
35
|
const notifications = notificationsQueue
|
|
33
36
|
notificationsQueue = []
|
|
37
|
+
debug('drain notifications', notifications.length)
|
|
34
38
|
for (const notification of notifications) {
|
|
35
39
|
await axios.post(options().eventsUrl + '/api/notifications', notification, { headers: { 'x-secret-key': options().eventsSecret } })
|
|
36
40
|
}
|
|
@@ -46,12 +50,16 @@ export const stop = async () => {
|
|
|
46
50
|
await drain()
|
|
47
51
|
}
|
|
48
52
|
export const pushEvent = (event) => {
|
|
53
|
+
options()
|
|
49
54
|
if (stopped) { throw new Error('events queue has been stopped') }
|
|
50
55
|
event.date = new Date().toISOString()
|
|
56
|
+
debug('pushEvent', event)
|
|
51
57
|
eventsQueue.push(event)
|
|
52
58
|
}
|
|
53
59
|
export const pushNotification = (notification) => {
|
|
60
|
+
options()
|
|
54
61
|
if (stopped) { throw new Error('notifications queue has been stopped') }
|
|
55
62
|
notification.date = new Date().toISOString()
|
|
63
|
+
debug('pushNotification', notification)
|
|
56
64
|
notificationsQueue.push(notification)
|
|
57
65
|
}
|