Let's understand the scenario, I have to call a third party API in one of the API calls to my server (let say A) from the mobile app. That third party API takes almost 12-15 seconds to execute. However, in the next consecutive API call to my server (let say B), I need to have the response of that third party API before start processing.
Here, It's important for me to call that third party API in a new thread in the first API call to my server i.e. A. So that I can efficiently use the processing time for my second API i.e. B as the mobile user has to give certain inputs b/w these two API's A & B.
The solution I have in my mind is that I will hit the third party API in the new thread, will save the thread reference in a variable and pass it in the response of my first API call like
@th_ref = Thread.new do
// serive for third party api
end
json.thread_ref @th_ref
In the next API call i.e. B, I will get this reference in a body and then I will join my main thread with the previous thread so that B does not start work until I get the third party API response.
But with the above solution, I get the thread_ref in String class in the second API's body like
"#<Thread:0x00007fc84e316e38@(pry):157 run>"
and I have to change it back to Thread class so that I can use functions like thread_ref.join
or thread_ref.alive?
Kindly suggest how can I achieve this conversion and am I using the right approach for my problem?