H
H
Hidralisk2020-06-22 17:10:23
Java
Hidralisk, 2020-06-22 17:10:23

Access from multiple instances to a method?

Hello.
There is such a question.
There are 2 (or many) application instances. There is a service with some kind of conditional method run ()
run () launches an asynchronous task and returns some kind of response. run() twitches through the controller.
The task is to run run() once for multiple identical incoming requests .. When repeated, respectively, give the response status "in progress" or "such a request has already been completed".
How to correctly synchronize this method between many instances?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-06-23
@Robur

Save the state in run and check when you call it.
For example like this:

async run() {
     if (this.isRunning) return 'in process'
     this.isRunning = true
     await doTask()
     this.isRunning = false
}

You can use a promise in isRunning and return it, then everyone who calls run () will be able to wait for the result of the task.
at the end of isRunning , or null if run() is called many times, or leave it like that, then all subsequent calls will return the same result even after the request has long been completed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question