@data-fair/lib-node 1.4.0 → 2.0.1

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 CHANGED
@@ -1,5 +1,21 @@
1
1
  import type { Db } from 'mongodb';
2
- export declare const init: (db: Db, ttl?: number) => Promise<void>;
3
- export declare const stop: () => Promise<void>;
4
- export declare function acquire(_id: string, origin?: string): Promise<boolean>;
5
- export declare const release: (_id: string, delay?: number) => Promise<void>;
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,73 @@ 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
- const pid = randomBytes(8).toString('hex')
6
- debug('locks with pid', pid)
7
- let interval
8
- let _db
9
- const collection = () => {
10
- if (!_db) { throw new Error('locks utils was not initialized') }
11
- return _db?.collection('locks')
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
- // prolongate lock acquired by this process while it is still active
24
- interval = setInterval(() => {
25
- locks.updateMany({ pid }, { $currentDate: { updatedAt: true } })
26
- }, (ttl / 2) * 1000)
27
- }
28
- export const stop = async () => {
29
- clearInterval(interval)
30
- if (_db) { await collection().deleteMany({ pid }) }
31
- }
32
- export async function acquire (_id, origin) {
33
- if (!_db) { throw new Error('locks not initialized') }
34
- debug('acquire', _id, origin)
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
+ this._db = db
25
+ await this.collection.createIndex({ pid: 1 })
38
26
  try {
39
- await locks.updateOne({ _id }, { $currentDate: { updatedAt: true } })
27
+ await this.collection.createIndex({ updatedAt: 1 }, { expireAfterSeconds: ttl })
40
28
  } catch (err) {
41
- await locks.deleteOne({ _id, pid })
42
- throw err
29
+ console.log('Failure to create TTL index. Probably because the value changed. Try to update it.')
30
+ db.command({ collMod: 'locks', index: { keyPattern: { updatedAt: 1 }, expireAfterSeconds: ttl } })
43
31
  }
44
- debug('acquire ok', _id)
45
- return true
46
- } catch (err) {
47
- if (err.code !== 11000) { throw err }
48
- // duplicate means the lock was already acquired
49
- debug('acquire ko', _id)
50
- return false
32
+ // prolongate lock acquired by this process while it is still active
33
+ this.interval = setInterval(() => {
34
+ this.collection.updateMany({ pid: this.pid }, { $currentDate: { updatedAt: true } })
35
+ }, (ttl / 2) * 1000)
51
36
  }
52
- }
53
- export const release = async (_id, delay = 0) => {
54
- if (!_db) { throw new Error('locks not initialized') }
55
- debug('release', _id)
56
- const locks = collection()
57
- if (delay) {
58
- const date = new Date((new Date()).getTime() + delay)
59
- await locks.updateOne({ _id, pid }, { $unset: { pid: 1 }, $set: { delayed: true, updatedAt: date } })
60
- } else {
61
- await locks.deleteOne({ _id, pid })
37
+
38
+ stop = async () => {
39
+ clearInterval(this.interval)
40
+ if (this._db) { await this.collection.deleteMany({ pid: this.pid }) }
41
+ }
42
+
43
+ acquire = async (_id, origin) => {
44
+ debug('acquire', _id, origin)
45
+ try {
46
+ await this.collection.insertOne({ _id, pid: this.pid, origin, hostname: hostname(), createdAt: new Date() })
47
+ try {
48
+ await this.collection.updateOne({ _id }, { $currentDate: { updatedAt: true } })
49
+ } catch (err) {
50
+ await this.collection.deleteOne({ _id, pid: this.pid })
51
+ throw err
52
+ }
53
+ debug('acquire ok', _id)
54
+ return true
55
+ } catch (err) {
56
+ if (err.code !== 11000) { throw err }
57
+ // duplicate means the lock was already acquired
58
+ debug('acquire ko', _id)
59
+ return false
60
+ }
61
+ }
62
+
63
+ release = async (_id, delay = 0) => {
64
+ debug('release', _id)
65
+ if (delay) {
66
+ const date = new Date((new Date()).getTime() + delay)
67
+ await this.collection.updateOne({ _id, pid: this.pid }, { $unset: { pid: 1 }, $set: { delayed: true, updatedAt: date } })
68
+ } else {
69
+ await this.collection.deleteOne({ _id, pid: this.pid })
70
+ }
62
71
  }
63
72
  }
73
+ const locks = new Locks()
74
+ export default locks
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-node",
3
- "version": "1.4.0",
3
+ "version": "2.0.1",
4
4
  "description": "Small nodejs related utility functions for the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -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>;
@@ -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) {