-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
5781170
commit 49799fb
Showing
12 changed files
with
444 additions
and
10 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
79 changes: 79 additions & 0 deletions
79
twake/backend/node/src/core/platform/services/pubsub/amqp/client.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,79 @@ | ||
import { logger } from "../../../framework/logger"; | ||
import { constants as CONSTANTS } from "./constants"; | ||
import { ConfirmChannel, Replies, Message, Options, ConsumeMessage } from "amqplib"; | ||
|
||
export type AmqpCallbackType = ( | ||
err: Error, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
parsedMessage: any, | ||
originalMessage: ConsumeMessage, | ||
) => void; | ||
|
||
// see http://www.squaremobius.net/amqp.node/ for the amqp documentation | ||
export class AmqpClient { | ||
protected _subscribeCallbackToConsumerTags: Map<AmqpCallbackType, string[]>; | ||
|
||
constructor(protected channel: ConfirmChannel) { | ||
this._subscribeCallbackToConsumerTags = new Map(); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
dispose(): Promise<void> { | ||
logger.info("AMQP: closing the connection"); | ||
|
||
return this.channel.close(); | ||
} | ||
|
||
assertExchange( | ||
exchange: string, | ||
type = CONSTANTS.EXCHANGE_TYPES.topic, | ||
): Promise<Replies.AssertExchange> { | ||
return this.channel.assertExchange(exchange, type); | ||
} | ||
|
||
ack(message: Message, allUpTo = false): void { | ||
this.channel.ack(message, allUpTo); | ||
} | ||
|
||
assertQueue(name: string, options: Options.AssertQueue): Promise<Replies.AssertQueue> { | ||
return this.channel.assertQueue(name, options); | ||
} | ||
|
||
assertBinding(queue: string, exchange: string, routingPattern?: string): Promise<Replies.Empty> { | ||
return this.channel.bindQueue(queue, exchange, routingPattern); | ||
} | ||
|
||
send(exchange: string, data: unknown, routingKey = ""): boolean { | ||
return this.channel.publish(exchange, routingKey, dataAsBuffer(data)); | ||
} | ||
|
||
consume(queue: string, options: Options.Consume, callback: AmqpCallbackType): Promise<void> { | ||
return this.channel | ||
.consume(queue, onMessage, options) | ||
.then(res => this._registerNewConsumerTag(callback, res.consumerTag)); | ||
|
||
function onMessage(originalMessage: ConsumeMessage) { | ||
try { | ||
const message = JSON.parse(originalMessage.content.toString()); | ||
callback(null, message, originalMessage); | ||
} catch (err) { | ||
logger.warn({ err }, "Can not parse the incoming message"); | ||
callback(err, null, originalMessage); | ||
} | ||
} | ||
} | ||
|
||
_registerNewConsumerTag(callback: AmqpCallbackType, consumerTag: string): void { | ||
const sameCallbackTags = this._subscribeCallbackToConsumerTags.get(callback) || []; | ||
|
||
sameCallbackTags.push(consumerTag); | ||
this._subscribeCallbackToConsumerTags.set(callback, sameCallbackTags); | ||
|
||
logger.info(`AMQP: A new consumer has been created: ${consumerTag}`); | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function dataAsBuffer(data: any): Buffer { | ||
return Buffer.from(JSON.stringify(data)); | ||
} |
Oops, something went wrong.