In Ruby, I have a controller action that immediately initiates a computationally-intensive task that takes several seconds. I want the client to poll my server and get status updates.
in my controller:
def complex_task
Thread.new do
loop do
one_part_of_the_computationally_intensive_task
# Note how much progress we've made, in case the client asks.
save_progress
end
end
# returns to client straightaway, but task continues in background.
end
def check_status
# Retrieve state in here.
get_progress
end
Note how check_status
needs to pull state that is stored by the separate thread.
How do I write the save_progress
and get_progress
functions? Where do I store the state? It seems like it isn't a good idea to store it in session
, since this gets sent to the client, but where else do I store it?
(related to this question, but much more general. I expect quite different answers.)