UNPKG

@message-queue-toolkit/amqp

Version:
86 lines 2.84 kB
import { AbstractQueueService } from '@message-queue-toolkit/core'; export class AbstractAmqpService extends AbstractQueueService { connection; connectionManager; // @ts-ignore channel; isShuttingDown; constructor(dependencies, options) { super(dependencies, options); this.isShuttingDown = false; this.connectionManager = dependencies.amqpConnectionManager; this.connection = this.connectionManager.getConnectionSync(); this.connectionManager.subscribeConnectionReceiver(this); } async receiveNewConnection(connection) { this.connection = connection; this.isShuttingDown = false; // If channel already exists, recreate it const oldChannel = this.channel; try { this.channel = await this.connection.createChannel(); } catch (err) { // @ts-ignore this.logger.error(`Error creating channel: ${err.message}`); await this.connectionManager.reconnect(); return; } if (oldChannel) { this.isShuttingDown = true; try { await oldChannel.close(); } catch { // errors are ok } this.isShuttingDown = false; } this.channel.on('close', () => { if (!this.isShuttingDown) { this.logger.error('AMQP connection lost!'); this.reconnect().catch((err) => { this.handleError(err); throw err; }); } }); this.channel.on('error', (err) => { this.handleError(err); }); await this.createMissingEntities(); } async destroyChannel() { if (this.channel) { try { await this.channel.close(); } catch (_err) { // We don't care about connection closing errors } finally { // @ts-ignore this.channel = undefined; } } } async init() { if (this.creationConfig?.updateAttributesIfExists) { throw new Error('updateAttributesIfExists parameter is not currently supported by the AMQP adapter'); } // if we don't have connection yet, it's fine, we'll wait for a later receiveNewConnection() call if (this.connection) { await this.receiveNewConnection(this.connection); } this.isInitted = true; } async reconnect() { await this.connectionManager.reconnect(); } async close() { this.isShuttingDown = true; await this.destroyChannel(); this.isInitted = false; } } //# sourceMappingURL=AbstractAmqpService.js.map