2018-08-07 15:13:20 +08:00
|
|
|
#!/usr/bin/env ruby
|
2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
2018-08-07 15:13:20 +08:00
|
|
|
|
|
|
|
require 'pathname'
|
|
|
|
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
|
|
|
Pathname.new(__FILE__).realpath)
|
2019-11-19 07:15:09 +08:00
|
|
|
RAILS_ROOT = File.expand_path("../../", Pathname.new(__FILE__).realpath)
|
2018-08-07 15:13:20 +08:00
|
|
|
|
2021-05-05 20:33:11 +08:00
|
|
|
if defined? Bundler
|
|
|
|
STDERR.puts <<~MESSAGE
|
|
|
|
WARNING: Using `bundle exec` to start the server is unnecessary, and will make startup slower. Use `bin/rails s` or `bin/unicorn`.
|
|
|
|
MESSAGE
|
|
|
|
end
|
|
|
|
|
2018-08-07 15:13:20 +08:00
|
|
|
require 'rubygems'
|
|
|
|
require 'bundler/setup'
|
2019-11-19 07:15:09 +08:00
|
|
|
require 'digest'
|
2020-12-21 13:31:06 +08:00
|
|
|
require 'fileutils'
|
2018-08-07 15:13:20 +08:00
|
|
|
|
2019-09-11 15:35:58 +08:00
|
|
|
dev_mode = false
|
|
|
|
|
2019-11-19 07:15:09 +08:00
|
|
|
def ensure_cache_clean!
|
2021-06-09 10:44:33 +08:00
|
|
|
_all_plugin_directories = Pathname.new(RAILS_ROOT + '/plugins').children.select(&:directory?)
|
2019-11-20 06:47:51 +08:00
|
|
|
core_git_sha = `git rev-parse HEAD`.strip
|
|
|
|
plugins_combined_git_sha = `git ls-files -s plugins | git hash-object --stdin`.strip
|
2024-10-29 23:17:59 +08:00
|
|
|
client_locale_paths_digest =
|
|
|
|
Digest::SHA1.hexdigest(Dir.glob("#{RAILS_ROOT}/plugins/*/config/locales/client.*.yml").join)
|
|
|
|
super_sha =
|
|
|
|
Digest::SHA1.hexdigest(core_git_sha + plugins_combined_git_sha + client_locale_paths_digest)
|
2019-11-19 07:15:09 +08:00
|
|
|
hash_file = "#{RAILS_ROOT}/tmp/plugin-hash"
|
|
|
|
|
2022-01-06 01:45:08 +08:00
|
|
|
old_hash = File.exist?(hash_file) ? File.read(hash_file) : nil
|
2019-11-19 07:15:09 +08:00
|
|
|
|
2019-11-20 06:47:51 +08:00
|
|
|
if old_hash && old_hash != super_sha
|
2023-11-29 01:02:27 +08:00
|
|
|
FileUtils.rm_rf("#{RAILS_ROOT}/tmp/cache")
|
2019-11-19 07:15:09 +08:00
|
|
|
end
|
|
|
|
|
2019-11-20 06:29:34 +08:00
|
|
|
FileUtils.mkdir_p(RAILS_ROOT + "/tmp")
|
2019-11-20 06:47:51 +08:00
|
|
|
File.write(hash_file, super_sha)
|
2019-11-19 07:15:09 +08:00
|
|
|
end
|
|
|
|
|
2018-08-07 15:13:20 +08:00
|
|
|
# in development do some fussing around, to automate config
|
|
|
|
if !ARGV.include?("-E") &&
|
|
|
|
!ARGV.include?("--env") &&
|
2021-09-22 00:14:10 +08:00
|
|
|
(["development", "test"].include?(ENV["RAILS_ENV"]) || !ENV["RAILS_ENV"])
|
2018-08-07 15:13:20 +08:00
|
|
|
|
2019-09-11 15:35:58 +08:00
|
|
|
dev_mode = true
|
|
|
|
|
2018-08-07 15:13:20 +08:00
|
|
|
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
|
|
|
|
|
2018-08-15 08:35:24 +08:00
|
|
|
# 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
|
|
|
|
|
2018-08-07 16:20:08 +08:00
|
|
|
ENV["UNICORN_PORT"] ||= "9292"
|
2018-08-17 08:01:44 +08:00
|
|
|
|
|
|
|
if ARGV.delete("-x")
|
|
|
|
puts "Running without sidekiq"
|
|
|
|
ENV["UNICORN_SIDEKIQS"] = "0"
|
|
|
|
end
|
|
|
|
|
2018-08-07 16:20:08 +08:00
|
|
|
ENV["UNICORN_SIDEKIQS"] ||= "1"
|
2018-08-07 16:27:44 +08:00
|
|
|
|
2019-11-19 07:15:09 +08:00
|
|
|
ensure_cache_clean!
|
2018-08-07 15:13:20 +08:00
|
|
|
end
|
|
|
|
|
2018-08-17 08:01:44 +08:00
|
|
|
if ARGV.include?("--help")
|
|
|
|
fork do
|
|
|
|
load Gem.bin_path('unicorn', 'unicorn')
|
|
|
|
end
|
|
|
|
Process.wait
|
|
|
|
puts "Extra Discourse Options:"
|
|
|
|
puts " -x run without sidekiq"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2022-01-09 06:39:46 +08:00
|
|
|
# this dev_mode hackery enables the following to be used to restart unicorn:
|
2019-09-11 15:35:58 +08:00
|
|
|
#
|
2019-09-11 15:43:36 +08:00
|
|
|
# pkill -USR2 -f 'ruby bin/unicorn'
|
2019-09-11 15:35:58 +08:00
|
|
|
#
|
|
|
|
# This is handy if you want to bind a key to restarting unicorn in dev
|
|
|
|
|
|
|
|
if dev_mode
|
2022-01-09 06:39:46 +08:00
|
|
|
UNICORN_DEV_SUPERVISOR_PID = Process.pid
|
2021-05-15 01:17:31 +08:00
|
|
|
|
2019-09-11 15:35:58 +08:00
|
|
|
restart = true
|
|
|
|
while restart
|
|
|
|
restart = false
|
|
|
|
pid = fork do
|
|
|
|
load Gem.bin_path('unicorn', 'unicorn')
|
|
|
|
end
|
|
|
|
done = false
|
|
|
|
|
|
|
|
Signal.trap('INT') do
|
|
|
|
# wait for parent to be done
|
|
|
|
end
|
|
|
|
|
|
|
|
Signal.trap('USR2') do
|
|
|
|
Process.kill('QUIT', pid)
|
|
|
|
puts "RESTARTING UNICORN"
|
|
|
|
restart = true
|
|
|
|
end
|
|
|
|
|
2022-09-03 05:51:51 +08:00
|
|
|
Signal.trap("TERM") do
|
|
|
|
Process.kill('TERM', pid)
|
|
|
|
end
|
|
|
|
|
2019-09-11 15:35:58 +08:00
|
|
|
while !done
|
|
|
|
sleep 1
|
|
|
|
done = Process.waitpid(pid, Process::WNOHANG)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
load Gem.bin_path('unicorn', 'unicorn')
|
|
|
|
end
|