Open ponchosb opened 11 months ago
Hi, please check that this.paymentsCalculatorSuccLevTopic
is defined when you create a client createClient(this.paymentsCalculatorSuccLevTopic)
. Seems like in one case it passes undefined
value
That variable is defined in the constructor of the controller and with a getOrThrow. This is the entire controller
import { Controller, Logger } from '@nestjs/common';
import { Ctx, EventPattern, Payload } from '@nestjs/microservices';
import { CalculationResponse, CalculationsService } from './calculations.service';
import { GCPubSubContext } from 'nestjs-google-pubsub-microservice';
import * as process from 'process';
import { MessageService } from '../message/message.service';
import { ConfigService } from '@nestjs/config';
import { Calculation } from '../common/interfaces/payment-strategy.interface';
import { firstValueFrom } from 'rxjs';
@Controller('calculations')
export class CalculationsController {
private readonly paymentsCalculatorSuccLevPattern;
private readonly paymentsCalculatorSuccLevTopic;
constructor(
private calculationService: CalculationsService,
private logger: Logger,
private messageService: MessageService,
private readonly config: ConfigService,
) {
this.paymentsCalculatorSuccLevPattern = this.config.getOrThrow('PAYMENT_CALCULATOR_SUCCESS_LEV_PATTERN');
this.paymentsCalculatorSuccLevTopic = this.config.getOrThrow('PAYMENT_CALCULATOR_SUCCESS_LEV_TOPIC');
}
@EventPattern(process.env.PAYMENT_CALCULATOR_RESULT_PATTERN)
async publish(@Payload() rawInput: Calculation, @Ctx() context: GCPubSubContext): Promise<void> {
const originalMsg = context.getMessage();
originalMsg.ack();
this.logger.log(`Event received: ${JSON.stringify(rawInput)}`, 'Calculation Controller');
const data: CalculationResponse | void = await this.calculationService.createCalculation(rawInput);
if (data && !data.error && data.calculationId) {
try {
await firstValueFrom(
this.messageService
.createClient(this.paymentsCalculatorSuccLevTopic)
.emit(this.paymentsCalculatorSuccLevPattern, data),
);
this.logger.log(
`Event emitted with pattern: ${this.paymentsCalculatorSuccLevPattern} and payload: ${JSON.stringify(data)}`,
'Calculation Controller',
);
} catch (e) {
this.logger.log(
{
context: 'Calculation Controller',
message: `Error emitting event.`,
service: 'Calculation Controller',
component: 'controller',
action: 'event-emited',
pattern: this.paymentsCalculatorSuccLevPattern,
},
'Payment Controller',
);
}
}
}
}
Hi, any comment on this?
I'm experiencing an awkward bug. I've two microservices with the same MessageService class
And then I'm using it in one on my controllers in the next way
In one of the services it's working perfectly but in the other I get all the time the next error:
PubSub client is not connected: topic undefined does not exist
I've confirmed that the topic is there, and that the connection is done and all the configuration is working. Also, if I call the PubSub functions like the example above it works with no problem
Here is a log of the client
The log from the other microservice looks the same but just with different topic and subscription.
Do you know what could be happening?