@data-fair/lib-node 1.4.0 → 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/locks.d.ts +20 -4
- package/locks.js +62 -52
- package/package.json +1 -1
- package/upgrade-scripts.d.ts +2 -1
- package/upgrade-scripts.js +3 -4
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
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) {
|