Added correlation ID in Async Tasks
MODIFY
src/async-task/AsyncTask.ts
src/async-task/AsyncTask.ts
import AsyncTask from '../types/AsyncTask';
-import Constants from '../utils/Constants';
-import Logging from '../utils/Logging';
-import { ServerAction } from '../types/Server';
-import moment from 'moment';
const MODULE_NAME = 'AbstractAsyncTask';
@@ -14,23 +10,18 @@ export default abstract class AbstractAsyncTask {
}
public async run(): Promise<void> {
- const startTime = moment();
- await Logging.logInfo({
- tenantID: Constants.DEFAULT_TENANT,
- action: ServerAction.ASYNC_TASK,
- module: MODULE_NAME, method: 'run',
- message: `The async task '${this.asyncTask.name}' is running...`
- });
- // Execute
+ // Hook
+ await this.beforeAsyncTaskRun();
+ // Process
await this.executeAsyncTask();
- // Log Total Processing Time
- const totalMigrationTimeSecs = moment.duration(moment().diff(startTime)).asSeconds();
- await Logging.logInfo({
- tenantID: Constants.DEFAULT_TENANT,
- action: ServerAction.SCHEDULER,
- module: MODULE_NAME, method: 'run',
- message: `The task '${this.asyncTask.name}' has been run in ${totalMigrationTimeSecs} secs`
- });
+ // Hook
+ await this.afterAsyncTaskRun();
+ }
+
+ public async beforeAsyncTaskRun(): Promise<void> {
+ }
+
+ public async afterAsyncTaskRun(): Promise<void> {
}
protected abstract executeAsyncTask(): Promise<void>;

MODIFY
src/async-task/AsyncTaskManager.ts
src/async-task/AsyncTaskManager.ts
@@ -88,6 +88,7 @@ export default class AsyncTaskManager {
// Get the lock
const asyncTaskLock = await LockingHelper.acquireAsyncTaskLock(Constants.DEFAULT_TENANT, asyncTask.id);
if (asyncTaskLock) {
+ const correlationID = Utils.generateShortNonUniqueID();
const startAsyncTaskTime = new Date().getTime();
try {
// Update the task
@@ -101,7 +102,7 @@ export default class AsyncTaskManager {
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.ASYNC_TASK,
module: MODULE_NAME, method: 'handleAsyncTasks',
- message: `The asynchronous task '${asyncTask.name}' is running...`
+ message: `The Async Task '${asyncTask.name}~${correlationID}' is running...`
});
// Run
await abstractAsyncTask.run();
@@ -118,7 +119,7 @@ export default class AsyncTaskManager {
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.ASYNC_TASK,
module: MODULE_NAME, method: 'handleAsyncTasks',
- message: `The asynchronous task '${asyncTask.name}' has been processed in ${asyncTaskTotalDurationSecs} secs`
+ message: `The Async Task '${asyncTask.name}~${correlationID}' has been processed in ${asyncTaskTotalDurationSecs} secs`
});
} catch (error) {
processedTask.inError++;
@@ -133,7 +134,7 @@ export default class AsyncTaskManager {
tenantID: Constants.DEFAULT_TENANT,
module: MODULE_NAME, method: 'handleAsyncTasks',
action: ServerAction.ASYNC_TASK,
- message: `Error while running the asynchronous task '${asyncTask.name}': ${error.message as string}`,
+ message: `Error while running the Async Task '${asyncTask.name}~${correlationID}': ${error.message as string}`,
detailedMessages: { error: error.stack, asyncTask }
});
} finally {

MODIFY
src/scheduler/SchedulerManager.ts
src/scheduler/SchedulerManager.ts
@@ -60,118 +60,94 @@ export default class SchedulerManager {
});
continue;
}
- let schedulerTask: SchedulerTask;
+ const schedulerTask = await SchedulerManager.createTask(task.name);
+ if (schedulerTask) {
+ cron.schedule(task.periodicity, () => SchedulerManager.runTask(schedulerTask, task));
+ await Logging.logInfo({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.SCHEDULER,
+ module: MODULE_NAME, method: 'init',
+ message: `The task '${task.name}' has been scheduled with periodicity ''${task.periodicity}'`
+ });
+ }
+ }
+ }
+
+ private static runTask(task: SchedulerTask, taskConfiguration: SchedulerTaskConfiguration): void {
+ // Set default config
+ if (!taskConfiguration.config) {
+ taskConfiguration.config = {};
+ }
+ // Do not wait for the task result
+ void task.run(taskConfiguration.name, taskConfiguration.config);
+ }
+
+ private static async createTask(name: string): Promise<SchedulerTask> {
// Tasks
- switch (task.name) {
+ switch (name) {
case 'AsyncTaskCheckTask':
- schedulerTask = new AsyncTaskCheckTask();
- break;
+ return new AsyncTaskCheckTask();
case 'LoggingDatabaseTableCleanupTask':
- schedulerTask = new LoggingDatabaseTableCleanupTask();
- break;
+ return new LoggingDatabaseTableCleanupTask();
case 'CheckUserAccountInactivityTask':
- schedulerTask = new CheckUserAccountInactivityTask();
- break;
+ return new CheckUserAccountInactivityTask();
case 'CheckOfflineChargingStationsTask':
// The task runs every five minutes
- schedulerTask = new CheckOfflineChargingStationsTask();
- break;
+ return new CheckOfflineChargingStationsTask();
case 'CheckPreparingSessionNotStartedTask':
// The task runs every five minutes
- schedulerTask = new CheckPreparingSessionNotStartedTask();
- break;
+ return new CheckPreparingSessionNotStartedTask();
case 'OICPPushEVSEDataTask':
- schedulerTask = new OICPPushEvseDataTask();
- break;
+ return new OICPPushEvseDataTask();
case 'OICPPushEvseStatusTask':
- schedulerTask = new OICPPushEvseStatusTask();
- break;
+ return new OICPPushEvseStatusTask();
case 'OCPIPushEVSEStatusesTask':
- schedulerTask = new OCPIPushEVSEStatusesTask();
- break;
+ return new OCPIPushEVSEStatusesTask();
case 'OCPIGetCdrsTask':
- schedulerTask = new OCPIGetCdrsTask();
- break;
+ return new OCPIGetCdrsTask();
case 'OCPIGetLocationsTask':
- schedulerTask = new OCPIGetLocationsTask();
- break;
+ return new OCPIGetLocationsTask();
case 'OCPIGetSessionsTask':
- schedulerTask = new OCPIGetSessionsTask();
- break;
+ return new OCPIGetSessionsTask();
case 'OCPICheckLocationsTask':
- schedulerTask = new OCPICheckLocationsTask();
- break;
+ return new OCPICheckLocationsTask();
case 'OCPICheckSessionsTask':
- schedulerTask = new OCPICheckSessionsTask();
- break;
+ return new OCPICheckSessionsTask();
case 'OCPICheckCdrsTask':
- schedulerTask = new OCPICheckCdrsTask();
- break;
+ return new OCPICheckCdrsTask();
case 'OCPIGetTokensTask':
- schedulerTask = new OCPIGetTokensTask();
- break;
+ return new OCPIGetTokensTask();
case 'OCPIPushCdrsTask':
- schedulerTask = new OCPIPushCdrsTask();
- break;
+ return new OCPIPushCdrsTask();
case 'SynchronizeRefundTransactionsTask':
- schedulerTask = new SynchronizeRefundTransactionsTask();
- break;
+ return new SynchronizeRefundTransactionsTask();
case 'SynchronizeBillingUsersTask':
- schedulerTask = new SynchronizeBillingUsersTask();
- break;
+ return new SynchronizeBillingUsersTask();
case 'SynchronizeBillingInvoicesTask':
- schedulerTask = new SynchronizeBillingInvoicesTask();
- break;
+ return new SynchronizeBillingInvoicesTask();
case 'BillingPeriodicOperationTask':
- schedulerTask = new BillingPeriodicOperationTask();
- break;
+ return new BillingPeriodicOperationTask();
case 'BillPendingTransactionTask':
- schedulerTask = new BillPendingTransactionTask();
- break;
+ return new BillPendingTransactionTask();
case 'SynchronizeCarsTask':
- schedulerTask = new SynchronizeCarsTask();
- break;
+ return new SynchronizeCarsTask();
case 'CheckSessionNotStartedAfterAuthorizeTask':
- schedulerTask = new CheckSessionNotStartedAfterAuthorizeTask();
- break;
+ return new CheckSessionNotStartedAfterAuthorizeTask();
case 'CheckAndComputeSmartChargingTask':
- schedulerTask = new CheckAndComputeSmartChargingTask();
- break;
+ return new CheckAndComputeSmartChargingTask();
case 'AssetGetConsumptionTask':
- schedulerTask = new AssetGetConsumptionTask();
- break;
+ return new AssetGetConsumptionTask();
case 'CheckChargingStationTemplateTask':
- schedulerTask = new CheckChargingStationTemplateTask();
- break;
+ return new CheckChargingStationTemplateTask();
case 'MigrateSensitiveDataTask':
- schedulerTask = new MigrateSensitiveDataTask();
- break;
+ return new MigrateSensitiveDataTask();
default:
await Logging.logError({
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.SCHEDULER,
module: MODULE_NAME, method: 'init',
- message: `The task '${task.name}' is unknown`
- });
- }
- if (schedulerTask) {
- cron.schedule(task.periodicity, () => SchedulerManager.runTask(schedulerTask, task));
- await Logging.logInfo({
- tenantID: Constants.DEFAULT_TENANT,
- action: ServerAction.SCHEDULER,
- module: MODULE_NAME, method: 'init',
- message: `The task '${task.name}' has been scheduled with periodicity ''${task.periodicity}'`
+ message: `The task '${name}' is unknown`
});
}
}
}
-
- private static runTask(task: SchedulerTask, taskConfiguration: SchedulerTaskConfiguration): void {
- // Set default config
- if (!taskConfiguration.config) {
- taskConfiguration.config = {};
- }
- // Do not wait for the task result
- void task.run(taskConfiguration.name, taskConfiguration.config);
- }
-}

MODIFY
src/scheduler/SchedulerTask.ts
src/scheduler/SchedulerTask.ts
@@ -11,11 +11,11 @@ const MODULE_NAME = 'SchedulerTask';
export default abstract class SchedulerTask {
private name: string;
- private taskID: string;
+ private correlationID: string;
public async run(name: string, config: TaskConfig): Promise<void> {
this.name = name;
- this.taskID = Utils.generateShortNonUniqueID();
+ this.correlationID = Utils.generateShortNonUniqueID();
// Get the lock
const scheduledTaskLock = await LockingHelper.acquireScheduledTaskLock(Constants.DEFAULT_TENANT, name);
if (scheduledTaskLock) {
@@ -25,7 +25,7 @@ export default abstract class SchedulerTask {
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.SCHEDULER,
module: MODULE_NAME, method: 'run',
- message: `The Task '${name}~${this.taskID}' is running...`
+ message: `The Task '${name}~${this.correlationID}' is running...`
});
try {
// Hook
@@ -39,7 +39,7 @@ export default abstract class SchedulerTask {
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.SCHEDULER,
module: MODULE_NAME, method: 'run',
- message: `Error while running the Task '${this.getName()}~${this.taskID}': ${error.message as string}`,
+ message: `Error while running the Task '${this.getName()}~${this.correlationID}': ${error.message as string}`,
detailedMessages: { error: error.stack }
});
}
@@ -49,7 +49,7 @@ export default abstract class SchedulerTask {
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.SCHEDULER,
module: MODULE_NAME, method: 'run',
- message: `The Task '${name}~${this.taskID}' has been run in ${totalMigrationTimeSecs} secs`
+ message: `The Task '${name}~${this.correlationID}' has been run in ${totalMigrationTimeSecs} secs`
});
} finally {
// Release lock
@@ -62,16 +62,15 @@ export default abstract class SchedulerTask {
return this.name;
}
- public getTaskID(): string {
- return this.taskID;
+ public getCorrelationID(): string {
+ return this.correlationID;
}
public async beforeTaskRun(config: TaskConfig): Promise<void> {
}
- public async processTask(config: TaskConfig): Promise<void> {
- }
-
public async afterTaskRun(config: TaskConfig): Promise<void> {
}
+
+ public abstract processTask(config: TaskConfig): Promise<void>;
}

MODIFY
src/scheduler/TenantSchedulerTask.ts
src/scheduler/TenantSchedulerTask.ts
@@ -16,19 +16,19 @@ export default abstract class TenantSchedulerTask extends SchedulerTask {
const tenants = await TenantStorage.getTenants({}, Constants.DB_PARAMS_MAX_LIMIT);
// Process them
for (const tenant of tenants.result) {
- const tenantTaskID = Utils.generateShortNonUniqueID();
+ const tenantCorrelationID = Utils.generateShortNonUniqueID();
const startMigrationTimeInTenant = moment();
await Logging.logDebug({
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.SCHEDULER,
module: MODULE_NAME, method: 'processTask',
- message: `The Task '${this.getName()}~${this.getTaskID()}~${tenantTaskID}' is running for Tenant ${Utils.buildTenantName(tenant)}...`
+ message: `The Task '${this.getName()}~${this.getCorrelationID()}~${tenantCorrelationID}' is running for Tenant ${Utils.buildTenantName(tenant)}...`
});
await Logging.logDebug({
tenantID: tenant.id,
action: ServerAction.SCHEDULER,
module: MODULE_NAME, method: 'processTask',
- message: `The Task '${this.getName()}~${this.getTaskID()}~${tenantTaskID}' is running...`
+ message: `The Task '${this.getName()}~${this.getCorrelationID()}~${tenantCorrelationID}' is running...`
});
try {
// Hook
@@ -42,14 +42,14 @@ export default abstract class TenantSchedulerTask extends SchedulerTask {
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.SCHEDULER,
module: MODULE_NAME, method: 'processTask',
- message: `Error while running the Task '${this.getName()}~${this.getTaskID()}~${tenantTaskID}' for Tenant ${Utils.buildTenantName(tenant)}: ${error.message as string}`,
+ message: `Error while running the Task '${this.getName()}~${this.getCorrelationID()}~${tenantCorrelationID}' for Tenant ${Utils.buildTenantName(tenant)}: ${error.message as string}`,
detailedMessages: { error: error.stack }
});
await Logging.logError({
tenantID: tenant.id,
action: ServerAction.SCHEDULER,
module: MODULE_NAME, method: 'processTask',
- message: `Error while running the Task '${this.getName()}~${this.getTaskID()}~${tenantTaskID}': ${error.message as string}`,
+ message: `Error while running the Task '${this.getName()}~${this.getCorrelationID()}~${tenantCorrelationID}': ${error.message as string}`,
detailedMessages: { error: error.stack }
});
}
@@ -59,13 +59,13 @@ export default abstract class TenantSchedulerTask extends SchedulerTask {
tenantID: Constants.DEFAULT_TENANT,
action: ServerAction.SCHEDULER,
module: MODULE_NAME, method: 'processTask',
- message: `The Task '${this.getName()}~${this.getTaskID()}~${tenantTaskID}' has been run successfully in ${totalMigrationTimeSecsInTenant} secs for Tenant ${Utils.buildTenantName(tenant)}`
+ message: `The Task '${this.getName()}~${this.getCorrelationID()}~${tenantCorrelationID}' has been run successfully in ${totalMigrationTimeSecsInTenant} secs for Tenant ${Utils.buildTenantName(tenant)}`
});
await Logging.logDebug({
tenantID: tenant.id,
action: ServerAction.SCHEDULER,
module: MODULE_NAME, method: 'processTask',
- message: `The Task '${this.getName()}~${this.getTaskID()}~${tenantTaskID}' has been run successfully in ${totalMigrationTimeSecsInTenant} secs`
+ message: `The Task '${this.getName()}~${this.getCorrelationID()}~${tenantCorrelationID}' has been run successfully in ${totalMigrationTimeSecsInTenant} secs`
});
}
}

Added correlation ID in Async Tasks