Last active
August 29, 2015 14:03
-
-
Save shawndeprey/910ae2417300703aabd0 to your computer and use it in GitHub Desktop.
Threading Pattern for Request Processing in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def index | |
Thread.abort_on_exception = true | |
thr_one = Thread.new do | |
@list_one = ModelOne.find_model_ones_in_elasticsearch(params[:page]) | |
end | |
thr_two = Thread.new do | |
@list_two = ModelTwo.find_model_twos_in_elasticsearch(params[:page]) | |
end | |
# Pause the main thread and allow our threads to execute | |
thr_one.join | |
thr_two.join | |
# Ensure we clean up our threads after processing is finished | |
thr_one.exit | |
thr_two.exit | |
render json: {ones: @list_one, twos: @list_two}, root: false, serializer: SomeSerializer | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment