X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fperformance%2Fstorage%2FJsonFileStorage.ts;h=2a712a6008f61722ef21dddaac5acd2e3ba14d53;hb=66dd344779f5258bbf4c76b386d005c0c2160b11;hp=2f90fc460063b335ef716a47b14ff6b694246445;hpb=edd134392e237a3242dc2093341df70244c51472;p=e-mobility-charging-stations-simulator.git diff --git a/src/performance/storage/JsonFileStorage.ts b/src/performance/storage/JsonFileStorage.ts index 2f90fc46..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 'fs'; +import fs from 'node:fs'; +import path from 'node:path'; -import lockfile from 'proper-lockfile'; - -import { FileType } from '../../types/FileType'; -import type { Statistics } from '../../types/Statistics'; -import FileUtils from '../../utils/FileUtils'; -import Utils from '../../utils/Utils'; import { Storage } from './Storage'; +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,46 +18,46 @@ 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.logPrefix, - FileType.PerformanceRecords, - this.dbName, - error as NodeJS.ErrnoException - ); - } - 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) { + 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( - this.logPrefix, - FileType.PerformanceRecords, + ErrorUtils.handleFileException( this.dbName, - error as NodeJS.ErrnoException + FileType.PerformanceRecords, + error as NodeJS.ErrnoException, + this.logPrefix ); } } @@ -71,18 +69,18 @@ export class JsonFileStorage extends Storage { this.fd = null; } } catch (error) { - FileUtils.handleFileException( - this.logPrefix, - FileType.PerformanceRecords, + ErrorUtils.handleFileException( this.dbName, - error as NodeJS.ErrnoException + FileType.PerformanceRecords, + error as NodeJS.ErrnoException, + this.logPrefix ); } } private checkPerformanceRecordsFile(): void { if (!this?.fd) { - throw new Error( + throw new BaseError( `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found` ); }