Comparing version 2.1.0 to 2.2.0
{ | ||
"name": "jembadb", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Json Embeddable Appendable Database", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -74,3 +74,3 @@ 'use strict'; | ||
try { | ||
this.lockTimer = await utils.getFileLock(this.dbPath, query.softLock, query.ignoreLock); | ||
this.fileWatcher = await utils.getFileLock(this.dbPath, query.softLock, query.ignoreLock); | ||
} catch (e) { | ||
@@ -101,4 +101,4 @@ if (e.message.indexOf('Path locked') === 0) { | ||
//release file lock | ||
await utils.releaseFileLock(this.dbPath, this.lockTimer); | ||
this.lockTimer = null; | ||
await utils.releaseFileLock(this.dbPath, this.fileWatcher); | ||
this.fileWatcher = null; | ||
@@ -105,0 +105,0 @@ this.opened = false; |
@@ -131,7 +131,8 @@ 'use strict'; | ||
// locking by file existence | ||
// returns timer | ||
// returns watcher | ||
async function getFileLock(lockPath, softLock, ignoreLock) { | ||
const lockFile = `${lockPath}/__lock`; | ||
const softLockFile = `${lockPath}/__softlock`; | ||
const softLockCheckFile = `${lockPath}/__softlockcheck`; | ||
const softLockCheckName = '__softlockcheck'; | ||
const softLockCheckFile = `${lockPath}/${softLockCheckName}`; | ||
@@ -146,9 +147,15 @@ //check locks | ||
if (!ignoreLock && await pathExists(softLockFile)) { | ||
let locked = true; | ||
await fs.writeFile(softLockCheckFile, ''); | ||
const stat = await fs.stat(softLockCheckFile); | ||
await sleep(1000); | ||
let locked = true; | ||
let stat = null; | ||
try { stat = await fs.stat(softLockCheckFile); } catch(e) {} // eslint-disable-line no-empty | ||
await sleep(1000);//if main process is busy, give it time to delete softLockCheckFile | ||
if (await pathExists(softLockCheckFile)) {//not deleted | ||
const stat2 = await fs.stat(softLockCheckFile); | ||
if (stat.ctimeMs === stat2.ctimeMs) {//created by us | ||
let stat2 = null; | ||
try { stat2 = await fs.stat(softLockCheckFile); } catch(e) {} // eslint-disable-line no-empty | ||
if (stat && stat2 && stat.ctimeMs === stat2.ctimeMs) {//created by us | ||
locked = false; | ||
@@ -166,4 +173,4 @@ } | ||
//get locks | ||
let timer; | ||
//obtain locks | ||
let fileWatcher; | ||
if (!softLock) {//hard lock | ||
@@ -174,11 +181,17 @@ await fs.writeFile(lockFile, ''); | ||
timer = setInterval(async() => { | ||
await deleteFile(softLockCheckFile); | ||
}, 200); | ||
fileWatcher = fsCB.watch(lockPath, async(eventType, filename) => { | ||
if (filename == softLockCheckName) { | ||
try { | ||
await deleteFile(softLockCheckFile); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
}); | ||
} | ||
return timer; | ||
return fileWatcher; | ||
} | ||
async function releaseFileLock(lockPath, timer) { | ||
async function releaseFileLock(lockPath, fileWatcher) { | ||
const lockFile = `${lockPath}/__lock`; | ||
@@ -188,4 +201,4 @@ const softLockFile = `${lockPath}/__softlock`; | ||
if (timer) | ||
clearInterval(timer); | ||
if (fileWatcher) | ||
fileWatcher.close(); | ||
@@ -192,0 +205,0 @@ await deleteFile(softLockCheckFile); |
133740
3495