-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement AMQP publisherManager (#144)
- Loading branch information
Showing
24 changed files
with
619 additions
and
122 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type * as Buffer from 'node:buffer' | ||
|
||
import { objectToBuffer } from '@message-queue-toolkit/core' | ||
import type { Options } from 'amqplib/properties' | ||
|
||
import type { AMQPPublisherOptions } from './AbstractAmqpPublisher' | ||
import { AbstractAmqpPublisher } from './AbstractAmqpPublisher' | ||
import type { AMQPDependencies } from './AbstractAmqpService' | ||
|
||
export type AMQPExchangePublisherOptions<MessagePayloadType extends object> = Omit< | ||
AMQPPublisherOptions<MessagePayloadType>, | ||
'creationConfig' | ||
> & { | ||
exchange: string | ||
} | ||
|
||
export type AmqpExchangeMessageOptions = { | ||
routingKey: string | ||
publishOptions: Options.Publish | ||
} | ||
|
||
export abstract class AbstractAmqpExchangePublisher< | ||
MessagePayloadType extends object, | ||
> extends AbstractAmqpPublisher<MessagePayloadType, AmqpExchangeMessageOptions> { | ||
constructor( | ||
dependencies: AMQPDependencies, | ||
options: AMQPExchangePublisherOptions<MessagePayloadType>, | ||
) { | ||
super(dependencies, { | ||
...options, | ||
// FixMe exchange publisher doesn't need queue at all | ||
creationConfig: { | ||
queueName: 'dummy', | ||
queueOptions: {}, | ||
updateAttributesIfExists: false, | ||
}, | ||
exchange: options.exchange, | ||
locatorConfig: undefined, | ||
}) | ||
} | ||
|
||
protected publishInternal(message: Buffer, options: AmqpExchangeMessageOptions): void { | ||
this.channel.publish( | ||
this.exchange!, | ||
options.routingKey, | ||
objectToBuffer(message), | ||
options.publishOptions, | ||
) | ||
} | ||
} |
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,23 @@ | ||
import type { Options } from 'amqplib/properties' | ||
|
||
import { AbstractAmqpPublisher } from './AbstractAmqpPublisher' | ||
|
||
export type AmqpQueueMessageOptions = { | ||
publishOptions: Options.Publish | ||
} | ||
|
||
const NO_PARAMS: AmqpQueueMessageOptions = { | ||
publishOptions: {}, | ||
} | ||
|
||
export abstract class AbstractAmqpQueuePublisher< | ||
MessagePayloadType extends object, | ||
> extends AbstractAmqpPublisher<MessagePayloadType, AmqpQueueMessageOptions> { | ||
protected publishInternal(message: Buffer, options: AmqpQueueMessageOptions): void { | ||
this.channel.sendToQueue(this.queueName, message, options.publishOptions) | ||
} | ||
|
||
publish(message: MessagePayloadType, options: AmqpQueueMessageOptions = NO_PARAMS) { | ||
super.publish(message, options) | ||
} | ||
} |
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,93 @@ | ||
import { AbstractPublisherManager } from '@message-queue-toolkit/core' | ||
import type { MessageMetadataType } from '@message-queue-toolkit/core/lib/messages/baseMessageSchemas' | ||
import type { TypeOf } from 'zod' | ||
import type z from 'zod' | ||
|
||
import type { | ||
AbstractAmqpExchangePublisher, | ||
AMQPExchangePublisherOptions, | ||
} from './AbstractAmqpExchangePublisher' | ||
import type { AmqpQueueMessageOptions } from './AbstractAmqpQueuePublisher' | ||
import type { AMQPCreationConfig, AMQPDependencies, AMQPLocator } from './AbstractAmqpService' | ||
import type { | ||
AmqpAwareEventDefinition, | ||
AmqpMessageSchemaType, | ||
AmqpPublisherManagerDependencies, | ||
AmqpPublisherManagerOptions, | ||
} from './AmqpQueuePublisherManager' | ||
import { CommonAmqpExchangePublisherFactory } from './CommonAmqpPublisherFactory' | ||
|
||
export class AmqpExchangePublisherManager< | ||
T extends AbstractAmqpExchangePublisher<z.infer<SupportedEventDefinitions[number]['schema']>>, | ||
SupportedEventDefinitions extends AmqpAwareEventDefinition[], | ||
MetadataType = MessageMetadataType, | ||
> extends AbstractPublisherManager< | ||
AmqpAwareEventDefinition, | ||
NonNullable<SupportedEventDefinitions[number]['exchange']>, | ||
AbstractAmqpExchangePublisher<z.infer<SupportedEventDefinitions[number]['schema']>>, | ||
AMQPDependencies, | ||
AMQPCreationConfig, | ||
AMQPLocator, | ||
AmqpMessageSchemaType<AmqpAwareEventDefinition>, | ||
Omit< | ||
AMQPExchangePublisherOptions<z.infer<SupportedEventDefinitions[number]['schema']>>, | ||
'messageSchemas' | 'locatorConfig' | 'exchange' | ||
>, | ||
SupportedEventDefinitions, | ||
MetadataType, | ||
z.infer<SupportedEventDefinitions[number]['schema']> | ||
> { | ||
constructor( | ||
dependencies: AmqpPublisherManagerDependencies<SupportedEventDefinitions>, | ||
options: AmqpPublisherManagerOptions< | ||
T, | ||
AmqpQueueMessageOptions, | ||
AMQPExchangePublisherOptions<z.infer<SupportedEventDefinitions[number]['schema']>>, | ||
z.infer<SupportedEventDefinitions[number]['schema']>, | ||
MetadataType | ||
>, | ||
) { | ||
super({ | ||
isAsync: false, | ||
eventRegistry: dependencies.eventRegistry, | ||
metadataField: options.metadataField ?? 'metadata', | ||
metadataFiller: options.metadataFiller, | ||
newPublisherOptions: options.newPublisherOptions, | ||
publisherDependencies: { | ||
amqpConnectionManager: dependencies.amqpConnectionManager, | ||
logger: dependencies.logger, | ||
errorReporter: dependencies.errorReporter, | ||
}, | ||
publisherFactory: options.publisherFactory ?? new CommonAmqpExchangePublisherFactory(), | ||
}) | ||
} | ||
|
||
protected resolvePublisherConfigOverrides( | ||
exchange: string, | ||
): Partial< | ||
Omit< | ||
AMQPExchangePublisherOptions<TypeOf<SupportedEventDefinitions[number]['schema']>>, | ||
'messageSchemas' | 'locatorConfig' | ||
> | ||
> { | ||
return { | ||
exchange, | ||
} | ||
} | ||
|
||
protected override resolveCreationConfig( | ||
queueName: NonNullable<SupportedEventDefinitions[number]['exchange']>, | ||
): AMQPCreationConfig { | ||
return { | ||
...this.newPublisherOptions, | ||
queueOptions: {}, | ||
queueName, | ||
} | ||
} | ||
|
||
protected override resolveEventTarget( | ||
event: AmqpAwareEventDefinition, | ||
): NonNullable<SupportedEventDefinitions[number]['exchange']> | undefined { | ||
return event.queueName | ||
} | ||
} |
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,34 @@ | ||
import type { AwilixContainer } from 'awilix' | ||
import { beforeAll } from 'vitest' | ||
|
||
import { FakeConsumer } from '../test/fakes/FakeConsumer' | ||
import { TEST_AMQP_CONFIG } from '../test/utils/testAmqpConfig' | ||
import { registerDependencies, TestEvents } from '../test/utils/testContext' | ||
import type { Dependencies } from '../test/utils/testContext' | ||
|
||
describe('AmqpQueuePublisherManager', () => { | ||
describe('publish', () => { | ||
let diContainer: AwilixContainer<Dependencies> | ||
beforeAll(async () => { | ||
diContainer = await registerDependencies(TEST_AMQP_CONFIG) | ||
}) | ||
|
||
it('publishes to the correct queue', async () => { | ||
const { queuePublisherManager } = diContainer.cradle | ||
const fakeConsumer = new FakeConsumer(diContainer.cradle, TestEvents.updated) | ||
await fakeConsumer.start() | ||
|
||
const publishedMessage = await queuePublisherManager.publish(FakeConsumer.QUEUE_NAME, { | ||
...queuePublisherManager.resolveBaseFields(), | ||
type: 'entity.updated', | ||
payload: { | ||
updatedData: 'msg', | ||
}, | ||
}) | ||
|
||
const result = await fakeConsumer.handlerSpy.waitForMessageWithId(publishedMessage.id) | ||
|
||
expect(result.processingResult).toBe('consumed') | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.