Skip to content

Commit 07ed4cb

Browse files
committed
refactor handlers
1 parent ba37cad commit 07ed4cb

File tree

4 files changed

+19
-60
lines changed

4 files changed

+19
-60
lines changed

docs/KAFKA_SETUP.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ docker compose -f docker-compose.kafka.yml ps
1515
```
1616

1717
This will start:
18+
1819
- **Zookeeper** on port 2181
1920
- **Kafka** on port 9092
2021
- **Kafka UI** on port 8080 (web interface)
@@ -40,6 +41,7 @@ pnpm run start:dev
4041
```
4142

4243
The application will automatically:
44+
4345
- Connect to Kafka on startup
4446
- Subscribe to registered topics
4547
- Start consuming messages
@@ -80,9 +82,13 @@ docker exec -it kafka kafka-console-consumer --topic avscan.action.scan --from-b
8082
### Adding New Event Handlers
8183

8284
1. Create a new handler class extending `BaseEventHandler`:
85+
8386
```typescript
8487
@Injectable()
85-
export class MyCustomHandler extends BaseEventHandler implements OnModuleInit {
88+
export class MyCustomHandler
89+
extends BaseEventHandler
90+
implements OnModuleInit
91+
{
8692
private readonly topic = 'my.custom.topic';
8793

8894
constructor(private readonly handlerRegistry: KafkaHandlerRegistry) {
@@ -103,14 +109,15 @@ docker exec -it kafka kafka-console-consumer --topic avscan.action.scan --from-b
103109
}
104110
```
105111

106-
2. Register the handler in the KafkaModule providers array
107-
3. The handler will automatically be registered and start consuming messages
112+
2. Register the handler in the `src/shared/modules/kafka/handlers/registered-handlers.config.ts` config handlers array.
113+
3. The handler will automatically be registered and start consuming messages.
108114

109115
### Dead Letter Queue (DLQ) Support
110116

111117
The application includes a robust Dead Letter Queue implementation for handling message processing failures:
112118

113119
1. **Configuration**:
120+
114121
```
115122
# DLQ Configuration in .env
116123
KAFKA_DLQ_ENABLED=true
@@ -119,11 +126,13 @@ The application includes a robust Dead Letter Queue implementation for handling
119126
```
120127

121128
2. **Retry Mechanism**:
129+
122130
- Failed messages are automatically retried up to the configured maximum number of retries
123131
- Retry count is tracked per message using a unique key based on topic, partition, and offset
124132
- Exponential backoff is applied between retries
125133

126134
3. **DLQ Processing**:
135+
127136
- After exhausting retries, messages are sent to a DLQ topic (original topic name + configured suffix)
128137
- DLQ messages include:
129138
- Original message content
@@ -133,6 +142,7 @@ The application includes a robust Dead Letter Queue implementation for handling
133142
- Original message headers
134143

135144
4. **Monitoring DLQ**:
145+
136146
- Use Kafka UI to monitor DLQ topics (they follow the pattern `<original-topic>.dlq`)
137147
- Check application logs for messages with "Message sent to DLQ" or "Failed to send message to DLQ"
138148

@@ -185,4 +195,4 @@ docker compose -f docker-compose.kafka.yml down -v
185195
- Messages in DLQ topics
186196
- High retry rates
187197
- Consumer failures
188-
- Implement a process for reviewing and potentially reprocessing DLQ messages
198+
- Implement a process for reviewing and potentially reprocessing DLQ messages

src/shared/modules/kafka/handlers/avscan-action-scan.handler.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { SubmissionScanCompleteHandler } from './submission-scan-complete.handler';
2+
3+
export default [SubmissionScanCompleteHandler];

src/shared/modules/kafka/kafka.module.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import {
44
KafkaModuleOptions,
55
} from './kafka-consumer.service';
66
import { KafkaHandlerRegistry } from './kafka-handler.registry';
7-
import { AVScanActionScanHandler } from './handlers/avscan-action-scan.handler';
8-
import { SubmissionScanCompleteHandler } from './handlers/submission-scan-complete.handler';
7+
import registeredHandlersConfig from './handlers/registered-handlers.config';
98

109
@Module({})
1110
export class KafkaModule {
@@ -25,8 +24,7 @@ export class KafkaModule {
2524
},
2625
inject: [KafkaHandlerRegistry],
2726
},
28-
AVScanActionScanHandler,
29-
SubmissionScanCompleteHandler,
27+
...registeredHandlersConfig,
3028
],
3129
exports: [KafkaConsumerService, KafkaHandlerRegistry],
3230
};

0 commit comments

Comments
 (0)