From cfc31b6643955be33effae018fe9710946611128 Mon Sep 17 00:00:00 2001 From: aidewoode Date: Tue, 7 Nov 2023 14:25:52 +0800 Subject: [PATCH] Adopt some new APIs from Rails 7.1 --- Dockerfile | 2 +- app/controllers/health_controller.rb | 9 --------- app/models/user.rb | 7 ++----- config/puma.rb | 14 ++++++------- config/routes.rb | 4 +--- docker/production_start.sh | 5 ++--- lib/tasks/docker.rake | 22 --------------------- test/controllers/health_controller_test.rb | 23 ---------------------- 8 files changed, 12 insertions(+), 74 deletions(-) delete mode 100644 app/controllers/health_controller.rb delete mode 100644 lib/tasks/docker.rake delete mode 100644 test/controllers/health_controller_test.rb diff --git a/Dockerfile b/Dockerfile index ecd6442d..331a5b03 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,7 +30,7 @@ RUN bundle config --local without 'development test' \ COPY . /app -RUN bundle exec rails assets:precompile SECRET_KEY_BASE=fake_secure_for_compile \ +RUN SECRET_KEY_BASE_DUMMY=1 bundle exec rails assets:precompile \ && yarn cache clean \ && rm -rf node_modules tmp/cache/* /tmp/* yarn.lock log/production.log app/javascript/* app/assets/* diff --git a/app/controllers/health_controller.rb b/app/controllers/health_controller.rb deleted file mode 100644 index e5fe2cc1..00000000 --- a/app/controllers/health_controller.rb +++ /dev/null @@ -1,9 +0,0 @@ -# frozen_string_literal: true - -class HealthController < ActionController::Base - rescue_from(Exception) { head :internal_server_error } - - def show - head :ok - end -end diff --git a/app/models/user.rb b/app/models/user.rb index e1306137..817d899d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -11,10 +11,11 @@ class User < ApplicationRecord has_setting :theme, default: DEFAULT_THEME serialize :recently_played_album_ids, type: Array, coder: YAML - before_create :downcase_email before_update :remove_deprecated_password_salt, if: :will_save_change_to_password_digest? after_create :create_buildin_playlists + normalizes :email, with: ->(email) { email.strip.downcase } + validates :email, presence: true, format: {with: URI::MailTo::EMAIL_REGEXP}, uniqueness: {case_sensitive: false} validates :password, allow_nil: true, length: {minimum: 6} validates :theme, inclusion: {in: AVAILABLE_THEME_OPTIONS}, allow_nil: true @@ -66,8 +67,4 @@ def create_buildin_playlists create_current_playlist create_favorite_playlist end - - def downcase_email - self.email = email.downcase - end end diff --git a/config/puma.rb b/config/puma.rb index 9bacad24..2067b380 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -23,12 +23,12 @@ # Specifies the `pidfile` that Puma will use. pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } -# Specifies the number of `workers` to boot in clustered mode. -# Workers are forked web server processes. If using threads and workers together -# the concurrency of the application would be max `threads` * `workers`. -# Workers do not work on JRuby or Windows (both of which do not support -# processes). -# workers ENV.fetch("WEB_CONCURRENCY") { 2 } +# Specifies that the worker count should equal the number of processors in production. +if ENV["RAILS_ENV"] == "production" + require "concurrent-ruby" + worker_count = Integer(ENV.fetch("WEB_CONCURRENCY") { Concurrent.physical_processor_count }) + workers worker_count if worker_count > 1 +end # Use the `preload_app!` method when specifying a `workers` number. # This directive tells Puma to first boot the application and load code @@ -41,8 +41,6 @@ plugin :tmp_restart if BlackCandy::Config.embedded_sidekiq? - workers 2 - # Preloading the application is necessary to ensure # the configuration in your initializer runs before # the boot callback below. diff --git a/config/routes.rb b/config/routes.rb index 5598918b..ddd9ef44 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -68,9 +68,7 @@ get "/422", to: "errors#unprocessable_entity", as: :unprocessable_entity get "/500", to: "errors#internal_server_error", as: :internal_server_error - # Rails 7.1 will support health check by default, - # So we will replace this route with the default one in the future. - get "/up", to: "health#show" + get "up", to: "rails/health#show", as: :rails_health_check namespace :api do namespace :v1 do diff --git a/docker/production_start.sh b/docker/production_start.sh index 64e6c4cb..e034db73 100755 --- a/docker/production_start.sh +++ b/docker/production_start.sh @@ -1,5 +1,4 @@ #!/bin/sh -rails docker:db_prepare - -bundle exec puma -C config/puma.rb +rails db:prepare +bundle exec puma -C config/puma.rb \ No newline at end of file diff --git a/lib/tasks/docker.rake b/lib/tasks/docker.rake deleted file mode 100644 index 32d09eac..00000000 --- a/lib/tasks/docker.rake +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -namespace :docker do - # Prepare database. Beacuse when the database adapter is sqlite, the task 'db:prepare' won't run 'db:seed'. - # So add 'db:seed' task explicitly to avoit it. See https://github.com/rails/rails/issues/36383. - # Note: this issue is fixed in Rails master branch. This code is also base the code on Rails master branch. - # See here: https://github.com/rails/rails/blob/2cef3ac45bd85e1642b1270ab4fa31629044c962/activerecord/lib/active_record/tasks/database_tasks.rb#L179. - # So this is just temprary fix. This task can be replace with 'db:prepare' in the future after Rails 7.1 is released. - task db_prepare: :environment do - begin - database_initialized = ActiveRecord::Base.connection.schema_migration.table_exists? - rescue ActiveRecord::NoDatabaseError - database_initialized = false - end - - Rake::Task["db:prepare"].invoke - - unless database_initialized - Rake::Task["db:seed"].invoke - end - end -end diff --git a/test/controllers/health_controller_test.rb b/test/controllers/health_controller_test.rb deleted file mode 100644 index 749ddd92..00000000 --- a/test/controllers/health_controller_test.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class HealthControllerTest < ActionDispatch::IntegrationTest - class DummyHealthController < HealthController - def show - raise "error" - end - end - - test "should return ok" do - get up_url - assert_response :success - end - - test "should return 500 when exception raised" do - HealthController.stub(:new, DummyHealthController.new) do - get up_url - assert_response :internal_server_error - end - end -end