Why Rails response takes one minute in development mode? - ruby-on-rails

Can't understand why my rails app become make response ultra-slow - in 1 minute!
As i see total time Rails should be: Views 0.2ms + ActiveRecord 219.5ms + Solr 379.7ms = 599.4ms
But it takes 62615ms, where is it spending the rest of time 62015.6ms?
Started POST "/applications/135" for ::1 at 2019-08-05 17:59:04 +0300
Processing by ApplicationController#create as JS
...
Completed 200 OK in 62615ms (Views: 0.2ms | ActiveRecord: 219.5ms | Solr: 379.7ms)
config/environments/development.rb:
# frozen_string_literal: true
require 'sidekiq/testing/inline'
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# Verifies that versions and hashed value of the package contents in the project's package.json
config.webpacker.check_yarn_integrity = false
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = false
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# SMTP configuration
config.action_mailer.default_url_options = {
host: ENV['HOST']
}
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.perform_deliveries = true
# Care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
end

Found cause of slow down.
Somebody of our team added delayed service into the controller.
After commenting #require 'sidekiq/testing/inline' in the config/environments/development.rb and running separate sidekiq -C config/sidekiq.yml it solved the problem, now service is running asynchronously.
But problem was inside the service where was the call RestClient.post(endpoint, payload.to_json, 'api-key': api_key) which was wrapped into begin rescue end block that catch the error (that raises in a minute by RestClient timeout).
Completed 401 Unauthorized in 62562ms (ActiveRecord: 191.5ms)
RestClient::Exceptions::ReadTimeout (Timed out reading data from server):

Related

rails app not connecting to redis on heroku (RedisToGo)

am trying to push my rails 4 app on heroku but getting this error:
Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED) (Redis::CannotConnectError)
on heroku logs, i have completed the RedisToGo setup as suggested by the heroku devcenter
but nothing happened i think am doing something wrong with RedisToGo URL
redis.rb
uri = URI.parse(ENV["REDIS_URL"] || "redis://redistogo:0deb5da103a95090a365444d016c59a6#angelfish.redistogo.com:9308/" )
$redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
my development.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Action Mailer configuration
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
ENV["REDISTOGO_URL"] = 'redis://redistogo:0deb5da103a95090a365444d016c59a6#angelfish.redistogo.com:9308/'
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# React configurations.
config.react.variant = :development
config.react.addons = true
config.after_initialize do
Bullet.enable = true
Bullet.alert = true
end
end

Rails 4.2 app very slow in development

I've been banging my head against the wall on that one for a couple of days now.
My app is running super slow in development mode. A good amount of time is spent on serving the assets (especially images). Each request is extremely slow, taking up to 7s to load a simple 50kb jpeg. Total page load time can be up to a couple of minutes...
I suspect there is a link with auto-loading of the code, since when I turn on classes caching my stuff is loaded much much faster (a few hundred ms maximum). That's not a solution though, since I need to be coding, I can't restart the server everytime I make a change.
Here's my config/development.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.reload_classes_only_on_change = true
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = false
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
config.action_mailer.default_url_options = { host: '127.0.0.1', port: 3000 }
# email sending process
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :test
# Defaults to:
# config.action_mailer.sendmail_settings = {
# location: '/usr/sbin/sendmail',
# arguments: '-i -t'
# }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'no-reply#example.com'}
config.web_console.whitelisted_ips = '0.0.0.0/0.0.0.0'
end
and initializers/assets.rb
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# Rails.application.config.assets.precompile += %w( jquery.Jcrop.min.scss )
# Rails.application.config.assets.precompile += %w( jquery.Jcrop.min.js )
# Rails.application.config.assets.precompile += %w( flat-ui.css.bak )
# Rails.application.config.assets.precompile += %w( bootsrap.css.map)
# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path
# Precompile additional assets.
# application.js, application.scss, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )
Dir.glob("#{Rails.root}/app/assets/images/**/").each do |path|
Rails.application.config.assets.paths << path
end
Any insight would be much appreciated!
Many issues can happens in your application, for example it can be N+1 queries or some slow code, HTTP queries into other site or big assets bundle. So I suggest use profiler it will show bottleneck in your code. See list popular profilers below:
https://github.com/glassechidna/stackprofiler
https://github.com/ruby-prof/ruby-prof
Other helpful links you can find here:
https://awesome-ruby.com/#-profiler-and-optimization

Session ID changes when I make a POST request in Rails but only during tests

I'm having a rather odd issue in that POST requests triggered by link_to with method: :post. In the test environment the session_id seems to change. This causes issues such as the current_user object doesn't exist within the action I'm posting too. I've logged out the request and the session info and I can see that the session has changed for the POST action and when I try to use current_user the test fails.
I have other POST requests through forms around the app. And they work fine. It seems to be some Rails magic around the link_to with method: :post and passing the CSRF token.
I'm able to get around this by changing my test.rb to be the same as my development.rb. But I'm sure this isn't a good solution. It's possible it's related to some configuration but it seems like this is the default behavior.
Controller
class RecruitersController < ApplicationController
before_action -> { STDOUT.puts "Request: #{request.method} #{request.fullpath}" }
before_action -> { STDOUT.puts "Session: #{session[:session_id]}" }
...
end
Button that triggers the POST Request
= link_to "<3", recruiter_request_url(id: recruiter.id), method: :post, remote: true
Output in Tests
Request: GET /recruiters/dashboard
Session: ee8c577fdf6d1714c2a837f0890e0294
Request: GET /recruiters/premium
Session: ee8c577fdf6d1714c2a837f0890e0294
Request: POST /recruiters/request_premium_trial/1
Session: 314c6eef0156aa36a469a4f9ea7513a8
Output in Development
Request: GET /recruiters/dashboard
Session: cdb333efb5d62e6ddbb5914c8edd7a92
Request: GET /recruiters/premium
Session: cdb333efb5d62e6ddbb5914c8edd7a92
Request: POST /recruiters/request_premium_trial/1
Session: cdb333efb5d62e6ddbb5914c8edd7a92
The Spec
Simple spec user signs in goes to the dashboard, goes to the premium page and then clicks on the link that makes the POST request.
scenario 'Should be able to make request', js:true do
rsign_in # Function that simulates sign in
click_on 'Premium'
click_on '<3'
assert_text 'Request made' # Fails as we're redirected to sign in page when we try to authenticate the user
end
Test.rb
Rails.application.configure do
# Set log level
config.log_level = :debug
# This means that all URLs need to have 5 parts to them. This is for http://recruiter.127.0.0.1.xip.io:3000
config.action_dispatch.tld_length = 5
# Settings specified here will take precedence over those in config/application.rb.
cache_store = :file_store, "tmp/cache"
# Use a different job queue
config.active_job.queue_adapter = Figaro.env.job_queue.to_sym if Figaro.env.job_queue?
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = true
# Configure static asset server for tests with Cache-Control for performance.
config.serve_static_files = true
config.static_cache_control = 'public, max-age=3600'
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false
# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false
# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_controller.default_url_options = { host: 'localhost:5000' }
config.action_mailer.default_url_options = { host: 'localhost:5000' }
config.action_mailer.delivery_method = :test
# config.action_mailer.delivery_method = :smtp
# config.action_mailer.smtp_settings = { address: 'localhost', port: '1025' }
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
config.active_record.raise_in_transactional_callbacks = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
Development.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.preview_path = "#{Rails.root}/app/mailers/previews"
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
config.action_mailer.default_url_options = { host: 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { address: 'localhost', port: '1025' }
config.cache_store = :dalli_store
# Use a different job queue
config.active_job.queue_adapter = Figaro.env.job_queue.to_sym if Figaro.env.job_queue?
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
config.after_initialize do
Bullet.enable = false
Bullet.alert = true
Bullet.console = true
Bullet.rails_logger = true
end
end
Seems like the error was coming from this line in my test.rb, was just trying to set the default host on my links to not be example.com. Not sure how this resulted in the bug I was experiencing. But after going through the config and trying to figure out why development.rb was working. This is what I got.
config.action_controller.default_url_options = { host: 'localhost:5000' }
Thanks to everybody who helped me.

Assets compiling on fly (Rails with compass gem)

I am trying to change my styles of my scss assets on my ruby on rails project but they are applying just after rake assets:precompile and restarting rails server.
With JS files everything allright and they are changing on fly.
Possible problem is with compass gem.
That is my repo - https://github.com/tanotify/blog
And file development.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
end
Found solution! In config/environments/development.rb added line:
config.serve_static_assets = false

Rails stuck on post for a long time

Every time to POST a request
it takes me about 10~20 sec to stuck on the console.
What happened ? How to profile it ?
Started POST "/users" for 127.0.0.1 at 2015-01-07 11:20:43 +0800
Here's the gems are suspected to cause the slow response
gem 'quiet_assets'
gem "spring"
gem 'pry-rescue'
gem 'pry-byebug'
development.rb
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
--------------
I had a similar issue with slow performance, it became worse the larger your assets became. The solution, was that one line you have set to true in your development.rb file.
config.assets.debug = true
Change that to false and you should see a BIG improvement!

Resources