// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
import { createHash } from 'node:crypto';
-import {
- type FSWatcher,
- closeSync,
- existsSync,
- mkdirSync,
- openSync,
- readFileSync,
- writeFileSync,
-} from 'node:fs';
+import { type FSWatcher, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { URL } from 'node:url';
import { parentPort } from 'node:worker_threads';
)
.digest('hex');
if (this.configurationFileHash !== configurationHash) {
- AsyncLock.acquire(AsyncLockType.configuration)
- .then(() => {
- configurationData.configurationHash = configurationHash;
- const measureId = `${FileType.ChargingStationConfiguration} write`;
- const beginId = PerformanceStatistics.beginMeasure(measureId);
- const fileDescriptor = openSync(this.configurationFile, 'w');
- writeFileSync(fileDescriptor, JSON.stringify(configurationData, null, 2), 'utf8');
- closeSync(fileDescriptor);
- PerformanceStatistics.endMeasure(measureId, beginId);
- this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
- this.sharedLRUCache.setChargingStationConfiguration(configurationData);
- this.configurationFileHash = configurationHash;
- })
- .catch((error) => {
- handleFileException(
- this.configurationFile,
- FileType.ChargingStationConfiguration,
- error as NodeJS.ErrnoException,
- this.logPrefix(),
- );
- })
- .finally(() => {
- AsyncLock.release(AsyncLockType.configuration).catch(Constants.EMPTY_FUNCTION);
- });
+ AsyncLock.runExclusive(AsyncLockType.configuration, () => {
+ configurationData.configurationHash = configurationHash;
+ const measureId = `${FileType.ChargingStationConfiguration} write`;
+ const beginId = PerformanceStatistics.beginMeasure(measureId);
+ writeFileSync(
+ this.configurationFile,
+ JSON.stringify(configurationData, null, 2),
+ 'utf8',
+ );
+ PerformanceStatistics.endMeasure(measureId, beginId);
+ this.sharedLRUCache.deleteChargingStationConfiguration(this.configurationFileHash);
+ this.sharedLRUCache.setChargingStationConfiguration(configurationData);
+ this.configurationFileHash = configurationHash;
+ }).catch((error) => {
+ handleFileException(
+ this.configurationFile,
+ FileType.ChargingStationConfiguration,
+ error as NodeJS.ErrnoException,
+ this.logPrefix(),
+ );
+ });
} else {
logger.debug(
`${this.logPrefix()} Not saving unchanged charging station configuration file ${
import {
AsyncLock,
AsyncLockType,
- Constants,
JSONStringifyWithMapSupport,
handleFileException,
isNullOrUndefined,
public storePerformanceStatistics(performanceStatistics: Statistics): void {
this.checkPerformanceRecordsFile();
- AsyncLock.acquire(AsyncLockType.performance)
- .then(() => {
- JsonFileStorage.performanceRecords.set(performanceStatistics.id, performanceStatistics);
- writeSync(
- this.fd!,
- JSONStringifyWithMapSupport([...JsonFileStorage.performanceRecords.values()], 2),
- 0,
- 'utf8',
- );
- })
- .catch((error) => {
- handleFileException(
- this.dbName,
- FileType.PerformanceRecords,
- error as NodeJS.ErrnoException,
- this.logPrefix,
- );
- })
- .finally(() => {
- AsyncLock.release(AsyncLockType.performance).catch(Constants.EMPTY_FUNCTION);
- });
+ AsyncLock.runExclusive(AsyncLockType.performance, () => {
+ JsonFileStorage.performanceRecords.set(performanceStatistics.id, performanceStatistics);
+ writeSync(
+ this.fd!,
+ JSONStringifyWithMapSupport([...JsonFileStorage.performanceRecords.values()], 2),
+ 0,
+ 'utf8',
+ );
+ }).catch((error) => {
+ handleFileException(
+ this.dbName,
+ FileType.PerformanceRecords,
+ error as NodeJS.ErrnoException,
+ this.logPrefix,
+ );
+ });
}
public open(): void {
if (!existsSync(dirname(this.dbName))) {
mkdirSync(dirname(this.dbName), { recursive: true });
}
- this.fd = openSync(this.dbName, 'w+');
+ this.fd = openSync(this.dbName, 'w');
}
} catch (error) {
handleFileException(
import Queue from 'mnemonist/queue.js';
+import { Constants } from './Constants';
+
export enum AsyncLockType {
configuration = 'configuration',
performance = 'performance',
this.resolveQueue = new Queue<ResolveType>();
}
- public static async acquire(type: AsyncLockType): Promise<void> {
+ public static async runExclusive<T>(type: AsyncLockType, fn: () => T | Promise<T>): Promise<T> {
+ return AsyncLock.acquire(type)
+ .then(fn)
+ .finally(() => {
+ AsyncLock.release(type).catch(Constants.EMPTY_FUNCTION);
+ });
+ }
+
+ private static async acquire(type: AsyncLockType): Promise<void> {
const asyncLock = AsyncLock.getAsyncLock(type);
if (!asyncLock.acquired) {
asyncLock.acquired = true;
});
}
- public static async release(type: AsyncLockType): Promise<void> {
+ private static async release(type: AsyncLockType): Promise<void> {
const asyncLock = AsyncLock.getAsyncLock(type);
if (asyncLock.resolveQueue.size === 0 && asyncLock.acquired) {
asyncLock.acquired = false;