fix: use homebrew async locking primitive to order file writing
[e-mobility-charging-stations-simulator.git] / src / performance / storage / JsonFileStorage.ts
CommitLineData
edd13439 1// Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
c27c3eee 2
130783a7 3import fs from 'node:fs';
8114d10e 4
268a74bb 5import { FileType, type Statistics } from '../../types';
1227a6f1 6import { AsyncLock, AsyncLockType, Constants, FileUtils, Utils } from '../../utils';
2896e06d 7import { Storage } from '../internal';
72f041bd 8
100a5301 9export class JsonFileStorage extends Storage {
2a370053
JB
10 private fd: number | null = null;
11
1f5df42a
JB
12 constructor(storageUri: string, logPrefix: string) {
13 super(storageUri, logPrefix);
14 this.dbName = this.storageUri.pathname;
72f041bd
JB
15 }
16
17 public storePerformanceStatistics(performanceStatistics: Statistics): void {
2a370053 18 this.checkPerformanceRecordsFile();
1227a6f1
JB
19 const asyncLock = AsyncLock.getInstance(AsyncLockType.performance);
20 asyncLock
21 .acquire()
22 .then(() => {
23 const fileData = fs.readFileSync(this.dbName, 'utf8');
24 const performanceRecords: Statistics[] = fileData
25 ? (JSON.parse(fileData) as Statistics[])
26 : [];
27 performanceRecords.push(performanceStatistics);
28 fs.writeFileSync(
29 this.dbName,
30 Utils.JSONStringifyWithMapSupport(performanceRecords, 2),
31 'utf8'
32 );
33 })
34 .catch((error) => {
35 FileUtils.handleFileException(
36 this.dbName,
37 FileType.PerformanceRecords,
38 error as NodeJS.ErrnoException,
39 this.logPrefix
40 );
c63c21bc 41 })
1227a6f1
JB
42 .finally(() => {
43 asyncLock.release().catch(Constants.EMPTY_FUNCTION);
44 });
72f041bd 45 }
b652b0c3 46
2a370053 47 public open(): void {
b652b0c3 48 try {
4c5e87ae 49 if (Utils.isNullOrUndefined(this?.fd)) {
a6ceb16a
JB
50 this.fd = fs.openSync(this.dbName, 'a+');
51 }
b652b0c3 52 } catch (error) {
e7aeea18 53 FileUtils.handleFileException(
e7aeea18 54 this.dbName,
7164966d
JB
55 FileType.PerformanceRecords,
56 error as NodeJS.ErrnoException,
57 this.logPrefix
e7aeea18 58 );
2a370053
JB
59 }
60 }
61
62 public close(): void {
63 try {
a6ceb16a 64 if (this?.fd) {
2a370053
JB
65 fs.closeSync(this.fd);
66 this.fd = null;
67 }
68 } catch (error) {
e7aeea18 69 FileUtils.handleFileException(
e7aeea18 70 this.dbName,
7164966d
JB
71 FileType.PerformanceRecords,
72 error as NodeJS.ErrnoException,
73 this.logPrefix
e7aeea18 74 );
2a370053
JB
75 }
76 }
77
78 private checkPerformanceRecordsFile(): void {
73d09045 79 if (!this?.fd) {
e7aeea18
JB
80 throw new Error(
81 `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found`
82 );
b652b0c3
JB
83 }
84 }
72f041bd 85}