X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fperformance%2Fstorage%2FJsonFileStorage.ts;h=2a712a6008f61722ef21dddaac5acd2e3ba14d53;hb=66dd344779f5258bbf4c76b386d005c0c2160b11;hp=bacc8d21486e02f2b1fd18e79c7db5d28fa71605;hpb=7164966d863b4539243b473c5e2e9d22fb9b5fd1;p=e-mobility-charging-stations-simulator.git diff --git a/src/performance/storage/JsonFileStorage.ts b/src/performance/storage/JsonFileStorage.ts index bacc8d21..2a712a60 100644 --- a/src/performance/storage/JsonFileStorage.ts +++ b/src/performance/storage/JsonFileStorage.ts @@ -1,14 +1,12 @@ // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. import fs from 'node:fs'; - -import lockfile from 'proper-lockfile'; +import path from 'node:path'; import { Storage } from './Storage'; -import { FileType } from '../../types/FileType'; -import type { Statistics } from '../../types/Statistics'; -import FileUtils from '../../utils/FileUtils'; -import Utils from '../../utils/Utils'; +import { BaseError } from '../../exception'; +import { FileType, type Statistics } from '../../types'; +import { AsyncLock, AsyncLockType, Constants, ErrorUtils, Utils } from '../../utils'; export class JsonFileStorage extends Storage { private fd: number | null = null; @@ -20,42 +18,42 @@ export class JsonFileStorage extends Storage { public storePerformanceStatistics(performanceStatistics: Statistics): void { this.checkPerformanceRecordsFile(); - lockfile - .lock(this.dbName, { stale: 5000, retries: 3 }) - .then(async (release) => { - try { - const fileData = fs.readFileSync(this.dbName, 'utf8'); - const performanceRecords: Statistics[] = fileData - ? (JSON.parse(fileData) as Statistics[]) - : []; - performanceRecords.push(performanceStatistics); - fs.writeFileSync( - this.dbName, - Utils.JSONStringifyWithMapSupport(performanceRecords, 2), - 'utf8' - ); - } catch (error) { - FileUtils.handleFileException( - this.dbName, - FileType.PerformanceRecords, - error as NodeJS.ErrnoException, - this.logPrefix - ); - } - await release(); + AsyncLock.acquire(AsyncLockType.performance) + .then(() => { + const fileData = fs.readFileSync(this.dbName, 'utf8'); + const performanceRecords: Statistics[] = fileData + ? (JSON.parse(fileData) as Statistics[]) + : []; + performanceRecords.push(performanceStatistics); + fs.writeFileSync( + this.dbName, + Utils.JSONStringifyWithMapSupport(performanceRecords, 2), + 'utf8' + ); }) - .catch(() => { - /* This is intentional */ + .catch((error) => { + ErrorUtils.handleFileException( + this.dbName, + FileType.PerformanceRecords, + error as NodeJS.ErrnoException, + this.logPrefix + ); + }) + .finally(() => { + AsyncLock.release(AsyncLockType.performance).catch(Constants.EMPTY_FUNCTION); }); } public open(): void { try { - if (this?.fd === undefined || this?.fd === null) { + if (Utils.isNullOrUndefined(this?.fd)) { + if (!fs.existsSync(path.dirname(this.dbName))) { + fs.mkdirSync(path.dirname(this.dbName), { recursive: true }); + } this.fd = fs.openSync(this.dbName, 'a+'); } } catch (error) { - FileUtils.handleFileException( + ErrorUtils.handleFileException( this.dbName, FileType.PerformanceRecords, error as NodeJS.ErrnoException, @@ -71,7 +69,7 @@ export class JsonFileStorage extends Storage { this.fd = null; } } catch (error) { - FileUtils.handleFileException( + ErrorUtils.handleFileException( this.dbName, FileType.PerformanceRecords, error as NodeJS.ErrnoException, @@ -82,7 +80,7 @@ export class JsonFileStorage extends Storage { private checkPerformanceRecordsFile(): void { if (!this?.fd) { - throw new Error( + throw new BaseError( `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found` ); }