2018-01-05 10:37:36 +08:00
|
|
|
/**
|
|
|
|
* Sync makes 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;
|
2018-01-14 11:51:39 +08:00
|
|
|
const instance = new Mutex();
|
2018-01-05 10:37:36 +08:00
|
|
|
|
2018-01-14 11:51:39 +08:00
|
|
|
async function doExclusively(func) {
|
|
|
|
const releaseMutex = await instance.acquire();
|
|
|
|
|
|
|
|
try {
|
2018-07-25 02:35:03 +08:00
|
|
|
return await func();
|
2018-01-14 11:51:39 +08:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
releaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
doExclusively
|
|
|
|
};
|