A platform for community discussion. Free, open, simple.
DEV: Remove Unicorn web server in favor of Pitchfork (#39032)
Loïc Guitaut6b243ffdfdec
diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.jsonindex de3d82c4526..176ede89e7d 100644--- a/.devcontainer/devcontainer.json+++ b/.devcontainer/devcontainer.json@@ -5,7 +5,7 @@"workspaceFolder": "/workspace/discourse","postStartCommand": "./.devcontainer/scripts/start.rb","forwardPorts": [- 9292, // bin/unicorn+ 9292, // bin/pitchfork3000, // bin/rails s4200, // ember-cli8025, // mailhogdiff --git a/.licensed.yml b/.licensed.ymlindex 4e218853187..1338dd6254f 100644--- a/.licensed.yml+++ b/.licensed.yml@@ -82,4 +82,3 @@ reviewed:- tilt # MIT- ttfunk # Ruby or GPLv2/GPLv3- unf # BSD-2-Clause- - unicorn # Ruby or GPLv2/GPLv3diff --git a/Gemfile b/Gemfileindex 6145632cb73..b7f47a31c01 100644--- a/Gemfile+++ b/Gemfile@@ -20,7 +20,7 @@ gem "propshaft"gem "json"# this will eventually be added to rails,-# allows us to precompile all our templates in the unicorn master+# allows us to precompile all our templates in the app server mastergem "actionview_precompiler", require: falsegem "discourse-seed-fu"@@ -105,7 +105,7 @@ gem "mini_racer"gem "highline", require: false-# When unicorn is not used anymore, we can use Rack 3+# TODO: upgrade to Rack 3 now that Unicorn has been removedgem "rack", "< 3"gem "rack-protection" # security@@ -194,9 +194,12 @@ gem "htmlentities", require: falsegem "rack-mini-profiler", require: ["enable_rails_patches"]-gem "unicorn", require: false, platform: :rubygem "pitchfork", require: false+# Used by discourse-prometheus to collect socket queue stats.+# Was previously a transitive dependency of the unicorn gem.+gem "raindrops", require: false, platform: :ruby+gem "rbtrace", require: false, platform: :mri# required for feed importing and embeddingdiff --git a/Gemfile.lock b/Gemfile.lockindex 81a733f38cd..52f52b3091f 100644--- a/Gemfile.lock+++ b/Gemfile.lock@@ -253,7 +253,6 @@ GEMsimpleidn (~> 0.2)jwt (2.10.1)base64- kgio (2.11.4)language_server-protocol (3.17.0.5)libv8-node (24.12.0.1)libv8-node (24.12.0.1-aarch64-linux)@@ -724,9 +723,6 @@ GEMunicode-display_width (3.2.0)unicode-emoji (~> 4.1)unicode-emoji (4.2.0)- unicorn (6.1.0)- kgio (~> 2.6)- raindrops (~> 0.7)uniform_notifier (1.18.0)uri (1.1.1)useragent (0.16.11)@@ -879,6 +875,7 @@ DEPENDENCIESrails_failoverrails_multisiterailties (~> 8.0.0)+ raindropsrakerb-fseventrbtrace@@ -926,7 +923,6 @@ DEPENDENCIEStty-prompttzinfo-dataunf- unicornweb-pushwebmockyaml-lint@@ -1050,7 +1046,6 @@ CHECKSUMSjson-schema (6.2.0) sha256=e8bff46ed845a22c1ab2bd0d7eccf831c01fe23bb3920caa4c74db4306813666json_schemer (2.5.0) sha256=2f01fb4cce721a4e08dd068fc2030cffd0702a7f333f1ea2be6e8991f00ae396jwt (2.10.1) sha256=e6424ae1d813f63e761a04d6284e10e7ec531d6f701917fadcd0d9b2deaf1cc5- kgio (2.11.4) sha256=bda7a2146115998a5b07154e708e0ac02c38dcee7e793c33e2e14f600fdfffc6language_server-protocol (3.17.0.5) sha256=fd1e39a51a28bf3eec959379985a72e296e9f9acfce46f6a79d31ca8760803cclibv8-node (24.12.0.1) sha256=d93d23b861bfe5de3ba829e34a142402fb83b4c3592dd58d9ac4e027588d739alibv8-node (24.12.0.1-aarch64-linux) sha256=22bd0246cde85d70c88cf772727ac3677a20ac1d169105f82efe5f1f7c39f755@@ -1275,7 +1270,6 @@ CHECKSUMSunf (0.2.0) sha256=e6bcc2e101d80e3f9459753db747d5926aada1aaaf61e629e93359da9a5b04abunicode-display_width (3.2.0) sha256=0cdd96b5681a5949cdbc2c55e7b420facae74c4aaf9a9815eee1087cb1853c42unicode-emoji (4.2.0) sha256=519e69150f75652e40bf736106cfbc8f0f73aa3fb6a65afe62fefa7f80b0f80f- unicorn (6.1.0) sha256=45dd987add4c2b084c1880a68373af42797a704ad7441faff9b14b4982aa0fc0uniform_notifier (1.18.0) sha256=4787785556f66f6418486da0f1d78b3239aaff98e2e7938fb05e2062b0ffce9duri (1.1.1) sha256=379fa58d27ffb1387eaada68c749d1426738bd0f654d812fcc07e7568f5c57c6useragent (0.16.11) sha256=700e6413ad4bb954bb63547fa098dddf7b0ebe75b40cc6f93b8d54255b173844diff --git a/bin/docker/unicorn b/bin/docker/unicorndeleted file mode 100755index 6dd548e1242..00000000000--- a/bin/docker/unicorn+++ /dev/null@@ -1,3 +0,0 @@-#!/bin/bash--exec "$(dirname "$0")/exec" bin/unicorn "$@"diff --git a/bin/ember-cli b/bin/ember-cliindex ea993ca777a..57851a0843c 100755--- a/bin/ember-cli+++ b/bin/ember-cli@@ -7,7 +7,7 @@ require "open3"RAILS_ROOT = File.expand_path("../../", Pathname.new(__FILE__).realpath)PORT = ENV["UNICORN_PORT"] ||= "3000"HOSTNAME = ENV["DISCOURSE_HOSTNAME"] ||= "127.0.0.1"-CUSTOM_ARGS = %w[--try --test --build --unicorn -u --forward-host]+CUSTOM_ARGS = %w[--try --test --build --server --unicorn -u --forward-host]PROXY =if ARGV.include?("--try")"https://try.discourse.org"@@ -44,7 +44,7 @@ if ARGV.include?("-h") || ARGV.include?("--help")puts "ember-cli OPTIONS"puts "#{"--try".cyan} To proxy try.discourse.org"puts "#{"--test".cyan} To run the test suite"- puts "#{"--unicorn, -u".cyan} To run a unicorn server as well"+ puts "#{"--server, -u".cyan} To run a server as well"puts "The rest of the arguments are passed to ember server per:", ""exec "pnpm ember #{command} --help"end@@ -79,11 +79,11 @@ pnpm_env = {}pnpm_env["FORWARD_HOST"] = "true" if ARGV.include?("--forward-host")-if ARGV.include?("-u") || ARGV.include?("--unicorn")- unicorn_env = { "DISCOURSE_PORT" => ENV["DISCOURSE_PORT"] || "4200" }+if ARGV.include?("-u") || ARGV.include?("--server") || ARGV.include?("--unicorn")+ server_env = { "DISCOURSE_PORT" => ENV["DISCOURSE_PORT"] || "4200" }if command == "server" && ENV["CODESPACE_NAME"]- unicorn_env.merge!(+ server_env.merge!({"DISCOURSE_PORT" => "443","DISCOURSE_FORCE_HTTPS" => "1",@@ -93,8 +93,7 @@ if ARGV.include?("-u") || ARGV.include?("--unicorn"))end- server_name = ENV["RUN_PITCHFORK"] == "0" ? "unicorn" : "pitchfork"- unicorn_pid = spawn(unicorn_env, "#{__dir__}/#{server_name}")+ server_pid = spawn(server_env, "#{__dir__}/pitchfork")ember_cli_pid = nilsigint_received = false@@ -117,9 +116,9 @@ if ARGV.include?("-u") || ARGV.include?("--unicorn")# additional TERM races with the server's hard shutdown and can cause it# to fall back to a slow graceful shutdown, making the process appear# stuck for up to 60 seconds.- if !sigint_received && process_running?(unicorn_pid)- puts "[bin/ember-cli] ember-cli process stopped. Terminating unicorn."- Process.kill("TERM", unicorn_pid)+ if !sigint_received && process_running?(server_pid)+ puts "[bin/ember-cli] ember-cli process stopped. Terminating server."+ Process.kill("TERM", server_pid)endend@@ -135,7 +134,7 @@ if ARGV.include?("-u") || ARGV.include?("--unicorn")elseputs "\n[bin/ember-cli] Forcing shutdown..."begin- Process.kill("KILL", unicorn_pid)+ Process.kill("KILL", server_pid)rescue StandardErrornilend@@ -150,10 +149,10 @@ if ARGV.include?("-u") || ARGV.include?("--unicorn")endend- Process.wait(unicorn_pid)+ Process.wait(server_pid)if ember_cli_pid && process_running?(ember_cli_pid)- puts "[bin/ember-cli] unicorn process stopped. Terminating ember-cli."+ puts "[bin/ember-cli] server process stopped. Terminating ember-cli."Process.kill("TERM", ember_cli_pid)endelsediff --git a/bin/pitchfork b/bin/pitchforkindex d2578ddec34..2cb16de470f 100755--- a/bin/pitchfork+++ b/bin/pitchfork@@ -50,7 +50,7 @@ end# This is handy if you want to bind a key to restarting pitchfork in devif dev_mode- UNICORN_DEV_SUPERVISOR_PID = Process.pid+ DEV_SUPERVISOR_PID = Process.pidrestart = truewhile restartdiff --git a/bin/qunit b/bin/qunitindex 11f8699f3c6..a66e07d51a5 100755--- a/bin/qunit+++ b/bin/qunit@@ -368,20 +368,20 @@ class QunitRunnerinstall_node_dependencies!@rails_port = next_available_port(60_098)- unicorn_env = build_unicorn_environment(@rails_port)- unicorn_pid = nil+ server_env = build_server_environment(@rails_port)+ server_pid = nilbeginif @dry_run- puts "[dry-run] skipping unicorn startup"+ puts "[dry-run] skipping server startup"else- unicorn_pid =- Process.spawn(unicorn_env, File.join(rails_root, "bin", "pitchfork"), pgroup: true)+ server_pid =+ Process.spawn(server_env, File.join(rails_root, "bin", "pitchfork"), pgroup: true)endwait_for_rails_server(@rails_port)yieldensure- stop_unicorn(unicorn_pid, unicorn_env["UNICORN_PID_PATH"]) if unicorn_pid+ stop_server(server_pid, server_env["UNICORN_PID_PATH"]) if server_pidendend@@ -426,16 +426,16 @@ class QunitRunnerfalseend- def build_unicorn_environment(unicorn_port)- pid_path = File.join(rails_root, "tmp/pids", "unicorn_test_#{unicorn_port}.pid")+ def build_server_environment(server_port)+ pid_path = File.join(rails_root, "tmp/pids", "test_server_#{server_port}.pid")env = {"RAILS_ENV" => @rails_env || "test","SKIP_ENFORCE_HOSTNAME" => "1","UNICORN_PID_PATH" => pid_path,- "UNICORN_PORT" => unicorn_port.to_s,+ "UNICORN_PORT" => server_port.to_s,"UNICORN_SIDEKIQS" => "0","DISCOURSE_SKIP_CSS_WATCHER" => "1",- "UNICORN_LISTENER" => "127.0.0.1:#{unicorn_port}",+ "UNICORN_LISTENER" => "127.0.0.1:#{server_port}","LOGSTASH_UNICORN_URI" => nil,"UNICORN_WORKERS" => "1","UNICORN_TIMEOUT" => "90",@@ -524,9 +524,9 @@ class QunitRunnerencode_query_string(params)end- def stop_unicorn(pid, pid_path)+ def stop_server(pid, pid_path)if @dry_run- puts "[dry-run] skipping unicorn shutdown"+ puts "[dry-run] skipping server shutdown"returnendProcess.kill("-KILL", pid)diff --git a/bin/rails b/bin/railsindex a3e9676eec1..5a8c7ed4c84 100755--- a/bin/rails+++ b/bin/rails@@ -10,7 +10,7 @@ if !ENV["RAILS_ENV"] && (ARGV[0] == "s" || ARGV[0] == "server") && Process.respoENV["RAILS_LOGS_STDOUT"] ||= "1"- exec File.expand_path(ENV["RUN_PITCHFORK"] == "0" ? "unicorn" : "pitchfork", __dir__)+ exec File.expand_path("pitchfork", __dir__)endAPP_PATH = File.expand_path("../config/application", __dir__)diff --git a/bin/unicorn b/bin/unicorndeleted file mode 100755index 71aab192d5c..00000000000--- a/bin/unicorn+++ /dev/null@@ -1,115 +0,0 @@-#!/usr/bin/env ruby-# frozen_string_literal: true--if ENV["RUN_PITCHFORK"] != "0"- STDERR.puts "[bin/unicorn] Running bin/pitchfork. Use RUN_PITCHFORK=0 to run unicorn instead."- exec File.expand_path("pitchfork", __dir__), *ARGV-end--require "pathname"-ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", Pathname.new(__FILE__).realpath)--require "rubygems"-require "bundler/setup"-require "fileutils"--dev_mode = false--# in development do some fussing around, to automate config-if !ARGV.include?("-E") && !ARGV.include?("--env") &&- (%w[development test].include?(ENV["RAILS_ENV"]) || !ENV["RAILS_ENV"])- dev_mode = true-- ARGV.push("-N")- if !ARGV.include?("-c") && !ARGV.include?("--config-file")- ARGV.push("-c")- ARGV.push(File.expand_path("../../config/unicorn.conf.rb", Pathname.new(__FILE__).realpath))- end-- # we do not want to listen on 2 ports, so lets fix it- if (idx = ARGV.index("-p")) && (port = ARGV[idx + 1].to_i) > 0- ENV["UNICORN_PORT"] ||= port.to_s- end-- ENV["UNICORN_PORT"] ||= "9292"-- if ARGV.delete("-x")- puts "Running without sidekiq"- ENV["UNICORN_SIDEKIQS"] = "0"- end-- ENV["UNICORN_SIDEKIQS"] ||= "1"-end--if ARGV.include?("--help")- fork { load Gem.bin_path("unicorn", "unicorn") }- Process.wait- puts "Extra Discourse Options:"- puts " -x run without sidekiq"- exit-end--# this dev_mode hackery enables the following to be used to restart unicorn:-#-# pkill -USR2 -f 'ruby bin/unicorn'-#-# This is handy if you want to bind a key to restarting unicorn in dev--if dev_mode- UNICORN_DEV_SUPERVISOR_PID = Process.pid-- restart = true- while restart- restart = false- pid =- fork do- begin- load Gem.bin_path("unicorn", "unicorn")- rescue ArgumentError => err- if (err.message =~ /Already running on PID/)- puts "\n"- puts "Unicorn is already running!\n"- pid = err.message[/PID:(\d+)/, 1]&.to_i- puts "\n"- puts "kill -9 #{pid} to stop the existing unicorn master process, then try again."- else- raise err- end- end- end- done = false-- int_count = 0- Signal.trap("INT") do- int_count += 1- case int_count- when 1- # Swallowed to give the child time to handle it- when 2- STDERR.puts "\nInterrupt again to force shutdown..."- else- begin- Process.kill("KILL", pid)- rescue StandardError- nil- end- exit!(1)- end- end-- Signal.trap("USR2") do- Process.kill("QUIT", pid)- puts "RESTARTING UNICORN"- restart = true- end-- Signal.trap("TERM") { Process.kill("TERM", pid) }-- while !done- sleep 1- done = Process.waitpid(pid, Process::WNOHANG)- end- end-else- load Gem.bin_path("unicorn", "unicorn")-enddiff --git a/config/discourse_defaults.conf b/config/discourse_defaults.confindex 6f00545ab41..186acce8d7d 100644--- a/config/discourse_defaults.conf+++ b/config/discourse_defaults.conf@@ -193,7 +193,7 @@ cors_origin = ''# enable if you really need to serve assets in prodserve_static_assets = false-# number of sidekiq workers (launched via unicorn master)+# number of sidekiq workers (launched via app server master)sidekiq_workers = 5# Logs Sidekiq jobs that have been running for longer than the configured number of minutes to the Rails log@@ -227,7 +227,7 @@ secret_key_base =# fallback path for all assets which are served via the application# used by static_controller-# in multi host setups this allows you to have old unicorn instances serve+# in multi host setups this allows you to have old app server instances serve# newly compiled assetsfallback_assets_path =diff --git a/config/environments/development.rb b/config/environments/development.rbindex 3c45fa8c05d..03011dc9831 100644--- a/config/environments/development.rb+++ b/config/environments/development.rb@@ -57,8 +57,8 @@ Discourse::Application.configure doif defined?(BetterErrors)BetterErrors::Middleware.allow_ip! ENV["TRUSTED_IP"] if ENV["TRUSTED_IP"]- if (defined?(Unicorn) || defined?(Pitchfork)) && ENV["UNICORN_WORKERS"].to_i != 1- # BetterErrors doesn't work with multiple unicorn workers. Disable it to avoid confusion+ if defined?(Pitchfork) && ENV["UNICORN_WORKERS"].to_i != 1+ # BetterErrors doesn't work with multiple workers. Disable it to avoid confusionRails.configuration.middleware.delete BetterErrors::Middlewareendend@@ -80,8 +80,7 @@ Discourse::Application.configure doconfig.developer_emails = emails.split(",").map(&:downcase).map(&:strip)end- if ENV["DISCOURSE_SKIP_CSS_WATCHER"] != "1" &&- (defined?(Rails::Server) || defined?(Unicorn) || defined?(Pitchfork))+ if ENV["DISCOURSE_SKIP_CSS_WATCHER"] != "1" && (defined?(Rails::Server) || defined?(Pitchfork))require "stylesheet/watcher"STDERR.puts "Starting CSS change watcher"@watcher = Stylesheet::Watcher.watchdiff --git a/config/initializers/000-development_reload_warnings.rb b/config/initializers/000-development_reload_warnings.rbindex cd7a6df9000..bf8768c5ff1 100644--- a/config/initializers/000-development_reload_warnings.rb+++ b/config/initializers/000-development_reload_warnings.rb@@ -40,7 +40,7 @@ if Rails.env.development? && !Rails.configuration.cache_classes && Discourse.runonly: /(\.rb|settings.yml)$/,ignore: [/node_modules/, /\.git/, /\.devenv/],) do |modified, added, removed|- supervisor_pid = UNICORN_DEV_SUPERVISOR_PID+ supervisor_pid = DEV_SUPERVISOR_PIDauto_restart = supervisor_pid && ENV["AUTO_RESTART"] != "0"files = modified + added + removeddiff --git a/config/initializers/000-zeitwerk.rb b/config/initializers/000-zeitwerk.rbindex 81991bb7fc3..b8142b83222 100644--- a/config/initializers/000-zeitwerk.rb+++ b/config/initializers/000-zeitwerk.rb@@ -58,6 +58,5 @@ Rails.autoloaders.main.ignore("lib/freedom_patches","lib/i18n/backend","lib/release_utils",- "lib/unicorn_logstash_patch.rb","lib/pitchfork_logstash_patch.rb",)diff --git a/config/pitchfork.conf.rb b/config/pitchfork.conf.rbindex f4ee053c1b3..c8a5bc48ab4 100644--- a/config/pitchfork.conf.rb+++ b/config/pitchfork.conf.rb@@ -2,14 +2,14 @@discourse_path = File.expand_path(File.expand_path(File.dirname(__FILE__)) + "/../")enable_logstash_logger = ENV["ENABLE_LOGSTASH_LOGGER"] == "1"-unicorn_stderr_path = "#{discourse_path}/log/unicorn.stderr.log"+stderr_log_path = "#{discourse_path}/log/unicorn.stderr.log"if enable_logstash_loggerrequire_relative "../lib/discourse_logstash_logger"require_relative "../lib/pitchfork_logstash_patch"- FileUtils.touch(unicorn_stderr_path) if !File.exist?(unicorn_stderr_path)+ FileUtils.touch(stderr_log_path) if !File.exist?(stderr_log_path)logger DiscourseLogstashLogger.logger(- logdev: unicorn_stderr_path,+ logdev: stderr_log_path,type: :unicorn,customize_event: lambda { |event| event["@timestamp"] = ::Time.now.utc },)@@ -77,6 +77,7 @@ oob_gc_enabled = ENV["DISCOURSE_DISABLE_MAJOR_GC_DURING_REQUESTS"] && RUBY_VERSIafter_worker_fork do |server, worker|DiscourseEvent.trigger(:web_fork_started)+ Discourse.apply_worker_db_variables_overridesDiscourse.after_forkSignalTrapLogger.instance.after_fork@@ -101,17 +102,17 @@ before_service_worker_ready do |server, service_worker|if Discourse.enable_sidekiq_logging?# Trap USR1, so we can re-issue to sidekiq workers- # but chain the default unicorn implementation as well+ # but chain the default pitchfork implementation as wellold_handler =Signal.trap("USR1") doold_handler.call# We have seen Sidekiq processes getting stuck in production sporadically when log rotation happens.- # The cause is currently unknown but we suspect that it is related to the Unicorn master process and- # Sidekiq demon processes reopening logs at the same time as we noticed that Unicorn worker processes only- # reopen logs after the Unicorn master process is done. To workaround the problem, we are adding an arbitrary+ # The cause is currently unknown but we suspect that it is related to the master process and+ # Sidekiq demon processes reopening logs at the same time as we noticed that worker processes only+ # reopen logs after the master process is done. To workaround the problem, we are adding an arbitrary# delay of 1 second to Sidekiq's log reopening procedure. The 1 second delay should be- # more than enough for the Unicorn master process to finish reopening logs.+ # more than enough for the master process to finish reopening logs.Demon::Sidekiq.kill("USR2")endenddiff --git a/config/unicorn.conf.rb b/config/unicorn.conf.rbdeleted file mode 100644index 8c608742808..00000000000--- a/config/unicorn.conf.rb+++ /dev/null@@ -1,197 +0,0 @@-# frozen_string_literal: true--# See http://unicorn.bogomips.org/Unicorn/Configurator.html-discourse_path = File.expand_path(File.expand_path(File.dirname(__FILE__)) + "/../")--enable_logstash_logger = ENV["ENABLE_LOGSTASH_LOGGER"] == "1"-unicorn_stderr_path = "#{discourse_path}/log/unicorn.stderr.log"-unicorn_stdout_path = "#{discourse_path}/log/unicorn.stdout.log"--if enable_logstash_logger- require_relative "../lib/discourse_logstash_logger"- require_relative "../lib/unicorn_logstash_patch"- FileUtils.touch(unicorn_stderr_path) if !File.exist?(unicorn_stderr_path)- logger DiscourseLogstashLogger.logger(- logdev: unicorn_stderr_path,- type: :unicorn,- customize_event: lambda { |event| event["@timestamp"] = ::Time.now.utc },- )-else- logger Logger.new(STDOUT)-end--# tune down if not enough ram-worker_processes (ENV["UNICORN_WORKERS"] || 3).to_i--working_directory discourse_path--# listen "#{discourse_path}/tmp/sockets/unicorn.sock"--# stree-ignore-listen ENV["UNICORN_LISTENER"] || "#{(ENV["UNICORN_BIND_ALL"] ? "" : "127.0.0.1:")}#{(ENV["UNICORN_PORT"] || 3000).to_i}"--FileUtils.mkdir_p("#{discourse_path}/tmp/pids") if !File.exist?("#{discourse_path}/tmp/pids")--# feel free to point this anywhere accessible on the filesystem-pid(ENV["UNICORN_PID_PATH"] || "#{discourse_path}/tmp/pids/unicorn.pid")--if ENV["RAILS_ENV"] == "production"- # By default, the Unicorn logger will write to stderr.- # Additionally, some applications/frameworks log to stderr or stdout,- # so prevent them from going to /dev/null when daemonized here:- stderr_path unicorn_stderr_path- stdout_path unicorn_stdout_path-- # nuke workers after 30 seconds instead of 60 seconds (the default)- timeout 30-else- # we want a longer timeout in dev cause first request can be really slow- timeout(ENV["UNICORN_TIMEOUT"] && ENV["UNICORN_TIMEOUT"].to_i || 60)-end--# important for Ruby 2.0-preload_app true--# Enable this flag to have unicorn test client connections by writing the-# beginning of the HTTP headers before calling the application. This-# prevents calling the application for connections that have disconnected-# while queued. This is only guaranteed to detect clients on the same-# host unicorn runs on, and unlikely to detect disconnects even on a-# fast LAN.-check_client_connection false--initialized = false-before_fork do |server, worker|- unless initialized- Discourse.preload_rails!- Discourse.before_fork-- initialized = true-- supervisor = ENV["UNICORN_SUPERVISOR_PID"].to_i-- if supervisor > 0- Thread.new do- while true- unless File.exist?("/proc/#{supervisor}")- server.logger.error "Kill self supervisor is gone"- Process.kill "TERM", Process.pid- end- sleep 2- end- end- end-- sidekiqs = ENV["UNICORN_SIDEKIQS"].to_i-- if sidekiqs > 0- server.logger.info "starting #{sidekiqs} supervised sidekiqs"-- require "demon/sidekiq"- Demon::Sidekiq.after_fork { DiscourseEvent.trigger(:sidekiq_fork_started) }- Demon::Sidekiq.start(sidekiqs, logger: server.logger)-- if Discourse.enable_sidekiq_logging?- # Trap USR1, so we can re-issue to sidekiq workers- # but chain the default unicorn implementation as well- old_handler =- Signal.trap("USR1") do- old_handler.call-- # We have seen Sidekiq processes getting stuck in production sporadically when log rotation happens.- # The cause is currently unknown but we suspect that it is related to the Unicorn master process and- # Sidekiq demon processes reopening logs at the same time as we noticed that Unicorn worker processes only- # reopen logs after the Unicorn master process is done. To workaround the problem, we are adding an arbitrary- # delay of 1 second to Sidekiq's log reopening procedure. The 1 second delay should be- # more than enough for the Unicorn master process to finish reopening logs.- Demon::Sidekiq.kill("USR2")- end- end- end-- DiscoursePluginRegistry.demon_processes.each do |demon_class|- server.logger.info "starting #{demon_class.prefix} demon"- demon_class.start(1, logger: server.logger)- end-- Thread.new do- while true- begin- sleep 60-- if sidekiqs > 0- Demon::Sidekiq.ensure_running- Demon::Sidekiq.heartbeat_check- Demon::Sidekiq.rss_memory_check- end-- DiscoursePluginRegistry.demon_processes.each { |demon_class| demon_class.ensure_running }- rescue => e- Rails.logger.warn(- "Error in demon processes heartbeat check: #{e}\n#{e.backtrace.join("\n")}",- )- end- end- end-- class ::Unicorn::HttpServer- # Original source: https://github.com/defunkt/unicorn/blob/6c9c442fb6aa12fd871237bc2bb5aec56c5b3538/lib/unicorn/http_server.rb#L477-L496- def murder_lazy_workers- next_sleep = @timeout - 1- now = time_now.to_i- @workers.dup.each_pair do |wpid, worker|- tick = worker.tick- 0 == tick and next # skip workers that haven't processed any clients- diff = now - tick- tmp = @timeout - diff-- # START MONKEY PATCH- if tmp < 2 && !worker.instance_variable_get(:@timing_out_logged)- logger.error do- "worker=#{worker.nr} PID:#{wpid} running too long (#{diff}s), sending USR2 to dump thread backtraces"- end-- worker.instance_variable_set(:@timing_out_logged, true)- kill_worker(:USR2, wpid)- end- # END MONKEY PATCH-- if tmp >= 0- next_sleep > tmp and next_sleep = tmp- next- end- next_sleep = 0- logger.error "worker=#{worker.nr} PID:#{wpid} timeout " \- "(#{diff}s > #{@timeout}s), killing"-- kill_worker(:KILL, wpid) # take no prisoners for timeout violations- end- next_sleep <= 0 ? 1 : next_sleep- end- end- end-- Discourse.redis.close-- # Throttle the master from forking too quickly by sleeping. Due- # to the implementation of standard Unix signal handlers, this- # helps (but does not completely) prevent identical, repeated signals- # from being lost when the receiving process is busy.- sleep 1 if !Rails.env.development?-end--after_fork do |server, worker|- DiscourseEvent.trigger(:web_fork_started)- Discourse.after_unicorn_worker_fork- Discourse.after_fork- SignalTrapLogger.instance.after_fork-- Signal.trap("USR2") do- message = <<~MSG- Unicorn worker received USR2 signal indicating it is about to timeout, dumping backtrace for main thread- #{Thread.current.backtrace&.join("\n")}- MSG-- SignalTrapLogger.instance.log(Rails.logger, message, level: :warn)- end-enddiff --git a/lib/demon/sidekiq.rb b/lib/demon/sidekiq.rbindex 7b010d98fec..7352dbbff39 100644--- a/lib/demon/sidekiq.rb+++ b/lib/demon/sidekiq.rb@@ -112,16 +112,6 @@ class Demon::Sidekiq < ::Demon::Baserequire "sidekiq/cli"cli = Sidekiq::CLI.instance- if defined?(Unicorn)- # Unicorn uses USR1 to indicate that log files have been rotated- Signal.trap("USR1") { reopen_logs }-- Signal.trap("USR2") do- sleep 1- reopen_logs- end- end-options = ["-c",GlobalSetting.sidekiq_workers.to_s,@@ -153,21 +143,4 @@ class Demon::Sidekiq < ::Demon::Baseexit 1end-- private-- def reopen_logs- begin- log_in_trap("Sidekiq reopening logs...")- Unicorn::Util.reopen_logs- log_in_trap("Sidekiq done reopening logs...")- rescue => error- log_in_trap(- "Error encountered while reopening logs: [#{error.class}] #{error.message}\n#{error.backtrace.join("\n")}",- level: :error,- )-- exit 1- end- endenddiff --git a/lib/discourse.rb b/lib/discourse.rbindex 4203ec39f8e..7950d36f848 100644--- a/lib/discourse.rb+++ b/lib/discourse.rb@@ -1010,15 +1010,19 @@ module DiscourseProcess.warmupend- def self.after_unicorn_worker_fork+ # Called in web worker processes after fork to apply worker-specific+ # database variable overrides (e.g. a stricter statement_timeout for+ # web requests than for sidekiq jobs). Configured via GlobalSettings+ # with the `unicorn_worker_db_variables_` prefix.+ def self.apply_worker_db_variables_overridesvariables_overrides = {}- unicorn_worker_db_variables_prefix = "unicorn_worker_db_variables_"+ prefix = "unicorn_worker_db_variables_"GlobalSetting.provider.keys.each do |key|- if key.start_with?(unicorn_worker_db_variables_prefix)- variables_overrides[- key.to_s.sub(unicorn_worker_db_variables_prefix, "").downcase.to_sym- ] = GlobalSetting.public_send(key)+ if key.start_with?(prefix)+ variables_overrides[key.to_s.sub(prefix, "").downcase.to_sym] = GlobalSetting.public_send(+ key,+ )endenddiff --git a/lib/unicorn_logstash_patch.rb b/lib/unicorn_logstash_patch.rbdeleted file mode 100644index 7a23fc8096e..00000000000--- a/lib/unicorn_logstash_patch.rb+++ /dev/null@@ -1,14 +0,0 @@-# frozen_string_literal: true--# See https://github.com/defunkt/unicorn/commit/5f478f5a9a58f72c0a844258b8ee614bf24ea9f7-# Unicorn originally logs backtrace line by line with `exc.backtrace.each { |line| logger.error(line) }`.-# However, that means we get a separate logstash message for each backtrace which isn't what we want. The-# monkey patch here overrides Unicorn's logging of error so that we log the error and backtrace in a-# single message.-module Unicorn- def self.log_error(logger, prefix, exc)- message = exc.message- message = message.dump if /[[:cntrl:]]/ =~ message- logger.error "#{prefix}: #{message} (#{exc.class})\n#{exc.backtrace.join("\n")}"- end-enddiff --git a/script/bench.rb b/script/bench.rbindex 6620dd2e172..cb4ae59437d 100644--- a/script/bench.rb+++ b/script/bench.rb@@ -13,11 +13,10 @@ require "uri"@iterations = 500@best_of = 1@mem_stats = false-@unicorn = false@dump_heap = false@concurrency = 1@skip_asset_bundle = false-@unicorn_workers = 3+@workers = 3opts =OptionParser.new do |o|@@ -42,11 +41,9 @@ opts ="--concurrency [NUM]","Run benchmark with this number of concurrent requests (default: 1)",) { |i| @concurrency = i.to_i }- o.on(- "-w",- "--unicorn_workers [NUM]",- "Run benchmark with this number of unicorn workers (default: 3)",- ) { |i| @unicorn_workers = i.to_i }+ o.on("-w", "--workers [NUM]", "Run benchmark with this number of workers (default: 3)") do |i|+ @workers = i.to_i+ endo.on("-s", "--skip-bundle-assets", "Skip bundling assets") { @skip_asset_bundle = true }o.on(@@ -207,21 +204,14 @@ beginendENV["UNICORN_PORT"] = @port.to_s- ENV["UNICORN_WORKERS"] = @unicorn_workers.to_s+ ENV["UNICORN_WORKERS"] = @workers.to_sFileUtils.mkdir_p(File.join("tmp", "pids"))- pid = spawn("bundle exec unicorn -c config/unicorn.conf.rb")+ pid = spawn("bundle exec pitchfork -c config/pitchfork.conf.rb")- while (- unicorn_master_pid =- `ps aux | grep "unicorn master" | grep -v "grep" | awk '{print $2}'`.strip.to_i- ) == 0- sleep 1- end+ sleep 1 while `pgrep -f "pitchfork.*monitor"`.strip.empty?- while `pgrep -P #{unicorn_master_pid}`.split("\n").map(&:to_i).size != @unicorn_workers.to_i- sleep 1- end+ sleep 1 while `pgrep -f "pitchfork.*worker\\["`.lines.size < @workersputs "Starting benchmark..."@@ -285,7 +275,7 @@ beginputs "Your Results: (note for timings- percentile is first, duration is second in millisecs)"- puts "Unicorn: (workers: #{@unicorn_workers})"+ puts "Pitchfork: (workers: #{@workers})"puts "Include env: #{@include_env}"puts "Iterations: #{@iterations}, Best of: #{@best_of}"puts "Concurrency: #{@concurrency}"@@ -325,15 +315,6 @@ begin"pss_kb" => mem["pss_kb"],).merge(facts)- if @unicorn- child_pids = `ps --ppid #{pid} | awk '{ print $1; }' | grep -v PID`.split("\n")- child_pids.each do |child|- mem = get_mem(child)- results["rss_kb_#{child}"] = mem["rss_kb"]- results["pss_kb_#{child}"] = mem["pss_kb"]- end- end-puts results.to_yamlif @mem_statsdiff --git a/spec/lib/discourse_spec.rb b/spec/lib/discourse_spec.rbindex ab7e6afa7b6..37e26aca1d9 100644--- a/spec/lib/discourse_spec.rb+++ b/spec/lib/discourse_spec.rb@@ -67,7 +67,7 @@ RSpec.describe Discourse doendend- describe ".after_unicorn_worker_fork" do+ describe ".apply_worker_db_variables_overrides" doaround do |example|original_env = ENV.to_hashoriginal_config = ActiveRecord::Base.configurations@@ -95,8 +95,6 @@ RSpec.describe Discourse doit "applies worker-specific database variable overrides in a production environment" dotest_database_config = Rails.application.config.database_configuration["test"]- # In the production environment, `DISCOURSE_` ENV variables are written to the `discourse.conf` file so we need- # to simulate that here in the test environment.temp_discourse_conf = Tempfile.new("discourse.conf")temp_discourse_conf.write <<~TEXTdb_name = #{test_database_config["database"]}@@ -110,7 +108,7 @@ RSpec.describe Discourse doGlobalSetting.configure!(path: temp_discourse_conf.path, use_blank_provider: false)GlobalSetting.load_defaults- Discourse.after_unicorn_worker_fork+ Discourse.apply_worker_db_variables_overridesexpect(ActiveRecord::Base.connection.execute("SHOW statement_timeout").first["statement_timeout"],diff --git a/spec/rails_helper.rb b/spec/rails_helper.rbindex 3e23d64738b..25e46b09a78 100644--- a/spec/rails_helper.rb+++ b/spec/rails_helper.rb@@ -425,16 +425,15 @@ RSpec.configure do |config|(ENV["CAPYBARA_SERVER_PORT"].presence || "31_337").to_i + ENV["TEST_ENV_NUMBER"].to_iend- module IgnoreUnicornCapturedErrors+ module IgnoreServerCapturedErrorsdef raise_server_error!superrescue EOFError, Errno::ECONNRESET, Errno::EPIPE, Errno::ENOTCONN => e- # Ignore these exceptions - caused by client. Handled by unicorn in dev/prod- # https://github.com/defunkt/unicorn/blob/d947cb91cf/lib/unicorn/http_server.rb#L570-L573+ # Ignore these exceptions - caused by client. Handled by the app server in dev/prodendend- Capybara::Session.class_eval { prepend IgnoreUnicornCapturedErrors }+ Capybara::Session.class_eval { prepend IgnoreServerCapturedErrors }module CapybaraTimeoutExtensionclass CapybaraTimedOut < StandardErrordiff --git a/spec/tasks/qunit_cli_spec.rb b/spec/tasks/qunit_cli_spec.rbindex 6095683ff5b..0e745609be4 100644--- a/spec/tasks/qunit_cli_spec.rb+++ b/spec/tasks/qunit_cli_spec.rb@@ -15,7 +15,7 @@ describe "bin/qunit" dostatus: status.exitstatus,args: parsed_args,env: parsed_env,- launched_unicorn: out.include?("[dry-run] skipping unicorn startup"),+ launched_server: out.include?("[dry-run] skipping server startup"),)end@@ -28,7 +28,7 @@ describe "bin/qunit" doit "runs all core tests by default" doresult = runexpect(result.status).to eq(0)- expect(result.launched_unicorn).to eq(false)+ expect(result.launched_server).to eq(false)expect(result.args).to match([@@ -53,7 +53,7 @@ describe "bin/qunit" doit "allows running specific file" doresult = run(core_test_file)expect(result.status).to eq(0)- expect(result.launched_unicorn).to eq(false)+ expect(result.launched_server).to eq(false)expect(result.args).to match([@@ -80,7 +80,7 @@ describe "bin/qunit" doit "allows running all plugin tests" doresult = run("--target", "plugins")expect(result.status).to eq(0)- expect(result.launched_unicorn).to eq(false)+ expect(result.launched_server).to eq(false)expect(result.args).to match([@@ -104,7 +104,7 @@ describe "bin/qunit" doit "allows running tests for multiple plugins" doresult = run("--target", "chat,discourse-local-dates")expect(result.status).to eq(0)- expect(result.launched_unicorn).to eq(false)+ expect(result.launched_server).to eq(false)expect(result.args).to match([@@ -128,7 +128,7 @@ describe "bin/qunit" doit "allows running specific plugin test file" doresult = run(chat_test_file)expect(result.status).to eq(0)- expect(result.launched_unicorn).to eq(false)+ expect(result.launched_server).to eq(false)expect(result.args).to match([@@ -163,9 +163,9 @@ describe "bin/qunit" do)end- it "launches unicorn when using --standalone" do+ it "launches server when using --standalone" doresult = run("--standalone")expect(result.status).to eq(0)- expect(result.launched_unicorn).to eq(true)+ expect(result.launched_server).to eq(true)endend