Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(microservices): handle kafka consumer crashes #11910

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/microservices/client/client-kafka.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Logger } from '@nestjs/common/services/logger.service';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { isUndefined } from '@nestjs/common/utils/shared.utils';

import {
KAFKA_DEFAULT_BROKER,
KAFKA_DEFAULT_CLIENT,
Expand All @@ -15,8 +16,10 @@ import {
ConsumerConfig,
ConsumerGroupJoinEvent,
EachMessagePayload,
InstrumentationEvent,
Kafka,
KafkaConfig,
KafkaJSError,
KafkaMessage,
Producer,
TopicPartitionOffsetAndMetadata,
Expand Down Expand Up @@ -142,6 +145,10 @@ export class ClientKafka extends ClientProxy {
this.consumer.events.GROUP_JOIN,
this.setConsumerAssignments.bind(this),
);
this.consumer.on(
this.consumer.events.CRASH,
this.handleConsumerCrash,
);
await this.consumer.connect();
await this.bindTopics();
}
Expand Down Expand Up @@ -330,4 +337,18 @@ export class ClientKafka extends ClientProxy {
throw new Error('No consumer initialized');
}
}

private handleConsumerCrash(
event: InstrumentationEvent<{
error: KafkaJSError;
groupId: string;
restart: boolean;
}>,
) {
const { error, groupId, restart } = event.payload;

if (!restart) {
throw new Error('Consumer crashed');
}
}
}
19 changes: 19 additions & 0 deletions packages/microservices/server/server-kafka.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Logger } from '@nestjs/common/services/logger.service';
import { isNil } from '@nestjs/common/utils/shared.utils';
import { isObservable, lastValueFrom, Observable, ReplaySubject } from 'rxjs';

import {
KAFKA_DEFAULT_BROKER,
KAFKA_DEFAULT_CLIENT,
Expand All @@ -17,8 +18,10 @@ import {
Consumer,
ConsumerConfig,
EachMessagePayload,
InstrumentationEvent,
Kafka,
KafkaConfig,
KafkaJSError,
KafkaMessage,
Message,
Producer,
Expand Down Expand Up @@ -103,6 +106,8 @@ export class ServerKafka extends Server implements CustomTransportStrategy {
this.consumer = this.client.consumer(consumerOptions);
this.producer = this.client.producer(this.options.producer);

this.consumer.on(this.consumer.events.CRASH, this.handleConsumerCrash);

await this.consumer.connect();
await this.producer.connect();
await this.bindEvents(this.consumer);
Expand Down Expand Up @@ -317,4 +322,18 @@ export class ServerKafka extends Server implements CustomTransportStrategy {
protected initializeDeserializer(options: KafkaOptions['options']) {
this.deserializer = options?.deserializer ?? new KafkaRequestDeserializer();
}

private handleConsumerCrash(
event: InstrumentationEvent<{
error: KafkaJSError;
groupId: string;
restart: boolean;
}>,
) {
const { error, groupId, restart } = event.payload;

if (!restart) {
throw new Error('Consumer crashed');
}
}
}
4 changes: 3 additions & 1 deletion packages/microservices/test/client/client-kafka.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import * as sinon from 'sinon';

import { ClientKafka } from '../../client/client-kafka';
import { NO_MESSAGE_HANDLER } from '../../constants';
import { KafkaHeaders } from '../../enums';
Expand Down Expand Up @@ -176,6 +177,7 @@ describe('ClientKafka', () => {
run,
events: {
GROUP_JOIN: 'consumer.group_join',
CRASH: 'consumer.crash',
},
on,
};
Expand Down Expand Up @@ -288,7 +290,7 @@ describe('ClientKafka', () => {

expect(consumerStub.calledOnce).to.be.true;

expect(on.calledOnce).to.be.true;
expect(on.calledTwice).to.be.true;
expect(client['consumerAssignments']).to.be.empty;

expect(connect.calledTwice).to.be.true;
Expand Down
7 changes: 7 additions & 0 deletions packages/microservices/test/server/server-kafka.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Logger } from '@nestjs/common';
import { AssertionError, expect } from 'chai';
import * as sinon from 'sinon';

import { NO_MESSAGE_HANDLER } from '../../constants';
import { KafkaHeaders } from '../../enums';
import {
Expand Down Expand Up @@ -90,6 +91,7 @@ describe('ServerKafka', () => {
let subscribe: sinon.SinonSpy;
let run: sinon.SinonSpy;
let send: sinon.SinonSpy;
let on: sinon.SinonSpy;
let consumerStub: sinon.SinonStub;
let producerStub: sinon.SinonStub;
let client;
Expand All @@ -101,12 +103,17 @@ describe('ServerKafka', () => {
subscribe = sinon.spy();
run = sinon.spy();
send = sinon.spy();
on = sinon.spy();

consumerStub = sinon.stub(server as any, 'consumer').callsFake(() => {
return {
connect,
subscribe,
run,
events: {
CRASH: 'consumer.crash',
},
on,
};
});
producerStub = sinon.stub(server as any, 'producer').callsFake(() => {
Expand Down