Rails server doesn't see code changes and reload files
Asked Answered
S

5

26

I noticed that my rails server doesn't reload controllers, models and probably any other files after I change them. I use Vagrant and Rails API, and I found that some people fix this problem by adding below line to the Vagrantfile.

config.vm.provider "virtualbox" do |vb|
  vb.customize [ "guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 5000 ]
end

It doesn't fix the problem for me. I run out of the ideas what else I can do to solve this problem. I attach files which might be useful for you.

My Gemfile looks like this:

source 'https://rubygems.org'

gem 'rake', '< 11.0'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '>= 5.0.0.beta3', '< 5.1'
# Use mysql as the database for Active Record
# gem 'mysql2', '>= 0.3.18', '< 0.5'

# User PostgreSQL as the database for Active Record
gem 'pg', '~> 0.18'

gem 'active_model_serializers'

gem 'rspec-its'

gem 'database_cleaner'

# Use Puma as the app server
gem 'puma'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.0'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Gem allowing using external APIs
gem 'httparty'

# Auth0 gem for authentication using JWT
gem 'knock'

gem 'jwt'

# OpenID Omniauth gem for authenticating Steam users
gem 'omniauth-steam'

# Gem for managing environment variables
gem 'figaro'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem 'rack-cors', :require => 'rack/cors'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
  gem 'rspec-rails', '~> 3.0'
  gem 'factory_girl_rails'
  gem 'ffaker'
end

group :test do
  gem 'shoulda-matchers'
  gem 'json-schema'
end

group :development do
  gem 'listen', '~> 2.10'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

I'm sure that I run my server in development mode, because of the beggining of the log

=> Booting Puma
=> Rails 5.0.0.beta3 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma starting in single mode...
* Version 3.1.0 (ruby 2.2.3-p173), codename: El Niño Winter Wonderland
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000

This is my development.rb file

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.
  config.consider_all_requests_local = true

  # Enable/disable caching. By default caching is disabled.
  if Rails.root.join('tmp/caching-dev.txt').exist?
    config.action_controller.perform_caching = true

    config.action_mailer.perform_caching = false

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => 'public, max-age=172800'
    }
  else
    config.action_controller.perform_caching = false

    config.action_mailer.perform_caching = false

    config.cache_store = :null_store
  end

  # 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


  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  # Use an evented file watcher to asynchronously detect changes in source code,
  # routes, locales, etc. This feature depends on the listen gem.
  config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end

My Vagrantfile

Vagrant.configure(2) do |config|

  if Vagrant.has_plugin?("vagrant-timezone")
    config.timezone.value = "Europe/Warsaw"
  end

  config.vm.box = "ubuntu/trusty64"

  config.vm.network :forwarded_port, guest: 3000, host: 3000
  config.vm.synced_folder "E:/Projekty - Rails", "/home/projekty"

  config.vm.provider "virtualbox" do |vb|
    vb.customize [ "guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 5000 ]
  end
end
Snigger answered 12/3, 2016 at 15:22 Comment(7)
In development.rb file, can you add config.threadsafe! line. Please check it is working or not ?Absorptivity
@MohammadShahadatHossain it causes erorr when I try to start the server. I use rails api, so it probably the reason why, right?Snigger
using rails api shouldn't be a problem. There might be some configuration missing that is why the problem occurring.Absorptivity
can you show your Vagrantfile file? I think problem is there.Absorptivity
@MohammadShahadatHossain added Vagrantfile.Snigger
to start the server you need to use rails s -b 0.0.0.0 so it recognize the public interface. Did you tried that?Absorptivity
@MohammadShahadatHossain I always start server using this command.Snigger
S
19

I've solved my problem adding below line to the development.rb file.

config.reload_classes_only_on_change = false
Snigger answered 12/3, 2016 at 16:45 Comment(7)
This for some reason made my puma server very slow and started breaking some of my devise user session routes. Weird. Will try again and see if it still happens.Polyzoan
Yeah it gives me an Read error: #<ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool within 5.000 seconds (waited 5.000 seconds); all pooled connections were in use> issue. Not sure if it is caused by the change or a side effect of something else I have done wrong.Polyzoan
@JayKilleen I was not using Devise in my project, so I didn't encounter this type of errors. After quick research I found possible solution to your problem. You can read more link and give it a try.Snigger
I suspect you'll find it's slow because it's probably reloading all classes, not just changed ones, and doing so on every request.Barite
If config.cache_classes is true, this option is ignored.Mckean
after so long time, this finally fixed this for me on centos and ubuntu.Bergin
I'm using puma and for me it made also server very-very slow.Mani
I
101

Add the following to config/environments/development.rb

#config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.file_watcher = ActiveSupport::FileUpdateChecker

FileUpdateChecker will detect by polling the change of the file.

Interface answered 14/4, 2016 at 7:54 Comment(8)
changed in my development.rb and works like a charm - thanksCarmelinacarmelita
Worked for me too. I've been suffering for more than 2 weeks thinking it's a bug with Rails 5 beta. Thank you!Ombre
hi, Rev3rse. Using Virtual Box' shared folder, this problem has occured. Because OS (in running vm) can't detect file change event in shared folders.Interface
I use a shared folder too. No Vagrant, just Windows 10 as a host, Ubuntu 16.04 as a guest machine. When I used to update a file in Windows, Rails didn't see the change. The proposed solution helped, thanks.Weisburgh
Worked for me on Vagrant with Win10 host and Ubuntu guest. Thanks!Elliellicott
Is there such a thing as stackoverflow gold? Because you deserve it. I've tried tweaking clock sync settings, changing to nfs file type, etc. Nothing worked. Thank you.Cooksey
This worked for me just now in 2021, but the top commented line out has worked for weeks. Weird how this suddenly became an issue.Dub
it works effectively. thanks a lot!Loki
S
19

I've solved my problem adding below line to the development.rb file.

config.reload_classes_only_on_change = false
Snigger answered 12/3, 2016 at 16:45 Comment(7)
This for some reason made my puma server very slow and started breaking some of my devise user session routes. Weird. Will try again and see if it still happens.Polyzoan
Yeah it gives me an Read error: #<ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool within 5.000 seconds (waited 5.000 seconds); all pooled connections were in use> issue. Not sure if it is caused by the change or a side effect of something else I have done wrong.Polyzoan
@JayKilleen I was not using Devise in my project, so I didn't encounter this type of errors. After quick research I found possible solution to your problem. You can read more link and give it a try.Snigger
I suspect you'll find it's slow because it's probably reloading all classes, not just changed ones, and doing so on every request.Barite
If config.cache_classes is true, this option is ignored.Mckean
after so long time, this finally fixed this for me on centos and ubuntu.Bergin
I'm using puma and for me it made also server very-very slow.Mani
G
3

This worked for me with ruby 2.6.5 and Rails 5.2.4.1.:

Add the following line in config/environments/development.rb:

config.file_watcher = ActiveSupport::EventedFileUpdateChecker

along with

config.cache_classes = false

in the same file, and

gem 'listen'

in the :development group in Gemfile.

Groan answered 9/2, 2020 at 9:2 Comment(0)
C
2

pocari's solution worked for me but I had to wait a few seconds before a page reload, otherwise the content was not always updated.

Adding an option to the synced_folder as described in this answer worked fine:

config.vm.synced_folder ".", "/vagrant", type: "nfs", mount_options: ['actimeo=1']

(and the change in development.rb is not necessary)

Cypro answered 16/10, 2017 at 16:57 Comment(0)
I
0

I was having the same issue so what I did was make a quick script bump like this. Make sure you are in your app folder first.

!#/bin/bash

rake db:migrate
echo "MIGRATED"
rake routes
echo "routed"
sudo service apache2 restart
echo "web server reloaded"

Now you can just type ./bump and it will run all three commands then you know everything is loaded. I also use this method to repeat this like the command line installs for gems such as devise.

Imprisonment answered 13/3, 2016 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.