-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4906ff2
commit cb3a8b7
Showing
7 changed files
with
264 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const config = require('../env/config'); | ||
|
||
async function getLockCollection(db) { | ||
const { lockCollectionName, lockTtl } = await config.read(); | ||
if (lockTtl <= 0) { | ||
return null; | ||
} | ||
|
||
const lockCollection = db.collection(lockCollectionName); | ||
lockCollection.createIndex({ createdAt: 1 }, { expireAfterSeconds: lockTtl }); | ||
return lockCollection; | ||
} | ||
|
||
async function exist(db) { | ||
const lockCollection = await getLockCollection(db); | ||
if (!lockCollection) { | ||
return false; | ||
} | ||
const foundLocks = await lockCollection.find({}).toArray(); | ||
|
||
return foundLocks.length > 0; | ||
} | ||
|
||
async function activate(db) { | ||
const lockCollection = await getLockCollection(db); | ||
if (lockCollection) { | ||
await lockCollection.insertOne({ createdAt: new Date() }); | ||
} | ||
} | ||
|
||
async function clear(db) { | ||
const lockCollection = await getLockCollection(db); | ||
if (lockCollection) { | ||
await lockCollection.deleteMany({}); | ||
} | ||
} | ||
|
||
module.exports = { | ||
exist, | ||
activate, | ||
clear, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.