2018-01-04 21:37:36 -05:00
|
|
|
/**
|
2023-10-19 00:29:03 +02:00
|
|
|
* Sync process can make data intermittently inconsistent. Processes which require strong data consistency
|
2018-01-04 21:37:36 -05:00
|
|
|
* (like consistency checks) can use this mutex to make sure sync isn't currently running.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const Mutex = require('async-mutex').Mutex;
|
2018-01-13 22:51:39 -05:00
|
|
|
const instance = new Mutex();
|
2018-01-04 21:37:36 -05:00
|
|
|
|
2018-01-13 22:51:39 -05:00
|
|
|
async function doExclusively(func) {
|
|
|
|
const releaseMutex = await instance.acquire();
|
|
|
|
|
|
|
|
try {
|
2020-07-01 21:33:52 +02:00
|
|
|
return await func();
|
2018-01-13 22:51:39 -05:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
releaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
doExclusively
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|