First request to rails app extremely slow - ruby-on-rails

The first request to my rails app is extremely slow in all environments.
This should not be due different way of caching/loading gems. It was fine two hours ago and no major changes are made.
What I did the hours before I noticed my app turned slow:
I messed around in production.rb (NOT in development.rb): I was playing around with config.serve_static_assets = true
I did a bunch of tasks to diagnose why asset pipeline did not load my stylesheets and images in production (like rake assets:precompile RAILS_ENV=production and rake:clean assets:precompile).
Afterwards I obviously tried to undo all the changes I made, but for some reason my app is now slow in development, while it was perfectly fine before.
How can I fix this?
Thanks in advance :-)
UPDATE 1
When I send a request for localhost:3000, only after 12-13 seconds I receive:
Started GET "/" for ::1 at random time
Rendering behaving is normal. All requests after the first one are fine.
UPDATE 2
In an older version of my application I did the following:
Replace the old 'app'-folder with the newer one
Replace the old 'db'-files with the newer one
Replace the old 'config'-files with the newer one
Everything is running smoothly and still have no clue as to what was wrong in the first place. Please note that the version of the app from yesterday still runs slow, so this is not a non-rails related issue.

Answer
In production on localhost somehow my assets aren't served up (while they do appear in the public directory after a precompile).
In production.rb:
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
was changed to:
config.serve_static_files = true or even
config.serve_static_assets = true.
This also slows the first request down in a development environment apparently

I have this problem too, and i found bottleneck.
Problem in realization OpenSSL::Random.random_bytes on Windows (see this: https://github.com/rails/rails/issues/25805). It used for cookies.
I wrote this solution for my debug machine. But this is very dangerous and not must be used in production
module OpenSSL
module Random
def self.random_bytes(length)
::Random.new.bytes(length)
end
end
end

Related

Rails host app doesn't see engine layout

What I want to do works on my OSX dev machines and on a staging server. However, I encounter problems on the production server.
I have a Rails Engine which I have created, maintained, and used over the years: https://github.com/allesklar/tkh_admin_panel
It works fine on many apps and I have been porting it to this new website as of late. For admin views, my controllers render the 'admin' layout which is located on this engine.
Everything has gone great until I tried to deploy to the site's production server. I get the following error:
ActionView::Template::Error (no implicit conversion of nil into String):
This exception occurs in the line with the following code:
render layout: 'admin'
I can't pinpoint any differences in gems, rbenv ruby versions, or any other factor from one setup to the next.
I've tried to reset the binstubs and done a number of other things.
No matter what I do, it works on the staging server and not on the production server.
Please point me to some directions where I can investigate further.
UPDATE ---
The scope of the problem has changed entirely since now I'm experiencing the same issue on my development machines.
I therefore think the problem lies in the host app/gem relationship. The same version of this gem works fine with some other Rails host sites of mine.
Any ideas welcome.
Try to recompile all assets. If you can (it's a prod server) delete all assets and manifest and re-deploy.
Try to delete all cache, if you are using page or fragment caching, perhaps your deploy system is not cleaning it correctly.
Simply reboot the system (simple action that fixed a lot of problems in the past).
And if nothing works, try to display the full backtrace to see where this is happening

Rails observer causes slow processing time in development mode

I'm on Rails 3.1.1 and I have noticed my app has become exceedingly slow (15 seconds) in development mode. See my firebug 'Net' listing below:
I've done a number of things like:
reducing the number of gems
turning class caching on
turning asset debugging to false
turning asset compression to true
installing the rails-dev-boost gem
Maybe there were some improvements, but nothing helped it to go as fast I'd expect when running off localhost. That is, until I commented out my observers config line in application.rb:
config.active_record.observers = :item_observer, :loan_observer, :friendship_observer, :message_observer, :user_observer
And then the app was fast again (~1 sec) load time. See the firebug listing now:
Other notes:
When in production on Heroku, it's fast (~1 sec), as you'd expect.
I'm using postgresql and Thin; I have not tried using other DBs to see if this problem exists.
When I commented out just the last observer, user_observer, the load time dropped by about half.
The load times printed in development.log do not reflect actual load times. Assets were flagged as 304 Not Modified (0ms) they really took a while to load.
Yes, I'm using the asset pipeline
The Golden Question: Is the simple act of registering observers causing assets to load slowly? And what can be done about it?
Take a look at https://github.com/wavii/rails-dev-tweaks.
Rails is running all of the to_prepare hooks on every Sprockets asset request in development mode. This includes things like auto-(re)loading your code, and various gems perform work in there too. And in your case, observers are being registered (which - I believe - causes Rails to reference a good portion of your app in order to reference the models)
rails-dev-tweaks disables to_prepare & reloading on any asset request (and a few others - read the first part of its README). Speeds up your dev environment by a huge amount for any decently sized project. It's also configurable to do this for any additional requests you like
The way I am fixing this is refactoring the observers into concerns. https://gist.github.com/1014971

Server does not show recent changes

I'm developing a Ruby on Rails project related to semantic technology, and I'm making something basic that allow the uploading of files and searching in those files.
So far it's all working out ok, but I have noticed that when I make changes to my code files or haml files, I don't see those changes on the webserver. Only after either rebooting the server or mashing the F5 button like crazy, the changes come through. And even that is not guaranteed.
The server is running on a local, virtual, ubuntu system. This is an Apache2 webserver configured with Passenger. The website is visibile, it's just not always the latest changes.
Anyone have an idea what might be causing this, or how I can fix this?
In your config/environments/environment_name.rb file it's likely that you have these lines:
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
Which you can switch to have the behavior you want.
If you don't want to change these then you can just touch tmp/restart, which will push the changes through (it's quicker and more graceful then restarting the web server)
To change the environment passenger runs in add the following line to your vhost:
RailsEnv development

Rails production static files routing error

When I run my app locally in test/dev my views come up nicely and everything is happy. When I try to navigate to those same erb files running on my remote server/local production server I get errors like the following:
ActionController::RoutingError (No route matches "/stylesheets/scaffold.css")
I've seen similar questions here on SO but none have been able to solve my problem. The closest thing I've found to an answer is the first answer here: Rails 404 error for Stylesheet or JavaScript files
As I understand it the best thing to do would be to configure my webserver to serve static files. How do I do this locally/on Heroku?
Update
As per raidfive's suggestion I changed config.serve_static_assets from false to true and this fixed my issue. However, I see that it says in production.rb that Apache or nginx should already be serving static assets. Is it any less good/professional to serve static assets in this way and if so how would I achieve the desired results if I'm using Heroku?
Update 2
Apparently Heroku does this automatically, I had an extra comma that was causing the mischief. I was able to look in the extended Heroku logs using the following tip to track down the trouble. Thanks SO!
Are you using Rails 3? By default Rails 3 / webrick does not serve static files in production mode. You can easily enable this by setting
config.serve_static_assets to true in your production.rb file.
In Rails5, you should comment
"config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?"
in config/enviroment/production.rb

Speeding up the rails 3 development server

Now that Rails 3 is out, my favorite dev-mode plugin (rails-dev-boost) is broken. I'm working on a large application (>100 models and controllers) and loading them all every request takes more than ten seconds. I could turn config.cache_classes on, but then I might as well run in production mode since I have to restart the server every change.
Is there something out there that could speed this up?
That plugin owner (rails-dev-boost) is working on getting it running with rails3, so hopefully soon!
I sure wish rails did that kind of stat-optimization out of the box!
https://github.com/thedarkone/rails-dev-boost
Here's the link to the rail3 branch (might disappear if/when the updates are moved into the core release)
https://github.com/thedarkone/rails-dev-boost/tree/rails3

Resources