File size: 1,389 Bytes
fc69895
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { collections } from "$lib/server/database";
import { ObjectId } from "mongodb";
import type { Semaphores } from "$lib/types/Semaphore";

/**
 * Returns the lock id if the lock was acquired, false otherwise
 */
export async function acquireLock(key: Semaphores | string): Promise<ObjectId | false> {
	try {
		const id = new ObjectId();

		const insert = await collections.semaphores.insertOne({
			_id: id,
			key,
			createdAt: new Date(),
			updatedAt: new Date(),
			deleteAt: new Date(Date.now() + 1000 * 60 * 3), // 3 minutes
		});

		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: Semaphores | string, lockId: ObjectId) {
	await collections.semaphores.deleteOne({
		_id: lockId,
		key,
	});
}

export async function isDBLocked(key: Semaphores | string): Promise<boolean> {
	const res = await collections.semaphores.countDocuments({
		key,
	});
	return res > 0;
}

export async function refreshLock(key: Semaphores | string, lockId: ObjectId): Promise<boolean> {
	const result = await collections.semaphores.updateOne(
		{
			_id: lockId,
			key,
		},
		{
			$set: {
				updatedAt: new Date(),
				deleteAt: new Date(Date.now() + 1000 * 60 * 3), // 3 minutes
			},
		}
	);

	return result.matchedCount > 0;
}