Notes/src/services/sync_mutex.ts

23 lines
513 B
TypeScript
Raw Normal View History

/**
* Sync process can make data intermittently inconsistent. Processes which require strong data consistency
* (like consistency checks) can use this mutex to make sure sync isn't currently running.
*/
const Mutex = require('async-mutex').Mutex;
const instance = new Mutex();
2024-02-18 18:11:56 +02:00
async function doExclusively<T>(func: () => T) {
const releaseMutex = await instance.acquire();
try {
2020-07-01 21:33:52 +02:00
return await func();
}
finally {
releaseMutex();
}
}
export default {
doExclusively
2020-06-20 12:31:38 +02:00
};