impress/app/controllers/pets_controller.rb
Matchu b885ff6ac0 Increase timeout on modeling
Okay so… I'm worried about this because of Rails whole single-threaded situation, which doesn't really let it handle blocking on external network requests very well.

Ultimately I think we're gonna have to do a clever thing but idk quite what?

I should look into whether like, puma + the new async stuff can enable Rails to be more tolerable about this, and handle a few requests at once, instead of having to have the Neopia server doing it. (Right now, the Neopia server isn't really doing its job quite right, because it depends on the Rails app being *local* to send stuff to it.)

But for now, let's just extend the timeout, cuz it's basically always getting hit in production—because there's currently no other way to do modeling, oops lol
2023-10-23 19:05:10 -07:00

93 lines
2.6 KiB
Ruby

class PetsController < ApplicationController
rescue_from Pet::PetNotFound, :with => :pet_not_found
rescue_from PetType::DownloadError, SwfAsset::DownloadError, :with => :asset_download_error
rescue_from Pet::DownloadError, :with => :pet_download_error
protect_from_forgery except: :submit
before_action :local_only, only: :submit
def load
if params[:name] == '!'
redirect_to roulette_path
else
raise Pet::PetNotFound unless params[:name]
@pet = Pet.load(
params[:name],
:item_scope => Item.includes(:translations),
:timeout => 2
)
points = contribute(current_user, @pet)
respond_to do |format|
format.html do
path = destination + @pet.wardrobe_query
redirect_to path
end
format.json do
render :json => {:points => points, :query => @pet.wardrobe_query}
end
end
end
end
def submit
viewer_data = HashWithIndifferentAccess.new(JSON.parse(params[:viewer_data]))
@pet = Pet.from_viewer_data(viewer_data, :item_scope => Item.includes(:translations))
@user = params[:user_id].present? ? User.find(params[:user_id]) : nil
render json: {points: contribute(@user, @pet)}
end
protected
def contribute(user, pet)
if user.present?
points = user.contribute! pet
else
pet.save!
points = true
end
pet.translate_items
points
end
def destination
case (params[:destination] || params[:origin])
when 'wardrobe' then wardrobe_path + '#'
when 'needed_items' then needed_items_path + '?'
else root_path + '#'
end
end
def pet_not_found
pet_load_error :long_message => t('pets.load.not_found'),
:status => :not_found
end
def asset_download_error(e)
Rails.logger.warn e.message
pet_load_error :long_message => t('pets.load.asset_download_error'),
:status => :gateway_timeout
end
def pet_download_error(e)
Rails.logger.warn e.message
Rails.logger.warn e.backtrace.join("\n")
pet_load_error :long_message => t('pets.load.pet_download_error'),
:status => :gateway_timeout
end
def pet_load_error(options)
respond_to do |format|
format.html do
path = params[:origin] || root_path
path += "?name=#{params[:name]}"
redirect_to path, :alert => options[:long_message]
end
format.json do
render :json => options[:long_message], :status => options[:status]
end
end
end
end