Spaces:
Paused
Paused
Download src/lib/migrations/lock.ts from vollsteksolutions/test: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/spaces/vollsteksolutions/test/resolve/main/src/lib/migrations/lock.ts
- Command line
-
hf download hf://spaces/vollsteksolutions/test/src/lib/migrations/lock.ts
-
curl -L -o lock.ts https://huggingface.co/spaces/vollsteksolutions/test/resolve/main/src/lib/migrations/lock.ts
1.15 kB
| import { collections } from "$lib/server/database"; | |
| import { ObjectId } from "mongodb"; | |
| /** | |
| * Returns the lock id if the lock was acquired, false otherwise | |
| */ | |
| export async function acquireLock(key: string): Promise<ObjectId | false> { | |
| try { | |
| const id = new ObjectId(); | |
| const insert = await collections.semaphores.insertOne({ | |
| _id: id, | |
| key, | |
| createdAt: new Date(), | |
| updatedAt: new Date(), | |
| }); | |
| return insert.acknowledged ? id : false; // true if the document was inserted | |
| } catch (e) { | |
| // unique index violation, so there must already be a lock | |
| return false; | |
| } | |
| } | |
| export async function releaseLock(key: string, lockId: ObjectId) { | |
| await collections.semaphores.deleteOne({ | |
| _id: lockId, | |
| key, | |
| }); | |
| } | |
| export async function isDBLocked(key: string): Promise<boolean> { | |
| const res = await collections.semaphores.countDocuments({ | |
| key, | |
| }); | |
| return res > 0; | |
| } | |
| export async function refreshLock(key: string, lockId: ObjectId): Promise<boolean> { | |
| const result = await collections.semaphores.updateOne( | |
| { | |
| _id: lockId, | |
| key, | |
| }, | |
| { | |
| $set: { | |
| updatedAt: new Date(), | |
| }, | |
| } | |
| ); | |
| return result.matchedCount > 0; | |
| } | |