Spaces:
Running
Running
File size: 857 Bytes
4dbcbb6 |
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 |
import { collections } from "$lib/server/database";
export async function acquireLock(key = "migrations") {
try {
const insert = await collections.semaphores.insertOne({
key,
createdAt: new Date(),
updatedAt: new Date(),
});
return !!insert.acknowledged; // 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 = "migrations") {
await collections.semaphores.deleteOne({
key,
});
}
export async function isDBLocked(key = "migrations"): Promise<boolean> {
const res = await collections.semaphores.countDocuments({
key,
});
return res > 0;
}
export async function refreshLock(key = "migrations") {
await collections.semaphores.updateOne(
{
key,
},
{
$set: {
updatedAt: new Date(),
},
}
);
}
|