-
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.
Separate AbstractAmqpConsumer from AbstractAmqpQueueConsumer (#146)
- Loading branch information
Showing
13 changed files
with
136 additions
and
62 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
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,12 @@ | ||
import { AbstractAmqpConsumer } from './AbstractAmqpConsumer' | ||
import { ensureAmqpQueue } from './utils/amqpQueueUtils' | ||
|
||
export class AbstractAmqpQueueConsumer< | ||
MessagePayloadType extends object, | ||
ExecutionContext, | ||
PrehandlerOutput = undefined, | ||
> extends AbstractAmqpConsumer<MessagePayloadType, ExecutionContext, PrehandlerOutput> { | ||
protected override createMissingEntities(): Promise<void> { | ||
return ensureAmqpQueue(this.connection!, this.channel, this.creationConfig, this.locatorConfig) | ||
} | ||
} |
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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { DeletionConfig } from '@message-queue-toolkit/core' | ||
import { isProduction } from '@message-queue-toolkit/core' | ||
import type { Channel, Connection } from 'amqplib' | ||
|
||
import type { AMQPCreationConfig, AMQPLocator } from '../AbstractAmqpService' | ||
|
||
export async function checkQueueExists(connection: Connection, locatorConfig: AMQPLocator) { | ||
// queue check breaks channel if not successful | ||
const checkChannel = await connection.createChannel() | ||
checkChannel.on('error', () => { | ||
// it's OK | ||
}) | ||
try { | ||
await checkChannel.checkQueue(locatorConfig.queueName) | ||
await checkChannel.close() | ||
} catch (err) { | ||
throw new Error(`Queue with queueName ${locatorConfig.queueName} does not exist.`) | ||
} | ||
} | ||
|
||
export async function ensureAmqpQueue( | ||
connection: Connection, | ||
channel: Channel, | ||
creationConfig?: AMQPCreationConfig, | ||
locatorConfig?: AMQPLocator, | ||
) { | ||
if (creationConfig) { | ||
await channel.assertQueue(creationConfig.queueName, creationConfig.queueOptions) | ||
} else { | ||
if (!locatorConfig) { | ||
throw new Error('locatorConfig is mandatory when creationConfig is not set') | ||
} | ||
await checkQueueExists(connection, locatorConfig) | ||
} | ||
} | ||
|
||
export async function deleteAmqpQueue( | ||
channel: Channel, | ||
deletionConfig: DeletionConfig, | ||
creationConfig: AMQPCreationConfig, | ||
) { | ||
if (!deletionConfig.deleteIfExists) { | ||
return | ||
} | ||
|
||
if (isProduction() && !deletionConfig.forceDeleteInProduction) { | ||
throw new Error( | ||
'You are running autodeletion in production. This can and probably will cause a loss of data. If you are absolutely sure you want to do this, please set deletionConfig.forceDeleteInProduction to true', | ||
) | ||
} | ||
|
||
if (!creationConfig.queueName) { | ||
throw new Error('QueueName must be set for automatic deletion') | ||
} | ||
|
||
await channel.deleteQueue(creationConfig.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
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