Giter Site home page Giter Site logo

RPC use case about nestjs-rmq HOT 2 CLOSED

alaricode avatar alaricode commented on August 12, 2024
RPC use case

from nestjs-rmq.

Comments (2)

AlariCode avatar AlariCode commented on August 12, 2024

Hello @akalitenya. As I understand your question, you need only one job processed in any route same time, but you messages processed in all routes.
prefetch: 1 will not help, because it will allow you to process only one message in any route.
You have to implement it manually. For example you can queue receiving massages in array and processed them one at a time. But you can't prevent them getting to your function.
Or you can have global static value - CAN_START_MY_METHOD. After getting your first message you can set it to false. Then after processing - true. Example:

import { RMQController, RMQRoute } from 'nestjs-rmq';

@Controller()
@RMQController()
export class MyController {

	CAN_START_MY_METHOD = true;

	@RMQRoute('my.rpc')
	async myMethod(data: myClass): number {
		this.CAN_START_MY_METHOD = false;
		await this.checkAvailability();
		// your code
		this.CAN_START_MY_METHOD = true;
	}

	async checkAvailability() {
		if(this.CAN_START_MY_METHOD === false) {
			await this.delay(1000);
			await this.checkAvailability();
		}	
	}

	async delay(time: number) {
		return new Promise((resolve) => {
			setTimeout(() => {
				resolve();
			}, time);
		});
	}
}

But! Be aware that your sender can get Timeout error if you will have long queues if you have set small timeout. So this is not a good pattern. Use it with caution.

from nestjs-rmq.

akalitenya avatar akalitenya commented on August 12, 2024

Thank you for your help @AlariCode !

I achieved what I need using another library.

from nestjs-rmq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.