@data-fair/lib-node 2.1.4 → 2.2.0
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.d.ts +16 -5
- package/events-queue.js +66 -56
- package/locks.js +1 -0
- package/package.json +1 -1
package/events-queue.d.ts
CHANGED
|
@@ -4,8 +4,19 @@ type EventsQueueOptions = {
|
|
|
4
4
|
eventsUrl: string;
|
|
5
5
|
eventsSecret: string;
|
|
6
6
|
};
|
|
7
|
-
export declare
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
export declare class EventsQueue {
|
|
8
|
+
_options: EventsQueueOptions | undefined;
|
|
9
|
+
eventsQueue: Event[];
|
|
10
|
+
notificationsQueue: Notification[];
|
|
11
|
+
stopped: boolean;
|
|
12
|
+
currentDrainPromise: Promise<void> | undefined;
|
|
13
|
+
options(): EventsQueueOptions;
|
|
14
|
+
start: (options: EventsQueueOptions) => Promise<void>;
|
|
15
|
+
stop: () => Promise<void>;
|
|
16
|
+
loop(): Promise<void>;
|
|
17
|
+
drain(): Promise<void>;
|
|
18
|
+
pushEvent(event: Omit<Event, 'date'>): void;
|
|
19
|
+
pushNotification(notification: Omit<Notification, 'date'>): void;
|
|
20
|
+
}
|
|
21
|
+
declare const eventsQueue: EventsQueue;
|
|
22
|
+
export default eventsQueue;
|
package/events-queue.js
CHANGED
|
@@ -2,64 +2,74 @@ import { internalError } from '@data-fair/lib-node/observer.js'
|
|
|
2
2
|
import axios from './axios.js'
|
|
3
3
|
import Debug from 'debug'
|
|
4
4
|
const debug = Debug('events-queue')
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const loop = async () => {
|
|
15
|
-
// eslint-disable-next-line no-unmodified-loop-condition
|
|
16
|
-
while (!stopped) {
|
|
17
|
-
await new Promise(resolve => setTimeout(resolve, 3000))
|
|
18
|
-
try {
|
|
19
|
-
currentDrainPromise = drain()
|
|
20
|
-
await currentDrainPromise
|
|
21
|
-
currentDrainPromise = undefined
|
|
22
|
-
} catch (err) {
|
|
23
|
-
internalError('events-queue-drain', err)
|
|
24
|
-
}
|
|
5
|
+
export class EventsQueue {
|
|
6
|
+
_options
|
|
7
|
+
eventsQueue = []
|
|
8
|
+
notificationsQueue = []
|
|
9
|
+
stopped = false
|
|
10
|
+
currentDrainPromise
|
|
11
|
+
options () {
|
|
12
|
+
if (!this._options) { throw new Error('events queue was not initialized') }
|
|
13
|
+
return this._options
|
|
25
14
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
15
|
+
|
|
16
|
+
start = async (options) => {
|
|
17
|
+
if (this._options) { throw new Error('events queue was already initialized') }
|
|
18
|
+
this._options = options
|
|
19
|
+
this.loop()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
stop = async () => {
|
|
23
|
+
this.stopped = true
|
|
24
|
+
if (this.currentDrainPromise) { await this.currentDrainPromise }
|
|
25
|
+
await this.drain()
|
|
33
26
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
27
|
+
|
|
28
|
+
async loop () {
|
|
29
|
+
while (!this.stopped) {
|
|
30
|
+
await new Promise(resolve => setTimeout(resolve, 3000))
|
|
31
|
+
try {
|
|
32
|
+
this.currentDrainPromise = this.drain()
|
|
33
|
+
await this.currentDrainPromise
|
|
34
|
+
this.currentDrainPromise = undefined
|
|
35
|
+
} catch (err) {
|
|
36
|
+
internalError('events-queue-drain', err)
|
|
37
|
+
}
|
|
40
38
|
}
|
|
41
39
|
}
|
|
40
|
+
|
|
41
|
+
async drain () {
|
|
42
|
+
if (this.eventsQueue.length) {
|
|
43
|
+
const events = this.eventsQueue
|
|
44
|
+
this.eventsQueue = []
|
|
45
|
+
debug('drain events', events.length)
|
|
46
|
+
await axios.post(this.options().eventsUrl + '/api/events', events, { headers: { 'x-secret-key': this.options().eventsSecret } })
|
|
47
|
+
}
|
|
48
|
+
if (this.notificationsQueue.length) {
|
|
49
|
+
const notifications = this.notificationsQueue
|
|
50
|
+
this.notificationsQueue = []
|
|
51
|
+
debug('drain notifications', notifications.length)
|
|
52
|
+
for (const notification of notifications) {
|
|
53
|
+
await axios.post(this.options().eventsUrl + '/api/notifications', notification, { headers: { 'x-secret-key': this.options().eventsSecret } })
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
pushEvent (event) {
|
|
59
|
+
this.options()
|
|
60
|
+
if (this.stopped) { throw new Error('events queue has been stopped') }
|
|
61
|
+
event.date = new Date().toISOString()
|
|
62
|
+
debug('pushEvent', event)
|
|
63
|
+
this.eventsQueue.push(event)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
pushNotification (notification) {
|
|
67
|
+
this.options()
|
|
68
|
+
if (this.stopped) { throw new Error('notifications queue has been stopped') }
|
|
69
|
+
notification.date = new Date().toISOString()
|
|
70
|
+
debug('pushNotification', notification)
|
|
71
|
+
this.notificationsQueue.push(notification)
|
|
72
|
+
}
|
|
42
73
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
loop()
|
|
46
|
-
}
|
|
47
|
-
export const stop = async () => {
|
|
48
|
-
stopped = true
|
|
49
|
-
if (currentDrainPromise) { await currentDrainPromise }
|
|
50
|
-
await drain()
|
|
51
|
-
}
|
|
52
|
-
export const pushEvent = (event) => {
|
|
53
|
-
options()
|
|
54
|
-
if (stopped) { throw new Error('events queue has been stopped') }
|
|
55
|
-
event.date = new Date().toISOString()
|
|
56
|
-
debug('pushEvent', event)
|
|
57
|
-
eventsQueue.push(event)
|
|
58
|
-
}
|
|
59
|
-
export const pushNotification = (notification) => {
|
|
60
|
-
options()
|
|
61
|
-
if (stopped) { throw new Error('notifications queue has been stopped') }
|
|
62
|
-
notification.date = new Date().toISOString()
|
|
63
|
-
debug('pushNotification', notification)
|
|
64
|
-
notificationsQueue.push(notification)
|
|
65
|
-
}
|
|
74
|
+
const eventsQueue = new EventsQueue()
|
|
75
|
+
export default eventsQueue
|
package/locks.js
CHANGED