@data-fair/lib-node 2.17.0 → 2.17.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.js +4 -1
- package/locks.js.map +1 -1
- package/package.json +1 -1
package/locks.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { hostname } from 'node:os';
|
|
2
2
|
import { randomBytes } from 'node:crypto';
|
|
3
3
|
import Debug from 'debug';
|
|
4
|
+
import { internalError } from './observer.js';
|
|
4
5
|
const debug = Debug('locks');
|
|
5
6
|
export class Locks {
|
|
6
7
|
pid;
|
|
@@ -31,8 +32,10 @@ export class Locks {
|
|
|
31
32
|
db.command({ collMod: 'locks', index: { keyPattern: { updatedAt: 1 }, expireAfterSeconds: ttl } });
|
|
32
33
|
}
|
|
33
34
|
// prolongate lock acquired by this process while it is still active
|
|
35
|
+
// a mongo error here must not become an unhandled rejection (it would crash the process on any failover)
|
|
34
36
|
this.interval = setInterval(() => {
|
|
35
|
-
this.collection.updateMany({ pid: this.pid }, { $currentDate: { updatedAt: true } })
|
|
37
|
+
this.collection.updateMany({ pid: this.pid }, { $currentDate: { updatedAt: true } })
|
|
38
|
+
.catch(err => internalError('locks-heartbeat', err));
|
|
36
39
|
}, (ttl / 2) * 1000);
|
|
37
40
|
};
|
|
38
41
|
stop = async () => {
|
package/locks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"locks.js","sourceRoot":"","sources":["locks.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,KAAK,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"locks.js","sourceRoot":"","sources":["locks.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,a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AI,KAAK,EAAE,CAAA;AAEzB,eAAe,KAAK,CAAA","sourcesContent":["import type { Db } from 'mongodb'\nimport { hostname } from 'node:os'\nimport { randomBytes } from 'node:crypto'\nimport Debug from 'debug'\nimport { internalError } from './observer.js'\n\nconst debug = Debug('locks')\n\nexport class Locks {\n pid: string\n private interval: ReturnType<typeof setInterval> | undefined\n private _db: Db | undefined\n get db () {\n if (!this._db) throw new Error('locks utils was not initialized')\n return this._db\n }\n\n get collection () {\n return this.db.collection<{ _id: string, pid: string, origin?: string, hostname: string, createdAt: Date }>('locks')\n }\n\n constructor () {\n this.pid = randomBytes(8).toString('hex')\n debug('locks with pid', this.pid)\n }\n\n start = async (db: Db, ttl = 60) => {\n if (this._db) throw new Error('locks utils was already initialized')\n this._db = db\n await this.collection.createIndex({ pid: 1 })\n try {\n await this.collection.createIndex({ updatedAt: 1 }, { expireAfterSeconds: ttl })\n } catch (err) {\n console.log('Failure to create TTL index. Probably because the value changed. Try to update it.')\n db.command({ collMod: 'locks', index: { keyPattern: { updatedAt: 1 }, expireAfterSeconds: ttl } })\n }\n\n // prolongate lock acquired by this process while it is still active\n // a mongo error here must not become an unhandled rejection (it would crash the process on any failover)\n this.interval = setInterval(() => {\n this.collection.updateMany({ pid: this.pid }, { $currentDate: { updatedAt: true } })\n .catch(err => internalError('locks-heartbeat', err))\n }, (ttl / 2) * 1000)\n }\n\n stop = async () => {\n clearInterval(this.interval)\n if (this._db) await this.collection.deleteMany({ pid: this.pid })\n }\n\n acquire = async (_id: string, origin?: string): Promise<boolean> => {\n debug('acquire', _id, origin)\n try {\n await this.collection.insertOne({ _id, pid: this.pid, origin, hostname: hostname(), createdAt: new Date() })\n try {\n await this.collection.updateOne({ _id }, { $currentDate: { updatedAt: true } })\n } catch (err) {\n await this.collection.deleteOne({ _id, pid: this.pid })\n throw err\n }\n debug('acquire ok', _id)\n return true\n } catch (err: any) {\n if (err.code !== 11000) throw err\n // duplicate means the lock was already acquired\n debug('acquire ko', _id)\n return false\n }\n }\n\n release = async (_id: string, delay = 0) => {\n debug('release', _id)\n if (delay) {\n const date = new Date((new Date()).getTime() + delay)\n await this.collection.updateOne({ _id, pid: this.pid }, { $unset: { pid: 1 }, $set: { delayed: true, updatedAt: date } })\n } else {\n await this.collection.deleteOne({ _id, pid: this.pid })\n }\n }\n}\n\nconst locks = new Locks()\n\nexport default locks\n"]}
|