From fc1275aeb2f893f88c65e2fbd9868952d24d3123 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Tue, 16 Feb 2021 22:29:57 -0600 Subject: [PATCH] Do not lock until after the file is created (#274) Proper-lockfile throws if the file it's locking does not exist. Lock around adapter creation rather than file creation. --- src/services/lowdbStorage.service.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/services/lowdbStorage.service.ts b/src/services/lowdbStorage.service.ts index 03890b8bd26..b41289f902b 100644 --- a/src/services/lowdbStorage.service.ts +++ b/src/services/lowdbStorage.service.ts @@ -28,17 +28,17 @@ export class LowdbStorageService implements StorageService { this.logService.info(`Created dir "${this.dir}".`); } this.dataFilePath = path.join(this.dir, 'data.json'); + if (!fs.existsSync(this.dataFilePath)) { + this.logService.warning(`Could not find data file, "${this.dataFilePath}"; creating it instead.`); + fs.writeFileSync(this.dataFilePath, '', { mode: 0o600 }); + fs.chmodSync(this.dataFilePath, 0o600); + this.logService.info(`Created data file "${this.dataFilePath}" with chmod 600.`); + } else { + this.logService.info(`db file "${this.dataFilePath} already exists"; using existing db`); + } await this.lockDbFile(() => { - if (!fs.existsSync(this.dataFilePath)) { - this.logService.warning(`Could not find data file, "${this.dataFilePath}"; creating it instead.`); - fs.writeFileSync(this.dataFilePath, '', { mode: 0o600 }); - fs.chmodSync(this.dataFilePath, 0o600); - this.logService.info(`Created data file "${this.dataFilePath}" with chmod 600.`); - } else { - this.logService.info(`db file "${this.dataFilePath} already exists"; using existing db`); - } + adapter = new FileSync(this.dataFilePath); }); - adapter = new FileSync(this.dataFilePath); } try { this.logService.info('Attempting to create lowdb storage adapter.');