Comments (2)
aviNrich I apply it this way.
I created a decorator named Requeueable:
export function Requeueable(maxRetries = 3, delayInMillis = 5000) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (...args: any[]) {
try {
await originalMethod.apply(this, args);
} catch (error) {
const message = args[1]; // The message is the second argument to the method
const retryCount = message.properties.headers?.retryCount || 0;
await sleep(delayInMillis * retryCount);
if (retryCount < maxRetries) {
console.error(`Error: ${error.message}, requeueing message. Retry Count: ${retryCount + 1}`);
const amqpConnection: AmqpConnection = this.amqpConnection; // Ensure AmqpConnection is injected into your service
await amqpConnection.publish(
message.fields.exchange,
message.fields.routingKey,
args[0],
{
headers: {
...message.properties.headers,
retryCount: retryCount + 1,
'x-delay': delayInMillis * retryCount,
}
}
);
return new Nack();
} else {
console.error(`Maximum retry count reached. Message will be discarded.`);
return new Nack();
}
}
};
return descriptor;
};
}
And in my service, I use it like this:
const MAX_RETRIES = 50;
const RETRY_DELAY = 2000;
@Injectable()
export class RabbitmqHandler {
constructor(
private readonly amqpConnection: AmqpConnection,
) {}
@RabbitSubscribe({
exchange: 'initiation_exchange',
routingKey: 'ScanReportCreatedEvent',
queue: 'product-creation-queue',
queueOptions: {
durable: true,
channel: 'product-creation-channel',
},
allowNonJsonMessages: true,
createQueueIfNotExists: true,
})
@Requeueable(MAX_RETRIES, RETRY_DELAY)
public async pubSubHandler(msg: {}, amqpMsg: ConsumeMessage) {
throw new Error('test');
}
from nestjs.
@brknesn Thank you for providing your Requeueable decorator
from nestjs.
Related Issues (20)
- rabbitmq: consumerTags are not cleaned up on connection/channel disconnect
- rabbitmq: canceled consumers are resumed if connection/channel reconnects HOT 1
- rabbitmq: direct sending to queue without the need for exchanges HOT 1
- rabbitmq: binding a queue to exchange without a routingKey
- ts-jest: Cannot create mock of a class which has private dependencies in constructor HOT 2
- rabbitmq: handlers didn't works in app-non-pub HOT 1
- golevelup/nestjs-stripe: project can not compile HOT 1
- Stripe Test Webhook HOT 2
- Stripe webhooks - "undefined" is not valid JSON HOT 2
- How should I use this within RabbitSubscribe method?
- ts-vitest: using with `vi.spyOn` isn't working
- golevelup/nestjs-graphql-request: update dependencies [email protected]
- NestJs-rabbitmq Connection loss without reason && No reconnect HOT 2
- Vhost connection HOT 1
- decorators param for Stripe webhook not works HOT 1
- Problem to connect RabbitMQ in Docker HOT 3
- Stripe v15 HOT 3
- Queue subscriber not being registered
- Error when use @RabbitRPC in one controller and listten exchange and queue that same for all actions with diffirent routing keys HOT 7
- Consumer can't receiver message from broker HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestjs.