Used constants in Locking lib, decrease Smart Charging lock timeout
MODIFY
src/locking/LockingHelper.ts
src/locking/LockingHelper.ts
@@ -32,9 +32,9 @@ export default class LockingHelper {
return lock;
}
- public static async acquireSiteAreaSmartChargingLock(tenantID: string, siteArea: SiteArea, timeoutSecs: number): Promise<Lock | null> {
+ public static async acquireSiteAreaSmartChargingLock(tenantID: string, siteArea: SiteArea): Promise<Lock | null> {
const lock = LockingManager.createExclusiveLock(tenantID, LockEntity.SITE_AREA, `${siteArea.id}-smart-charging`, 180);
- if (!(await LockingManager.acquire(lock, timeoutSecs))) {
+ if (!(await LockingManager.acquire(lock, Constants.SMART_CHARGING_LOCK_SECS))) {
return null;
}
return lock;

MODIFY
src/locking/LockingManager.ts
src/locking/LockingManager.ts
import Lock, { LockEntity, LockType } from '../types/Locking';
import BackendError from '../exception/BackendError';
+import Constants from '../utils/Constants';
import LockingStorage from '../storage/mongodb/LockingStorage';
import Logging from '../utils/Logging';
import { ServerAction } from '../types/Server';
@@ -137,9 +138,8 @@ export default class LockingManager {
return;
} catch {
// Wait before trying to get the next lock
- const lockWaitTimeMillis = 250 + Math.trunc(Math.random() * 750);
- Utils.isDevelopmentEnv() && Logging.logConsoleWarning(`>> Lock failed, Wait for ${lockWaitTimeMillis}ms, lock '${lock.tenantID}~${lock.entity}~${lock.key}`);
- await Utils.sleep(lockWaitTimeMillis);
+ Utils.isDevelopmentEnv() && Logging.logConsoleWarning(`>> Lock failed, Wait for ${Constants.LOCK_WAIT_MILLIS}ms, lock '${lock.tenantID}~${lock.entity}~${lock.key}`);
+ await Utils.sleep(Constants.LOCK_WAIT_MILLIS);
}
} while (Date.now() < timeoutDateMs);
throw Error(`Lock acquisition timeout ${timeoutSecs} secs reached`);

MODIFY
src/scheduler/tasks/AssetGetConsumptionTask.ts
src/scheduler/tasks/AssetGetConsumptionTask.ts
@@ -127,7 +127,7 @@ export default class AssetGetConsumptionTask extends SchedulerTask {
}
private async triggerSmartCharging(tenant: Tenant, siteArea: SiteArea) {
- const siteAreaLock = await LockingHelper.acquireSiteAreaSmartChargingLock(tenant.id, siteArea, 30);
+ const siteAreaLock = await LockingHelper.acquireSiteAreaSmartChargingLock(tenant.id, siteArea);
if (siteAreaLock) {
try {
const smartCharging = await SmartChargingFactory.getSmartChargingImpl(tenant);

MODIFY
src/scheduler/tasks/CheckAndComputeSmartChargingTask.ts
src/scheduler/tasks/CheckAndComputeSmartChargingTask.ts
@@ -22,7 +22,7 @@ export default class CheckAndComputeSmartChargingTask extends SchedulerTask {
Constants.DB_PARAMS_MAX_LIMIT);
// Get Site Area
for (const siteArea of siteAreas.result) {
- const siteAreaLock = await LockingHelper.acquireSiteAreaSmartChargingLock(tenant.id, siteArea, 30);
+ const siteAreaLock = await LockingHelper.acquireSiteAreaSmartChargingLock(tenant.id, siteArea);
if (siteAreaLock) {
try {
// Get implementation

MODIFY
src/server/ocpp/services/OCPPService.ts
src/server/ocpp/services/OCPPService.ts
@@ -1548,7 +1548,7 @@ export default class OCPPService {
// Get Site Area
const siteArea = await SiteAreaStorage.getSiteArea(tenant, chargingStation.siteAreaID);
if (siteArea && siteArea.smartCharging) {
- const siteAreaLock = await LockingHelper.acquireSiteAreaSmartChargingLock(tenant.id, siteArea, 30);
+ const siteAreaLock = await LockingHelper.acquireSiteAreaSmartChargingLock(tenant.id, siteArea);
if (siteAreaLock) {
try {
const smartCharging = await SmartChargingFactory.getSmartChargingImpl(tenant);

MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -571,7 +571,7 @@ export default class ChargingStationService {
user: req.user
});
}
- const siteAreaLock = await LockingHelper.acquireSiteAreaSmartChargingLock(req.user.tenantID, siteArea, 30);
+ const siteAreaLock = await LockingHelper.acquireSiteAreaSmartChargingLock(req.user.tenantID, siteArea);
if (siteAreaLock) {
try {
// Call

MODIFY
src/server/rest/v1/service/SiteAreaService.ts
src/server/rest/v1/service/SiteAreaService.ts
@@ -360,7 +360,7 @@ export default class SiteAreaService {
// FIXME: the lock acquisition can wait for 30s before timeout and the whole code execution timeout at 3s
// eslint-disable-next-line @typescript-eslint/no-misused-promises
setTimeout(async () => {
- const siteAreaLock = await LockingHelper.acquireSiteAreaSmartChargingLock(req.user.tenantID, siteArea, 30);
+ const siteAreaLock = await LockingHelper.acquireSiteAreaSmartChargingLock(req.user.tenantID, siteArea);
if (siteAreaLock) {
try {
const smartCharging = await SmartChargingFactory.getSmartChargingImpl(req.tenant);

MODIFY
src/utils/Constants.ts
src/utils/Constants.ts
@@ -39,6 +39,9 @@ export default class Constants {
public static readonly IMPORT_BATCH_INSERT_SIZE = 250;
public static readonly BATCH_PAGE_SIZE = 1000;
+ public static readonly LOCK_WAIT_MILLIS = 500;
+
+ public static readonly SMART_CHARGING_LOCK_SECS = 5;
public static readonly CHARGING_STATION_LOCK_SECS = 5;
public static readonly CHARGING_STATION_CONNECTION_LOCK_SECS = 5;

MODIFY
test/api/LockingTest.ts
test/api/LockingTest.ts
@@ -102,7 +102,7 @@ describe('Locking', function() {
const siteArea = testData.siteAreaContext.getSiteArea();
// Create and Acquire lock
testData.siteExclusiveLock = await LockingHelper.acquireSiteAreaSmartChargingLock(
- testData.tenantContext.getTenant().id, siteArea, 30);
+ testData.tenantContext.getTenant().id, siteArea);
expect(testData.siteExclusiveLock).not.null;
expect(testData.siteExclusiveLock.id).not.null;
expect(testData.siteExclusiveLock.hostname).not.null;

Used constants in Locking lib, decrease Smart Charging lock timeout