LoginSignup
2
2

More than 5 years have passed since last update.

unicornを再起動するとエラー【uninitialized constant Users (NameError)】

Last updated at Posted at 2018-05-22

unicornとは

unicornとはアプリケーションサーバーの一つです。
Webサーバーはクライアントからのリクエストを裁き、さらにリクエストを今回作成したRailsアプリが入っているアプリケーションサーバーへ投げる。アプリケーションサーバーは実際にアプリを起動して実行するもので、WebサーバーはHTTPでクライアントと接続し、静的ファイルを提供します。

なぜクライアントから直接アプリケーションサーバーに投げないのかという質問に対しては、簡単に言うとWebサーバーの方がリクエスト裁きが得意からだそうです。

また機会がある時にこの辺まとめたいなあ・・・と思います。

出現エラー

そんなunicorn設定後、再起動しようと・・・
Unicorn再起動時のコマンド

Terminal
$ unicorn_rails -c /home/[Username]/[Applicationname]/config/unicorn.conf.rb -D -E production

unicorn停止時

Terminal
kill -QUIT `cat /home/[Username]/[Applicationname]/tmp/pids/unicorn.pid`
Terminal
master failed to start, check stderr log for details

エラーが出てるぞ!unicorn.logを見てこい!
実際行ってみると・・・

~/madeit2/log/unicorn.log
<top (required)>': uninitialized constant Users (NameError)

わああ・・・・
このエラー Usersクラスが関係するのはgem 'devise'の問題。
ローカルではdeviseは問題なく動作していることから、本番環境に移る際にdeviseのgemを読み込めてないのかな・・・(推測)

現状

development環境では動作する。

Terminal
$ rails s -e development

=> Booting WEBrick
=> Rails 4.2.6 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2018-05-24 04:43:10] INFO  WEBrick 1.3.1
[2018-05-24 04:43:10] INFO  ruby 2.3.1 (2016-04-26) [x86_64-linux]
[2018-05-24 04:43:10] INFO  WEBrick::HTTPServer#start: pid=32553 port=3000

production環境ではNON・・・

Terminal
 $ rails s -e production

`<top (required)>': uninitialized constant Users (NameError)

やったこと(未解決)~application.rbが怪しい??

何となくの勘で/configディレクトリが怪しいかな・・・と。

application.rb
require File.expand_path('../boot', __FILE__)

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Madeit
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true
    config.assets.initialize_on_precompile = false

  end
end

ここに

require 'devise'

を打つことで解決された方がいるようですが、自分の場合は、

require 'rails/all'

が記述されており、関係ないかなあと・・・

Gemfileを見てもgemはきちんと導入されている。

やったこと2(未解決)~autoload_pathが怪しい?

application.rbではauto_loadとeager_loadを設定

application.rb
    #デフォルトでOFFになっているauto_loadをON
    config.enable_dependency_loading = true

    #auto_loadはrails4までは使える
    config.autoload_paths += Dir["#{config.root}/lib/**/"]

    #eager_load設定
    #config.paths.add 'lib', eager_load: true    
    #config.eager_load_paths += Dir["#{config.root}/lib/**/"]

    config.active_record.raise_in_transactional_callbacks = true
    #JS関連
    config.assets.initialize_on_precompile = false

autoload(自動読み込み)のパスがどこを指しているか・・・

Terminal
$ rails r 'puts ActiveSupport::Dependencies.autoload_paths'

~/madeit2/lib/
~/madeit2/lib/assets/
~/madeit2/lib/tasks/
~/madeit2/app/assets
~/madeit2/app/controllers
~/madeit2/app/helpers
~/madeit2/app/mailers
~/madeit2/app/models
~/madeit2/app/uploaders
~/madeit2/app/controllers/concerns
~/madeit2/app/models/concerns
~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/jquery-ui-rails-6.0.1/app/assets
~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/materialize-sass-0.100.2/app/assets
~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.4.3/app/controllers
~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.4.3/app/helpers

やったこと(解決)

uninitialized constant Users (NameError)

deviseのコントローラーのフォルダ'Users'を'users'に変更。
うまくいった。

おまけ

ちなみに上記のエラー・・・herokuを使った時にも出た・・・

参考文献

Webサーバーとアプリケーションサーバーの統合化?
https://techracho.bpsinc.jp/hachi8833/2018_04_11/54752

↑天使

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2