Reputation: 1435
I have an API built with Sinatra.
When a user logs in, the application checks the login and password and sends a jwt_refresh_token in a cookie to the client allowing the user to remain logged in.
I'm looking to implement various methods related to the user status, but that don't need to delay the user login (i.e. remove obsolete data from the database) so subsequent api call provide the correct answer.
How do I implement this in Sinatra or Ruby? To be honest, I don't even know how this is called in English, which makes looking it up pretty complicated.
Upvotes: 1
Views: 30
Reputation: 4197
I am not sure of the action you want to perform but assuming that some of it is kind of network calls, one way to go about it would be use some sort of background worker.
In any case weather you invoke via workers or web controller you can make use of Threads to make parallel invokations.
For example consider the code below which creates 3 threads and waits up on each one to finish.
require 'net/http'
def get(url)
uri = URI(url)
response = Net::HTTP.get_response(uri)
puts response.body
end
threads = []
threads << Thread.new {
# assume that this is call to update the user
get('https://jsonplaceholder.typicode.com/todos/1')
}
threads << Thread.new {
# assume that this is call to delete something get('https://jsonplaceholder.typicode.com/posts/2')
}
threads << Thread.new {
# some more network call
get('https://jsonplaceholder.typicode.com/posts/3')
}
threads.each(&:join)
puts "All good let's exit"
Threads are a bit of convoluted topic so you may want to research a bit. For the starters you could refer to something like - https://www.rubyguides.com/2015/07/ruby-threads/
Upvotes: 1