@data-fair/lib-node 1.3.1 → 2.0.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 +2 -0
- package/events-queue.js +21 -7
- package/locks.d.ts +20 -4
- package/locks.js +62 -52
- package/package.json +2 -2
- package/upgrade-scripts.d.ts +2 -1
- package/upgrade-scripts.js +3 -4
package/events-queue.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Event } from '@data-fair/lib-common-types/event/index.js';
|
|
2
|
+
import { type Notification } from '@data-fair/lib-common-types/notification/index.js';
|
|
2
3
|
type EventsQueueOptions = {
|
|
3
4
|
eventsUrl: string;
|
|
4
5
|
eventsSecret: string;
|
|
@@ -6,4 +7,5 @@ type EventsQueueOptions = {
|
|
|
6
7
|
export declare const start: (options: EventsQueueOptions) => Promise<void>;
|
|
7
8
|
export declare const stop: () => Promise<void>;
|
|
8
9
|
export declare const pushEvent: (event: Omit<Event, "date">) => void;
|
|
10
|
+
export declare const pushNotification: (notification: Omit<Notification, "date">) => void;
|
|
9
11
|
export {};
|
package/events-queue.js
CHANGED
|
@@ -5,7 +5,8 @@ const options = () => {
|
|
|
5
5
|
if (!_options) { throw new Error('events queue was not initialized') }
|
|
6
6
|
return _options
|
|
7
7
|
}
|
|
8
|
-
let
|
|
8
|
+
let eventsQueue = []
|
|
9
|
+
let notificationsQueue = []
|
|
9
10
|
let stopped = false
|
|
10
11
|
let currentDrainPromise
|
|
11
12
|
const loop = async () => {
|
|
@@ -22,10 +23,18 @@ const loop = async () => {
|
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
25
|
const drain = async () => {
|
|
25
|
-
if (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
if (eventsQueue.length) {
|
|
27
|
+
const events = eventsQueue
|
|
28
|
+
eventsQueue = []
|
|
29
|
+
await axios.post(options().eventsUrl + '/api/events', events, { headers: { 'x-secret-key': options().eventsSecret } })
|
|
30
|
+
}
|
|
31
|
+
if (notificationsQueue.length) {
|
|
32
|
+
const notifications = notificationsQueue
|
|
33
|
+
notificationsQueue = []
|
|
34
|
+
for (const notification of notifications) {
|
|
35
|
+
await axios.post(options().eventsUrl + '/api/notifications', notification, { headers: { 'x-secret-key': options().eventsSecret } })
|
|
36
|
+
}
|
|
37
|
+
}
|
|
29
38
|
}
|
|
30
39
|
export const start = async (options) => {
|
|
31
40
|
_options = options
|
|
@@ -37,7 +46,12 @@ export const stop = async () => {
|
|
|
37
46
|
await drain()
|
|
38
47
|
}
|
|
39
48
|
export const pushEvent = (event) => {
|
|
40
|
-
if (stopped) { throw new Error('events queue
|
|
41
|
-
queue.push(event)
|
|
49
|
+
if (stopped) { throw new Error('events queue has been stopped') }
|
|
42
50
|
event.date = new Date().toISOString()
|
|
51
|
+
eventsQueue.push(event)
|
|
52
|
+
}
|
|
53
|
+
export const pushNotification = (notification) => {
|
|
54
|
+
if (stopped) { throw new Error('notifications queue has been stopped') }
|
|
55
|
+
notification.date = new Date().toISOString()
|
|
56
|
+
notificationsQueue.push(notification)
|
|
43
57
|
}
|
package/locks.d.ts
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
import type { Db } from 'mongodb';
|
|
2
|
-
export declare
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
export declare class Locks {
|
|
3
|
+
pid: string;
|
|
4
|
+
private interval;
|
|
5
|
+
private _db;
|
|
6
|
+
get db(): Db;
|
|
7
|
+
get collection(): import("mongodb").Collection<{
|
|
8
|
+
_id: string;
|
|
9
|
+
pid: string;
|
|
10
|
+
origin?: string;
|
|
11
|
+
hostname: string;
|
|
12
|
+
createdAt: Date;
|
|
13
|
+
}>;
|
|
14
|
+
constructor();
|
|
15
|
+
start: (db: Db, ttl?: number) => Promise<void>;
|
|
16
|
+
stop: () => Promise<void>;
|
|
17
|
+
acquire: (_id: string, origin?: string) => Promise<boolean>;
|
|
18
|
+
release: (_id: string, delay?: number) => Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
declare const locks: Locks;
|
|
21
|
+
export default locks;
|
package/locks.js
CHANGED
|
@@ -2,62 +2,72 @@ import { hostname } from 'node:os'
|
|
|
2
2
|
import { randomBytes } from 'node:crypto'
|
|
3
3
|
import Debug from 'debug'
|
|
4
4
|
const debug = Debug('locks')
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
export const init = async (db, ttl = 60) => {
|
|
14
|
-
_db = db
|
|
15
|
-
const locks = collection()
|
|
16
|
-
await locks.createIndex({ pid: 1 })
|
|
17
|
-
try {
|
|
18
|
-
await locks.createIndex({ updatedAt: 1 }, { expireAfterSeconds: ttl })
|
|
19
|
-
} catch (err) {
|
|
20
|
-
console.log('Failure to create TTL index. Probably because the value changed. Try to update it.')
|
|
21
|
-
db.command({ collMod: 'locks', index: { keyPattern: { updatedAt: 1 }, expireAfterSeconds: ttl } })
|
|
5
|
+
export class Locks {
|
|
6
|
+
pid
|
|
7
|
+
interval
|
|
8
|
+
_db
|
|
9
|
+
get db () {
|
|
10
|
+
if (!this._db) { throw new Error('locks utils was not initialized') }
|
|
11
|
+
return this._db
|
|
22
12
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const locks = collection()
|
|
36
|
-
try {
|
|
37
|
-
await locks.insertOne({ _id, pid, origin, hostname: hostname(), createdAt: new Date() })
|
|
13
|
+
|
|
14
|
+
get collection () {
|
|
15
|
+
return this.db.collection('locks')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
constructor () {
|
|
19
|
+
this.pid = randomBytes(8).toString('hex')
|
|
20
|
+
debug('locks with pid', this.pid)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
start = async (db, ttl = 60) => {
|
|
24
|
+
await this.collection.createIndex({ pid: 1 })
|
|
38
25
|
try {
|
|
39
|
-
await
|
|
26
|
+
await this.collection.createIndex({ updatedAt: 1 }, { expireAfterSeconds: ttl })
|
|
40
27
|
} catch (err) {
|
|
41
|
-
|
|
42
|
-
|
|
28
|
+
console.log('Failure to create TTL index. Probably because the value changed. Try to update it.')
|
|
29
|
+
db.command({ collMod: 'locks', index: { keyPattern: { updatedAt: 1 }, expireAfterSeconds: ttl } })
|
|
43
30
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
// duplicate means the lock was already acquired
|
|
49
|
-
debug('acquire ko', _id)
|
|
50
|
-
return false
|
|
31
|
+
// prolongate lock acquired by this process while it is still active
|
|
32
|
+
this.interval = setInterval(() => {
|
|
33
|
+
this.collection.updateMany({ pid: this.pid }, { $currentDate: { updatedAt: true } })
|
|
34
|
+
}, (ttl / 2) * 1000)
|
|
51
35
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
36
|
+
|
|
37
|
+
stop = async () => {
|
|
38
|
+
clearInterval(this.interval)
|
|
39
|
+
if (this._db) { await this.collection.deleteMany({ pid: this.pid }) }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
acquire = async (_id, origin) => {
|
|
43
|
+
debug('acquire', _id, origin)
|
|
44
|
+
try {
|
|
45
|
+
await this.collection.insertOne({ _id, pid: this.pid, origin, hostname: hostname(), createdAt: new Date() })
|
|
46
|
+
try {
|
|
47
|
+
await this.collection.updateOne({ _id }, { $currentDate: { updatedAt: true } })
|
|
48
|
+
} catch (err) {
|
|
49
|
+
await this.collection.deleteOne({ _id, pid: this.pid })
|
|
50
|
+
throw err
|
|
51
|
+
}
|
|
52
|
+
debug('acquire ok', _id)
|
|
53
|
+
return true
|
|
54
|
+
} catch (err) {
|
|
55
|
+
if (err.code !== 11000) { throw err }
|
|
56
|
+
// duplicate means the lock was already acquired
|
|
57
|
+
debug('acquire ko', _id)
|
|
58
|
+
return false
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
release = async (_id, delay = 0) => {
|
|
63
|
+
debug('release', _id)
|
|
64
|
+
if (delay) {
|
|
65
|
+
const date = new Date((new Date()).getTime() + delay)
|
|
66
|
+
await this.collection.updateOne({ _id, pid: this.pid }, { $unset: { pid: 1 }, $set: { delayed: true, updatedAt: date } })
|
|
67
|
+
} else {
|
|
68
|
+
await this.collection.deleteOne({ _id, pid: this.pid })
|
|
69
|
+
}
|
|
62
70
|
}
|
|
63
71
|
}
|
|
72
|
+
const locks = new Locks()
|
|
73
|
+
export default locks
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@data-fair/lib-node",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Small nodejs related utility functions for the data-fair stack.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"build": "cd .. && npm run build"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@data-fair/lib-common-types": "^1.
|
|
17
|
+
"@data-fair/lib-common-types": "^1.5.0",
|
|
18
18
|
"@data-fair/lib-utils": "^1.1.0",
|
|
19
19
|
"agentkeepalive": "^4.5.0",
|
|
20
20
|
"ajv-formats": "^3.0.1",
|
package/upgrade-scripts.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { Db } from 'mongodb';
|
|
2
2
|
import type { Debugger } from 'debug';
|
|
3
|
+
import type { Locks } from './locks.js';
|
|
3
4
|
export interface UpgradeScript {
|
|
4
5
|
description: string;
|
|
5
6
|
exec: (db: Db, debug: Debugger) => Promise<void>;
|
|
6
7
|
}
|
|
7
|
-
export default function (db: Db, basePath?: string): Promise<void>;
|
|
8
|
+
export default function (db: Db, locks: Locks, basePath?: string): Promise<void>;
|
package/upgrade-scripts.js
CHANGED
|
@@ -2,11 +2,10 @@ import semver from 'semver'
|
|
|
2
2
|
import path from 'path'
|
|
3
3
|
import { readdirSync, readFileSync } from 'fs'
|
|
4
4
|
import Debug from 'debug'
|
|
5
|
-
import { acquire, release } from './locks.js'
|
|
6
5
|
const debug = Debug('upgrade')
|
|
7
6
|
// chose the proper scripts to execute, then run them
|
|
8
|
-
export default async function (db, basePath = './') {
|
|
9
|
-
const ack = await acquire('upgrade')
|
|
7
|
+
export default async function (db, locks, basePath = './') {
|
|
8
|
+
const ack = await locks.acquire('upgrade')
|
|
10
9
|
if (!ack) {
|
|
11
10
|
console.warn('upgrade scripts lock is already acquired, skip them')
|
|
12
11
|
// IMPORTANT: this behaviour of running the process when the upgrade scripts are still running on another one
|
|
@@ -16,7 +15,7 @@ export default async function (db, basePath = './') {
|
|
|
16
15
|
// are not considered "up" and the previous versions keep running in the mean time
|
|
17
16
|
} else {
|
|
18
17
|
await runScripts(db, basePath)
|
|
19
|
-
await release('upgrade')
|
|
18
|
+
await locks.release('upgrade')
|
|
20
19
|
}
|
|
21
20
|
}
|
|
22
21
|
async function runScripts (db, basePath) {
|