Made car catalog syncro async
MODIFY
src/async-task/AsyncTaskManager.ts
src/async-task/AsyncTaskManager.ts
@@ -18,6 +18,7 @@ import OCPIPullTokensAsyncTask from './tasks/ocpi/OCPIPullTokensAsyncTask';
import OCPIPushEVSEStatusesAsyncTask from './tasks/ocpi/OCPIPushEVSEStatusesAsyncTask';
import OCPIPushTokensAsyncTask from './tasks/ocpi/OCPIPushTokensAsyncTask';
import { ServerAction } from '../types/Server';
+import SynchronizeCarCatalogsAsyncTask from './tasks/SynchronizeCarCatalogsAsyncTask';
import TagsImportAsyncTask from './tasks/TagsImportAsyncTask';
import UsersImportAsyncTask from './tasks/UsersImportAsyncTask';
import Utils from '../utils/Utils';
@@ -80,6 +81,9 @@ export default class AsyncTaskManager {
case AsyncTasks.USERS_IMPORT:
abstractAsyncTask = new UsersImportAsyncTask(asyncTask);
break;
+ case AsyncTasks.SYNCHRONIZE_CAR_CATALOGS:
+ abstractAsyncTask = new SynchronizeCarCatalogsAsyncTask(asyncTask);
+ break;
case AsyncTasks.OCPI_PUSH_TOKENS:
abstractAsyncTask = new OCPIPushTokensAsyncTask(asyncTask);
break;
@@ -204,9 +208,6 @@ export default class AsyncTaskManager {
if (Utils.isNullOrUndefined(asyncTask.name)) {
throw new Error("The Name of the Async Task is mandatory");
}
- if (Utils.isNullOrUndefined(asyncTask.tenantID)) {
- throw new Error("The Tenant ID of the Async Task is mandatory");
- }
if (!Utils.isNullOrUndefined(asyncTask.parameters) && (typeof asyncTask.parameters !== 'object')) {
throw new Error("The Parameters of the Async Task must be a Json document");
}

ADD
src/async-task/tasks/SynchronizeCarCatalogsAsyncTask.ts
+import AbstractAsyncTask from '../AsyncTask';
+import BackendError from '../../exception/BackendError';
+import CarFactory from '../../integration/car/CarFactory';
+import Constants from '../../utils/Constants';
+import LockingHelper from '../../locking/LockingHelper';
+import LockingManager from '../../locking/LockingManager';
+import Logging from '../../utils/Logging';
+import { ServerAction } from '../../types/Server';
+
+const MODULE_NAME = 'SynchronizeCarCatalogsAsyncTask';
+
+export default class SynchronizeCarCatalogsAsyncTask extends AbstractAsyncTask {
+ protected async executeAsyncTask(): Promise<void> {
+ const syncCarCatalogsLock = await LockingHelper.createSyncCarCatalogsLock(Constants.DEFAULT_TENANT);
+ if (syncCarCatalogsLock) {
+ try {
+ const carDatabaseImpl = await CarFactory.getCarImpl();
+ if (!carDatabaseImpl) {
+ throw new BackendError({
+ source: Constants.CENTRAL_SERVER,
+ message: 'Car service is not configured',
+ module: MODULE_NAME, method: 'executeAsyncTask'
+ });
+ }
+ await carDatabaseImpl.synchronizeCarCatalogs();
+ } catch (error) {
+ // Log error
+ await Logging.logActionExceptionMessage(Constants.DEFAULT_TENANT, ServerAction.SYNCHRONIZE_CAR_CATALOGS, error);
+ } finally {
+ // Release the lock
+ await LockingManager.release(syncCarCatalogsLock);
+ }
+ }
+ }
+}

MODIFY
src/server/rest/v1/service/CarService.ts
src/server/rest/v1/service/CarService.ts
import { Action, Entity } from '../../../../types/Authorization';
+import { AsyncTaskType, AsyncTasks } from '../../../../types/AsyncTask';
import { Car, CarType } from '../../../../types/Car';
import { HTTPAuthError, HTTPError } from '../../../../types/HTTPError';
import { NextFunction, Request, Response } from 'express';
import AppAuthError from '../../../../exception/AppAuthError';
import AppError from '../../../../exception/AppError';
+import AsyncTaskManager from '../../../../async-task/AsyncTaskManager';
import Authorizations from '../../../../authorization/Authorizations';
import BackendError from '../../../../exception/BackendError';
import CarFactory from '../../../../integration/car/CarFactory';
@@ -163,18 +165,9 @@ export default class CarService {
module: MODULE_NAME, method: 'handleSynchronizeCarCatalogs'
});
}
- const carDatabaseImpl = await CarFactory.getCarImpl();
- if (!carDatabaseImpl) {
- throw new AppError({
- source: Constants.CENTRAL_SERVER,
- errorCode: HTTPError.MISSING_SETTINGS,
- message: 'Car service is not configured',
- module: MODULE_NAME, method: 'handleSynchronizeCarCatalogs'
- });
- }
// Get the lock
- const carLock = await LockingHelper.createSyncCarCatalogsLock(Constants.DEFAULT_TENANT);
- if (!carLock) {
+ const syncCarCatalogsLock = await LockingHelper.createSyncCarCatalogsLock(Constants.DEFAULT_TENANT);
+ if (!syncCarCatalogsLock) {
throw new AppError({
source: Constants.CENTRAL_SERVER,
action: action,
@@ -185,13 +178,21 @@ export default class CarService {
});
}
try {
- const result = await carDatabaseImpl.synchronizeCarCatalogs();
- res.json({ ...result, ...Constants.REST_RESPONSE_SUCCESS });
- next();
+ // Create and Save async task
+ await AsyncTaskManager.createAndSaveAsyncTasks({
+ name: AsyncTasks.SYNCHRONIZE_CAR_CATALOGS,
+ action,
+ type: AsyncTaskType.TASK,
+ module: MODULE_NAME,
+ method: 'handleSynchronizeCarCatalogs',
+ });
} finally {
// Release the lock
- await LockingManager.release(carLock);
+ await LockingManager.release(syncCarCatalogsLock);
}
+ // Return result
+ res.json(Constants.REST_RESPONSE_SUCCESS);
+ next();
}
public static async handleGetCarMakers(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {

MODIFY
src/server/rest/v1/service/OCPIEndpointService.ts
src/server/rest/v1/service/OCPIEndpointService.ts
@@ -289,7 +289,7 @@ export default class OCPIEndpointService {
}
try {
// Create and Save async task
- AsyncTaskManager.createAndSaveAsyncTasks({
+ await AsyncTaskManager.createAndSaveAsyncTasks({
name: AsyncTasks.OCPI_PULL_LOCATIONS,
action,
type: AsyncTaskType.TASK,
@@ -343,7 +343,7 @@ export default class OCPIEndpointService {
}
try {
// Create and Save async task
- AsyncTaskManager.createAndSaveAsyncTasks({
+ await AsyncTaskManager.createAndSaveAsyncTasks({
name: AsyncTasks.OCPI_PULL_SESSIONS,
action,
type: AsyncTaskType.TASK,
@@ -397,7 +397,7 @@ export default class OCPIEndpointService {
}
try {
// Create and Save async task
- AsyncTaskManager.createAndSaveAsyncTasks({
+ await AsyncTaskManager.createAndSaveAsyncTasks({
name: AsyncTasks.OCPI_PULL_TOKENS,
action,
type: AsyncTaskType.TASK,
@@ -451,7 +451,7 @@ export default class OCPIEndpointService {
}
try {
// Create and Save async task
- AsyncTaskManager.createAndSaveAsyncTasks({
+ await AsyncTaskManager.createAndSaveAsyncTasks({
name: AsyncTasks.OCPI_PULL_CDRS,
action,
type: AsyncTaskType.TASK,
@@ -505,7 +505,7 @@ export default class OCPIEndpointService {
}
try {
// Create and Save async task
- AsyncTaskManager.createAndSaveAsyncTasks({
+ await AsyncTaskManager.createAndSaveAsyncTasks({
name: AsyncTasks.OCPI_CHECK_CDRS,
action,
type: AsyncTaskType.TASK,
@@ -559,7 +559,7 @@ export default class OCPIEndpointService {
}
try {
// Create and Save async task
- AsyncTaskManager.createAndSaveAsyncTasks({
+ await AsyncTaskManager.createAndSaveAsyncTasks({
name: AsyncTasks.OCPI_CHECK_SESSIONS,
action,
type: AsyncTaskType.TASK,
@@ -613,7 +613,7 @@ export default class OCPIEndpointService {
}
try {
// Create and Save async task
- AsyncTaskManager.createAndSaveAsyncTasks({
+ await AsyncTaskManager.createAndSaveAsyncTasks({
name: AsyncTasks.OCPI_CHECK_LOCATIONS,
action,
type: AsyncTaskType.TASK,
@@ -667,7 +667,7 @@ export default class OCPIEndpointService {
}
try {
// Create and Save async task
- AsyncTaskManager.createAndSaveAsyncTasks({
+ await AsyncTaskManager.createAndSaveAsyncTasks({
name: AsyncTasks.OCPI_PUSH_EVSE_STATUSES,
action,
type: AsyncTaskType.TASK,
@@ -721,7 +721,7 @@ export default class OCPIEndpointService {
}
try {
// Create and Save async task
- AsyncTaskManager.createAndSaveAsyncTasks({
+ await AsyncTaskManager.createAndSaveAsyncTasks({
name: AsyncTasks.OCPI_PUSH_TOKENS,
action,
type: AsyncTaskType.TASK,

MODIFY
src/server/rest/v1/service/TagService.ts
src/server/rest/v1/service/TagService.ts
@@ -589,7 +589,7 @@ export default class TagService {
`No Tag have been uploaded in ${executionDurationSecs}s`, req.user
);
// Create and Save async task
- AsyncTaskManager.createAndSaveAsyncTasks({
+ await AsyncTaskManager.createAndSaveAsyncTasks({
name: AsyncTasks.TAGS_IMPORT,
action: ServerAction.TAGS_IMPORT,
type: AsyncTaskType.TASK,

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -987,7 +987,7 @@ export default class UserService {
`No User have been uploaded in ${executionDurationSecs}s`, req.user
);
// Create and Save async task
- AsyncTaskManager.createAndSaveAsyncTasks({
+ await AsyncTaskManager.createAndSaveAsyncTasks({
name: AsyncTasks.USERS_IMPORT,
action: ServerAction.USERS_IMPORT,
type: AsyncTaskType.TASK,

MODIFY
src/types/AsyncTask.ts
src/types/AsyncTask.ts
@@ -6,7 +6,7 @@ export default interface AsyncTask extends CreatedUpdatedProps {
name: AsyncTasks;
action: ServerAction;
type: AsyncTaskType;
- tenantID: string;
+ tenantID?: string;
status?: AsyncTaskStatus;
parent?: string;
execHost?: string;
@@ -43,5 +43,6 @@ export enum AsyncTasks {
OCPI_CHECK_LOCATIONS = 'OCPICheckLocationsAsyncTask',
OCPI_PULL_TOKENS = 'OCPIPullTokensAsyncTask',
OCPI_PUSH_EVSE_STATUSES = 'OCPIPushEVSEStatusesAsyncTask',
+ SYNCHRONIZE_CAR_CATALOGS = 'SynchronizeCarCatalogsAsyncTask',
}

Made car catalog syncro async