discourse/bin/pitchfork
Loïc Guitaut 6b243ffdfd
DEV: Remove Unicorn web server in favor of Pitchfork (#39032)
Pitchfork has been the default web server for some time now. This
removes Unicorn entirely to simplify the codebase and unblock future
improvements (like Rack 3).

Notable changes beyond the straightforward removal:

- `Discourse.after_unicorn_worker_fork` →
`Discourse.apply_worker_db_variables_overrides`: renamed and wired into
pitchfork.conf.rb's `after_worker_fork`. This actually *fixes*
per-worker DB variable overrides (`unicorn_worker_db_variables_*`) which
were never called under Pitchfork.
- `bin/ember-cli`: `--unicorn` flag renamed to `--server` (`-u` kept).
- `lib/demon/sidekiq.rb`: removed Unicorn-specific USR1/USR2 signal
handlers and `reopen_logs` (called `Unicorn::Util.reopen_logs`), which
were already dead code under Pitchfork.

Intentionally kept unchanged:
- `config/unicorn_launcher` (used by Docker images, separate effort)
- `docker_manager` plugin (separate repo)
- `UNICORN_*` env vars (renaming deferred)
- Rack < 3 constraint (separate PR)
2026-04-01 15:04:59 +02:00

94 lines
2.2 KiB
Ruby
Executable file

#!/usr/bin/env ruby
# frozen_string_literal: true
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
if !ARGV.include?("-c") && !ARGV.include?("--config-file")
ARGV.push("-c")
ARGV.push(File.expand_path("../../config/pitchfork.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("pitchfork", "pitchfork") }
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 pitchfork:
#
# pkill -USR2 -f 'ruby bin/pitchfork'
#
# This is handy if you want to bind a key to restarting pitchfork in dev
if dev_mode
DEV_SUPERVISOR_PID = Process.pid
restart = true
while restart
restart = false
pid = fork { load Gem.bin_path("pitchfork", "pitchfork") }
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 PITCHFORK"
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("pitchfork", "pitchfork")
end