Skip to content

Commit bb7e9d7

Browse files
committed
feat(microservices): add noAssert option for RMQ connection
Rabbit MQ brokers may not allow for queue declaration, so a check is needed to avoid 403 errors in that scenario
1 parent 83098e2 commit bb7e9d7

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

packages/microservices/client/client-rmq.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
RQM_DEFAULT_QUEUE,
1717
RQM_DEFAULT_QUEUE_OPTIONS,
1818
RQM_DEFAULT_URL,
19+
RQM_DEFAULT_NO_ASSERT,
1920
} from '../constants';
2021
import { RmqUrl } from '../external/rmq-url.interface';
2122
import { ReadPacket, RmqOptions, WritePacket } from '../interfaces';
@@ -38,6 +39,7 @@ export class ClientRMQ extends ClientProxy {
3839
protected responseEmitter: EventEmitter;
3940
protected replyQueue: string;
4041
protected persistent: boolean;
42+
protected noAssert: boolean;
4143

4244
constructor(protected readonly options: RmqOptions['options']) {
4345
super();
@@ -51,6 +53,9 @@ export class ClientRMQ extends ClientProxy {
5153
this.getOptionsProp(this.options, 'replyQueue') || REPLY_QUEUE;
5254
this.persistent =
5355
this.getOptionsProp(this.options, 'persistent') || RQM_DEFAULT_PERSISTENT;
56+
this.noAssert =
57+
this.getOptionsProp(this.options, 'noAssert') || RQM_DEFAULT_NO_ASSERT;
58+
5459
loadPackage('amqplib', ClientRMQ.name, () => require('amqplib'));
5560
rqmPackage = loadPackage('amqp-connection-manager', ClientRMQ.name, () =>
5661
require('amqp-connection-manager'),
@@ -143,7 +148,9 @@ export class ClientRMQ extends ClientProxy {
143148
this.getOptionsProp(this.options, 'isGlobalPrefetchCount') ||
144149
RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT;
145150

146-
await channel.assertQueue(this.queue, this.queueOptions);
151+
if (!this.queueOptions.noAssert) {
152+
await channel.assertQueue(this.queue, this.queueOptions);
153+
}
147154
await channel.prefetch(prefetchCount, isGlobalPrefetchCount);
148155

149156
this.responseEmitter = new EventEmitter();

packages/microservices/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT = false;
3636
export const RQM_DEFAULT_QUEUE_OPTIONS = {};
3737
export const RQM_DEFAULT_NOACK = true;
3838
export const RQM_DEFAULT_PERSISTENT = false;
39+
export const RQM_DEFAULT_NO_ASSERT = false;
3940
export const GRPC_DEFAULT_PROTO_LOADER = '@grpc/proto-loader';
4041

4142
export const NO_MESSAGE_HANDLER = `There is no matching message handler defined in the remote service.`;

packages/microservices/interfaces/microservice-configuration.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export interface RmqOptions {
184184
replyQueue?: string;
185185
persistent?: boolean;
186186
headers?: Record<string, string>;
187+
noAssert?: boolean;
187188
};
188189
}
189190

packages/microservices/server/server-rmq.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
RQM_DEFAULT_QUEUE,
1717
RQM_DEFAULT_QUEUE_OPTIONS,
1818
RQM_DEFAULT_URL,
19+
RQM_DEFAULT_NO_ASSERT,
1920
} from '../constants';
2021
import { RmqContext } from '../ctx-host';
2122
import { Transport } from '../enums';
@@ -40,6 +41,7 @@ export class ServerRMQ extends Server implements CustomTransportStrategy {
4041
protected readonly prefetchCount: number;
4142
protected readonly queueOptions: any;
4243
protected readonly isGlobalPrefetchCount: boolean;
44+
protected readonly noAssert: boolean;
4345

4446
constructor(protected readonly options: RmqOptions['options']) {
4547
super();
@@ -55,6 +57,8 @@ export class ServerRMQ extends Server implements CustomTransportStrategy {
5557
this.queueOptions =
5658
this.getOptionsProp(this.options, 'queueOptions') ||
5759
RQM_DEFAULT_QUEUE_OPTIONS;
60+
this.noAssert =
61+
this.getOptionsProp(this.options, 'noAssert') || RQM_DEFAULT_NO_ASSERT;
5862

5963
this.loadPackage('amqplib', ServerRMQ.name, () => require('amqplib'));
6064
rqmPackage = this.loadPackage(
@@ -115,7 +119,9 @@ export class ServerRMQ extends Server implements CustomTransportStrategy {
115119
public async setupChannel(channel: any, callback: Function) {
116120
const noAck = this.getOptionsProp(this.options, 'noAck', RQM_DEFAULT_NOACK);
117121

118-
await channel.assertQueue(this.queue, this.queueOptions);
122+
if (!this.queueOptions.noAssert) {
123+
await channel.assertQueue(this.queue, this.queueOptions);
124+
}
119125
await channel.prefetch(this.prefetchCount, this.isGlobalPrefetchCount);
120126
channel.consume(
121127
this.queue,

0 commit comments

Comments
 (0)