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

Bugfix/detach emitters #11

Merged
merged 2 commits into from
May 13, 2020
Merged
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
38 changes: 24 additions & 14 deletions lib/rmq.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ export class RMQService {
}

public async init(): Promise<void> {
return new Promise((resolve => {
return new Promise((resolve) => {
const connectionURLs: string[] = this.options.connections.map((connection: IRMQConnection) => {
return `amqp://${connection.login}:${connection.password}@${connection.host}`;
});
const connectionOptions = {
reconnectTimeInSeconds: this.options.reconnectTimeInSeconds ?? DEFAULT_RECONNECT_TIME
reconnectTimeInSeconds: this.options.reconnectTimeInSeconds ?? DEFAULT_RECONNECT_TIME,
};
this.server = amqp.connect(connectionURLs, connectionOptions);
this.channel = this.server.createChannel({
Expand All @@ -67,23 +67,30 @@ export class RMQService {
(msg: Message) => {
this.sendResponseEmitter.emit(msg.properties.correlationId, msg);
},
{ noAck: true }
{
noAck: true,
}
);
this.waitForReply();
if (this.options.queueName) {
this.listen(channel);
}
this.logger.log(CONNECTED_MESSAGE);
resolve();
},
});

this.server.on(DISCONNECT_EVENT, err => {
this.server.on('connect', (connection) => {
this.attachEmmitters();
});
this.server.on(DISCONNECT_EVENT, (err) => {
this.detachEmitters();
this.logger.error(DISCONNECT_MESSAGE);
this.logger.error(err.err);
});
}));
});
}

private ack(msg: Message): void {
this.channel.ack(msg);
}

public async send<IMessage, IReply>(topic: string, message: IMessage): Promise<IReply> {
Expand Down Expand Up @@ -120,8 +127,7 @@ export class RMQService {
}

public async disconnect() {
responseEmitter.removeAllListeners();
requestEmitter.removeAllListeners();
this.detachEmitters();
this.sendResponseEmitter.removeAllListeners();
await this.channel.close();
await this.server.close();
Expand All @@ -135,7 +141,7 @@ export class RMQService {
this.queueMeta = Reflect.getMetadata(RMQ_ROUTES_META, RMQService);
this.queueMeta = this.queueMeta ?? [];
if (this.queueMeta.length > 0) {
this.queueMeta.map(async meta => {
this.queueMeta.map(async (meta) => {
await channel.bindQueue(this.options.queueName, this.options.exchangeName, meta.topic);
});
}
Expand All @@ -154,27 +160,31 @@ export class RMQService {
);
}

private waitForReply(): void {
private detachEmitters(): void {
responseEmitter.removeAllListeners();
}

private attachEmmitters(): void {
responseEmitter.on(ResponseEmmiterResult.success, async (msg, result) => {
this.reply(result, msg);
});
responseEmitter.on(ResponseEmmiterResult.error, async (msg, err) => {
this.reply('', msg, err);
});
responseEmitter.on(ResponseEmmiterResult.ack, async (msg) => {
this.channel.ack(msg);
this.ack(msg);
});
}

private async reply(res: any, msg: Message, error: Error | RMQError = null) {
this.channel.ack(msg);
res = await this.intercept(res, msg, error);
await this.channel.sendToQueue(msg.properties.replyTo, Buffer.from(JSON.stringify(res)), {
correlationId: msg.properties.correlationId,
headers: {
...this.buildError(error),
},
});
this.ack(msg);
this.logger.debug(`Sent ▲ [${msg.fields.routingKey}] ${JSON.stringify(res)}`);
}

Expand All @@ -188,7 +198,7 @@ export class RMQService {
}

private isTopicExists(topic: string): boolean {
return !!this.queueMeta.find(x => x.topic === topic);
return !!this.queueMeta.find((x) => x.topic === topic);
}

private async useMiddleware(msg: Message) {
Expand Down