Made async pull tokens
MODIFY
src/async-task/AsyncTaskManager.ts
src/async-task/AsyncTaskManager.ts
@@ -14,6 +14,7 @@ import OCPICheckSessionsAsyncTask from './tasks/ocpi/OCPICheckSessionsAsyncTask'
import OCPIPullCdrsAsyncTask from './tasks/ocpi/OCPIPullCdrsAsyncTask';
import OCPIPullLocationsAsyncTask from './tasks/ocpi/OCPIPullLocationsAsyncTask';
import OCPIPullSessionsAsyncTask from './tasks/ocpi/OCPIPullSessionsAsyncTask';
+import OCPIPullTokensAsyncTask from './tasks/ocpi/OCPIPullTokensAsyncTask';
import OCPIPushTokensAsyncTask from './tasks/ocpi/OCPIPushTokensAsyncTask';
import { ServerAction } from '../types/Server';
import TagsImportAsyncTask from './tasks/TagsImportAsyncTask';
@@ -98,6 +99,8 @@ export default class AsyncTaskManager {
break;
case AsyncTasks.OCPI_CHECK_LOCATIONS:
abstractAsyncTask = new OCPICheckLocationsAsyncTask(asyncTask);
+ case AsyncTasks.OCPI_PULL_TOKENS:
+ abstractAsyncTask = new OCPIPullTokensAsyncTask(asyncTask);
break;
default:
await Logging.logError({

ADD
src/async-task/tasks/ocpi/OCPIPullTokensAsyncTask.ts
+import AbstractAsyncTask from '../../AsyncTask';
+import LockingHelper from '../../../locking/LockingHelper';
+import LockingManager from '../../../locking/LockingManager';
+import Logging from '../../../utils/Logging';
+import OCPIClientFactory from '../../../client/ocpi/OCPIClientFactory';
+import OCPIEndpointStorage from '../../../storage/mongodb/OCPIEndpointStorage';
+import { ServerAction } from '../../../types/Server';
+import TenantComponents from '../../../types/TenantComponents';
+import TenantStorage from '../../../storage/mongodb/TenantStorage';
+import Utils from '../../../utils/Utils';
+
+export default class OCPIPullTokensAsyncTask extends AbstractAsyncTask {
+ protected async executeAsyncTask(): Promise<void> {
+ const tenant = await TenantStorage.getTenant(this.asyncTask.tenantID);
+ // Check if OCPI component is active
+ if (Utils.isTenantComponentActive(tenant, TenantComponents.OCPI)) {
+ try {
+ // Get the OCPI Endpoint
+ const ocpiEndpoint = await OCPIEndpointStorage.getOcpiEndpoint(tenant.id, this.asyncTask.parameters.endpointID);
+ if (!ocpiEndpoint) {
+ throw new Error(`Unknown OCPI Endpoint ID '${this.asyncTask.parameters.endpointID}'`);
+ }
+ const pullTokensLock = await LockingHelper.createOCPIPullTokensLock(tenant.id, ocpiEndpoint, false);
+ if (pullTokensLock) {
+ try {
+ // Get the OCPI Client
+ const ocpiClient = await OCPIClientFactory.getCpoOcpiClient(tenant, ocpiEndpoint);
+ if (!ocpiClient) {
+ throw new Error(`OCPI Client not found in Endpoint ID '${this.asyncTask.parameters.endpointID}'`);
+ }
+ await ocpiClient.pullTokens(false);
+ } finally {
+ // Release the lock
+ await LockingManager.release(pullTokensLock);
+ }
+ }
+ } catch (error) {
+ await Logging.logActionExceptionMessage(tenant.id, ServerAction.OCPI_PULL_TOKENS, error);
+ }
+ }
+ }
+}

MODIFY
src/server/rest/v1/service/OCPIEndpointService.ts
src/server/rest/v1/service/OCPIEndpointService.ts
@@ -385,8 +385,8 @@ export default class OCPIEndpointService {
MODULE_NAME, 'handlePullTokensEndpoint', req.user);
const tenant = await TenantStorage.getTenant(req.user.tenantID);
// Get the lock
- const ocpiLock = await LockingHelper.createOCPIPullTokensLock(tenant.id, ocpiEndpoint, false);
- if (!ocpiLock) {
+ const pullTokensLock = await LockingHelper.createOCPIPullTokensLock(tenant.id, ocpiEndpoint, false);
+ if (!pullTokensLock) {
throw new AppError({
source: Constants.CENTRAL_SERVER,
action: ServerAction.OCPI_PULL_TOKENS,
@@ -396,15 +396,25 @@ export default class OCPIEndpointService {
});
}
try {
- // Build OCPI Client
- const ocpiClient = await OCPIClientFactory.getCpoOcpiClient(tenant, ocpiEndpoint);
- const result = await ocpiClient.pullTokens(false);
- // Return result
- res.json(result);
- next();
+ // Create and Save async task
+ AsyncTaskManager.createAndSaveAsyncTasks({
+ name: AsyncTasks.OCPI_PULL_TOKENS,
+ action,
+ type: AsyncTaskType.TASK,
+ tenantID: req.tenant.id,
+ parameters: {
+ endpointID: filteredRequest.id,
+ },
+ module: MODULE_NAME,
+ method: 'handlePullTokensEndpoint',
+ });
} finally {
- await LockingManager.release(ocpiLock);
+ // Release the lock
+ await LockingManager.release(pullTokensLock);
}
+ // Return result
+ res.json(Constants.REST_RESPONSE_SUCCESS);
+ next();
}
static async handlePullCdrsEndpoint(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {

MODIFY
src/types/AsyncTask.ts
src/types/AsyncTask.ts
@@ -41,5 +41,6 @@ export enum AsyncTasks {
OCPI_CHECK_CDRS = 'OCPICheckCdrsAsyncTask',
OCPI_CHECK_SESSIONS = 'OCPICheckSessionsAsyncTask',
OCPI_CHECK_LOCATIONS = 'OCPICheckLocationsAsyncTask',
+ OCPI_PULL_TOKENS = 'OCPIPullTokensAsyncTask',
}

Made async pull tokens