From 55e24f3adbeca964b06f00a1810f758620441aed Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Wed, 20 Nov 2024 22:00:24 +0100 Subject: [PATCH] Change default number of Puma workers to 2 --- .app_version | 2 +- CHANGELOG.md | 15 +++++++++++++++ config/initializers/prometheus.rb | 13 ++++++++----- config/initializers/sidekiq.rb | 4 ++-- config/puma.rb | 27 +++++++++++++++++---------- 5 files changed, 43 insertions(+), 18 deletions(-) diff --git a/.app_version b/.app_version index 427cda05..74aaa3f3 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.16.7 +0.16.8 diff --git a/CHANGELOG.md b/CHANGELOG.md index f0b86b1c..0d29ae67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# 0.16.8 - 2024-11-20 + +### Changed + +- Default number of Puma workers is now 2 instead of 1. This should improve the performance of the application. If you have a lot of users, you might want to increase the number of workers. You can do this by setting the `WEB_CONCURRENCY` env var in your `docker-compose.yml` file. Example: + +```diff + dawarich_app: + image: freikin/dawarich:latest + container_name: dawarich_app + environment: + ... + WEB_CONCURRENCY: "2" +``` + # 0.16.7 - 2024-11-20 ### Changed diff --git a/config/initializers/prometheus.rb b/config/initializers/prometheus.rb index b4b13538..0a3e1b37 100644 --- a/config/initializers/prometheus.rb +++ b/config/initializers/prometheus.rb @@ -1,12 +1,15 @@ -# in config/initializers/prometheus.rb -if Rails.env != "test" && ENV['PROMETHEUS_EXPORTER_ENABLED'].to_s == 'true' +# frozen_string_literal: true + +if !Rails.env.test? && ENV['PROMETHEUS_EXPORTER_ENABLED'].to_s == 'true' require 'prometheus_exporter/middleware' require 'prometheus_exporter/instrumentation' # This reports stats per request like HTTP status and timings Rails.application.middleware.unshift PrometheusExporter::Middleware - # this reports basic process stats like RSS and GC info, type master - # means it is instrumenting the master process - PrometheusExporter::Instrumentation::Process.start(type: "master") + # this reports basic process stats like RSS and GC info + PrometheusExporter::Instrumentation::Process.start(type: 'web') + + # Add ActiveRecord instrumentation + PrometheusExporter::Instrumentation::ActiveRecord.start end diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index c083e0bd..02c0f5ad 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -3,9 +3,9 @@ Sidekiq.configure_server do |config| config.redis = { url: ENV['REDIS_URL'] } - require 'prometheus_exporter/instrumentation' - if ENV['PROMETHEUS_EXPORTER_ENABLED'].to_s == 'true' + require 'prometheus_exporter/instrumentation' + config.server_middleware do |chain| chain.add PrometheusExporter::Instrumentation::Sidekiq end diff --git a/config/puma.rb b/config/puma.rb index 8825ec78..d3094caa 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -6,7 +6,7 @@ # the maximum value specified for Puma. Default is set to 5 threads for minimum # and maximum; this matches the default thread size of Active Record. # -max_threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 } +max_threads_count = ENV.fetch('RAILS_MAX_THREADS', 5) min_threads_count = ENV.fetch('RAILS_MIN_THREADS') { max_threads_count } threads min_threads_count, max_threads_count @@ -17,14 +17,14 @@ worker_timeout 3600 if ENV.fetch('RAILS_ENV', 'development') == 'development' # Specifies the `port` that Puma will listen on to receive requests; default is 3000. # -port ENV.fetch('PORT') { 3000 } +port ENV.fetch('PORT', 3000) # Specifies the `environment` that Puma will run in. # -environment ENV.fetch('RAILS_ENV') { 'development' } +environment ENV.fetch('RAILS_ENV', 'development') # Specifies the `pidfile` that Puma will use. -pidfile ENV.fetch('PIDFILE') { 'tmp/pids/server.pid' } +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 @@ -32,24 +32,31 @@ pidfile ENV.fetch('PIDFILE') { 'tmp/pids/server.pid' } # Workers do not work on JRuby or Windows (both of which do not support # processes). # -# workers ENV.fetch("WEB_CONCURRENCY") { 2 } +workers ENV.fetch('WEB_CONCURRENCY', 2) # Use the `preload_app!` method when specifying a `workers` number. # This directive tells Puma to first boot the application and load code # before forking the application. This takes advantage of Copy On Write # process behavior so workers use less memory. # -# preload_app! +preload_app! # Allow puma to be restarted by `bin/rails restart` command. plugin :tmp_restart # Prometheus exporter -# -# optional check, avoids spinning up and down threads per worker - if ENV['PROMETHEUS_EXPORTER_ENABLED'].to_s == 'true' require 'prometheus_exporter/instrumentation' - PrometheusExporter::Instrumentation::Puma.start unless PrometheusExporter::Instrumentation::Puma.started? + before_fork do + PrometheusExporter::Client.default = PrometheusExporter::Client.new( + host: ENV.fetch('PROMETHEUS_EXPORTER_HOST', '0.0.0.0'), + port: ENV.fetch('PROMETHEUS_EXPORTER_PORT', 9394) + ) + end + + on_worker_boot do + require 'prometheus_exporter/instrumentation' + PrometheusExporter::Instrumentation::Puma.start + end end