Skip to content

Commit

Permalink
Merge pull request #726 from sf316/703_handle_workstep_execution_fail…
Browse files Browse the repository at this point in the history
…ures

703 Handle Workstep Execution Failures
  • Loading branch information
Therecanbeonlyone1969 authored Aug 25, 2023
2 parents 1496e75 + 21dd22f commit 16211f1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum BpiMessageType {
Info,
Transaction,
Err,
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { LoggingService } from '../../../../shared/logging/logging.service';
import { CommandHandler, ICommandHandler, EventBus } from '@nestjs/cqrs';
import { TransactionStorageAgent } from '../../../transactions/agents/transactionStorage.agent';
import { TransactionAgent } from '../../../transactions/agents/transactions.agent';
import { TransactionStatus } from '../../../transactions/models/transactionStatus.enum';
import { WorkstepStorageAgent } from '../../../workgroup/worksteps/agents/workstepsStorage.agent';
import { ExecuteVsmCycleCommand } from './executeVsmCycle.command';
import { WorkstepExecutionFailuresEvent } from '../handleWorkstepFailuresEvents/workstepExecutionFailures.event';

@CommandHandler(ExecuteVsmCycleCommand)
export class ExecuteVsmCycleCommandHandler
Expand All @@ -14,7 +14,7 @@ export class ExecuteVsmCycleCommandHandler
private agent: TransactionAgent,
private workstepStorageAgent: WorkstepStorageAgent,
private txStorageAgent: TransactionStorageAgent,
private readonly logger: LoggingService,
private eventBus: EventBus,
) {}

async execute(command: ExecuteVsmCycleCommand) {

Check warning on line 20 in examples/bri-3/src/bri/vsm/capabilites/executeVsmCycle/executeVsmCycleCommand.handler.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 16.17.0)

'command' is defined but never used
Expand All @@ -29,6 +29,9 @@ export class ExecuteVsmCycleCommandHandler
await this.txStorageAgent.updateTransactionStatus(tx);

if (!this.agent.validateTransactionForExecution(tx)) {
this.eventBus.publish(
new WorkstepExecutionFailuresEvent(tx, 'Validation Error'),
);
tx.updateStatusToInvalid();
await this.txStorageAgent.updateTransactionStatus(tx);
return;
Expand All @@ -45,9 +48,7 @@ export class ExecuteVsmCycleCommandHandler
tx.updateStatusToExecuted();
this.txStorageAgent.updateTransactionStatus(tx);
} catch (error) {
this.logger.logError(
`Error executing transaction with id ${tx.id}: ${error}`,
);
this.eventBus.publish(new WorkstepExecutionFailuresEvent(tx, error));
tx.updateStatusToAborted();
this.txStorageAgent.updateTransactionStatus(tx);
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Transaction } from 'src/bri/transactions/models/transaction';

export class WorkstepExecutionFailuresEvent {
constructor(public readonly tx: Transaction, public readonly err: string) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { IEventHandler } from '@nestjs/cqrs';
import { EventsHandler } from '@nestjs/cqrs/dist/decorators/events-handler.decorator';
import { WorkstepExecutionFailuresEvent } from './workstepExecutionFailures.event';
import { LoggingService } from '../../../../shared/logging/logging.service';
import { BpiMessage } from '../../../communication/models/bpiMessage';
import { BpiMessageType } from '../../../communication/models/bpiMessageType.enum';
import { MessagingAgent } from '../../../communication/agents/messaging.agent';

@EventsHandler(WorkstepExecutionFailuresEvent)
export class WorkstepExecutionFailuresHandler
implements IEventHandler<WorkstepExecutionFailuresEvent>
{
constructor(
private readonly logger: LoggingService,
private readonly messagingAgent: MessagingAgent,
) {}

handle(event: WorkstepExecutionFailuresEvent) {
const message = `Failed execution of transaction with id ${event.tx.id}. Error: ${event.err}`;

this.logger.logError(message);

const errPayload = {
errorId: 'xxx',
errorMessage: message,
};

const errorBpiMessage = new BpiMessage(
event.tx.id,
event.tx.fromBpiSubjectAccountId,
event.tx.toBpiSubjectAccountId,
JSON.stringify(errPayload),
event.tx.signature,
BpiMessageType.Err,
);

this.messagingAgent.publishMessage(
event.tx.fromBpiSubjectAccount.ownerBpiSubject.publicKey,
JSON.stringify(errorBpiMessage),
);
}
}
19 changes: 17 additions & 2 deletions examples/bri-3/src/bri/vsm/vsm.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import { TransactionModule } from '../transactions/transactions.module';
import { WorkstepModule } from '../workgroup/worksteps/worksteps.module';
import { VsmTasksSchedulerAgent } from './agents/vsmTaskScheduler.agent';
import { ExecuteVsmCycleCommandHandler } from './capabilites/executeVsmCycle/executeVsmCycleCommand.handler';
import { MessagingAgent } from '../communication/agents/messaging.agent';
import { WorkstepExecutionFailuresHandler } from './capabilites/handleWorkstepFailuresEvents/workstepExecutionFailures.handler';
import { NatsMessagingClient } from '../communication/messagingClients/natsMessagingClient';

export const CommandHandlers = [ExecuteVsmCycleCommandHandler];
export const CommandHandlers = [
ExecuteVsmCycleCommandHandler,
WorkstepExecutionFailuresHandler,
];

export const QueryHandlers = [];

Expand All @@ -19,6 +25,15 @@ export const QueryHandlers = [];
LoggingModule,
WorkstepModule,
],
providers: [VsmTasksSchedulerAgent, ...CommandHandlers, ...QueryHandlers],
providers: [
VsmTasksSchedulerAgent,
...CommandHandlers,
...QueryHandlers,
MessagingAgent,
{
provide: 'IMessagingClient',
useClass: NatsMessagingClient,
},
],
})
export class VsmModule {}

0 comments on commit 16211f1

Please sign in to comment.