-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(providers): add provider for generic producer
GH-0
- Loading branch information
1 parent
cfe60bf
commit 5076645
Showing
12 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/__tests__/acceptance/fixtures/producer/generic-producer.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {genericProducer} from '../../../../decorators/generic-producer.decorator'; | ||
import {GenericProducer} from '../../../../types'; | ||
import {GenericStream} from '../stream'; | ||
import {Topics} from '../topics.enum'; | ||
|
||
export class GenericProducerService { | ||
constructor( | ||
@genericProducer(Topics.Generic) | ||
private producer: GenericProducer<GenericStream>, | ||
) {} | ||
|
||
async produceMessage(message: string): Promise<void> { | ||
await this.producer.send([ | ||
{ | ||
data: message, | ||
}, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {inject} from '@loopback/core'; | ||
import {genericProducerKey} from '../keys'; | ||
|
||
export function genericProducer(topic: string) { | ||
return inject(genericProducerKey(topic)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {inject, Provider} from '@loopback/core'; | ||
import {ILogger, LOGGER} from '@sourceloop/core'; | ||
import {CompressionTypes, Kafka, ProducerConfig} from 'kafkajs'; | ||
import {KafkaErrorKeys} from '../error-keys'; | ||
import {GenericProducerFactoryType, IStreamDefinition} from '../types'; | ||
import {KafkaClientBindings} from '../keys'; | ||
|
||
/* The class `GenericKafkaProducerFactoryProvider` is a TypeScript class that provides a factory for creating | ||
Kafka producers to send messages to specified topics without events. */ | ||
export class GenericKafkaProducerFactoryProvider<T extends IStreamDefinition> | ||
implements Provider<GenericProducerFactoryType<T>> | ||
{ | ||
constructor( | ||
@inject(KafkaClientBindings.KafkaClient) | ||
private client: Kafka, | ||
@inject(LOGGER.LOGGER_INJECT) private readonly logger: ILogger, | ||
@inject(KafkaClientBindings.ProducerConfiguration, {optional: true}) | ||
private configuration?: ProducerConfig, | ||
) {} | ||
|
||
value(): GenericProducerFactoryType<T> { | ||
return (topic: string) => { | ||
return { | ||
send: async (payload: T['messages'][], key?: string): Promise<void> => { | ||
const producer = this.client.producer(this.configuration); | ||
|
||
try { | ||
await producer.connect(); | ||
await producer.send({ | ||
topic: topic, | ||
compression: CompressionTypes.GZIP, | ||
messages: payload.map(message => ({ | ||
key, | ||
value: JSON.stringify(message), | ||
})), | ||
}); | ||
await producer.disconnect(); | ||
} catch (e) { | ||
this.logger.error( | ||
`${KafkaErrorKeys.PublishFailed}: ${JSON.stringify(e)}`, | ||
); | ||
await producer.disconnect(); | ||
throw e; | ||
} | ||
}, | ||
}; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './kafka-producer-factory.provider'; | ||
export * from './generic-kafka-producer-factory.provider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters