From f3549291a36157932ad015799aca6d39ccec083d Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 7 Aug 2018 17:13:20 +1000 Subject: [PATCH] DEV: use unicorn in development This commit also cleans up a bunch of pointless noise each time we boot app - narrative was loading i18n cause redefinition of consts - discourse.rb was loaded twice as was auth - bin/unicorn now does all the smart things and boots unicron in dev - bin/rails s will boot unicorn with no params - remove bin/puma which only causes confusion --- bin/puma | 32 ------------------- bin/rails | 5 +++ bin/unicorn | 29 +++++++++++++++++ config/application.rb | 11 ++++--- config/initializers/100-sidekiq.rb | 10 ++++++ config/unicorn.conf.rb | 21 +++++++----- .../autoload/jobs/narrative_init.rb | 1 - 7 files changed, 64 insertions(+), 45 deletions(-) delete mode 100755 bin/puma create mode 100755 bin/unicorn diff --git a/bin/puma b/bin/puma deleted file mode 100755 index 3249ddee001..00000000000 --- a/bin/puma +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env ruby -# -# This file was generated by Bundler. -# -# The application 'puma' is installed as part of a gem, and -# this file is here to facilitate running it. -# - -if ARGV[0] == '--kill-existing' - ARGV.delete_at(0) - - pids = `ps aux | grep puma | grep discourse | grep -v grep | awk '{print $2;}'`.strip - .split("\n") - .map(&:to_i) - .reject { |pid| pid == Process.pid } - - if pids.length > 0 - STDERR.puts "Terminating old version of puma at pid #{pids[0]}" - `kill -9 #{pids[0]}` - sleep 1 - end - -end - -require 'pathname' -ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile", - Pathname.new(__FILE__).realpath) - -require 'rubygems' -require 'bundler/setup' - -load Gem.bin_path('puma', 'puma') diff --git a/bin/rails b/bin/rails index 07396602377..57d0505314d 100755 --- a/bin/rails +++ b/bin/rails @@ -1,4 +1,9 @@ #!/usr/bin/env ruby + +if !ENV["RAILS_ENV"] && (ARGV == ["s"] || ARGV["server"]) + exec File.expand_path("unicorn", __dir__) +end + APP_PATH = File.expand_path('../config/application', __dir__) require_relative '../config/boot' require 'rails/commands' diff --git a/bin/unicorn b/bin/unicorn new file mode 100755 index 00000000000..77c45fa4e96 --- /dev/null +++ b/bin/unicorn @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby + +require 'pathname' +ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile", + Pathname.new(__FILE__).realpath) + +require 'rubygems' +require 'bundler/setup' + +# in development do some fussing around, to automate config +if !ARGV.include?("-E") && + !ARGV.include?("--env") && + (ENV["RAILS_ENV"] == "development" || !ENV["RAILS_ENV"]) + + 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 + + ENV["UNICORN_PORT"] = "9292" + + if !ENV["UNICORN_SIDEKIQS"] + ENV["UNICORN_SIDEKIQS"] = "1" + end +end + +load Gem.bin_path('unicorn', 'unicorn') diff --git a/config/application.rb b/config/application.rb index 2bb727bc01f..44599fa1651 100644 --- a/config/application.rb +++ b/config/application.rb @@ -49,12 +49,15 @@ module Discourse # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. - require 'discourse' - require 'es6_module_transpiler/rails' - require 'js_locale_helper' + # this pattern is somewhat odd but the reloader gets very + # confused here if we load the deps without `lib` it thinks + # discourse.rb is under the discourse folder incorrectly + require_dependency 'lib/discourse' + require_dependency 'lib/es6_module_transpiler/rails' + require_dependency 'lib/js_locale_helper' # tiny file needed by site settings - require 'highlight_js/highlight_js' + require_dependency 'lib/highlight_js/highlight_js' # mocha hates us, active_support/testing/mochaing.rb line 2 is requiring the wrong # require, patched in source, on upgrade remove this diff --git a/config/initializers/100-sidekiq.rb b/config/initializers/100-sidekiq.rb index 38f4fa8aeaa..9f781ff7279 100644 --- a/config/initializers/100-sidekiq.rb +++ b/config/initializers/100-sidekiq.rb @@ -1,5 +1,15 @@ require "sidekiq/pausable" +module Sidekiq + class CLI + private + + def print_banner + # banner takes up too much space + end + end +end + Sidekiq.configure_client do |config| config.redis = Discourse.sidekiq_redis_config end diff --git a/config/unicorn.conf.rb b/config/unicorn.conf.rb index 9c0e51c3d1b..d1d9c44b033 100644 --- a/config/unicorn.conf.rb +++ b/config/unicorn.conf.rb @@ -5,9 +5,6 @@ if ENV["LOGSTASH_UNICORN_URI"] logger DiscourseLogstashLogger.logger(uri: ENV['LOGSTASH_UNICORN_URI'], type: :unicorn) end -# enable out of band gc out of the box, it is low risk and improves perf a lot -ENV['UNICORN_ENABLE_OOBGC'] ||= "1" - discourse_path = File.expand_path(File.expand_path(File.dirname(__FILE__)) + "/../") # tune down if not enough ram @@ -21,14 +18,22 @@ listen (ENV["UNICORN_PORT"] || 3000).to_i # nuke workers after 30 seconds instead of 60 seconds (the default) timeout 30 +if !File.exist?("#{discourse_path}/tmp/pids") + Dir.mkdir("#{discourse_path}/tmp/pids") +end + # feel free to point this anywhere accessible on the filesystem pid (ENV["UNICORN_PID_PATH"] || "#{discourse_path}/tmp/pids/unicorn.pid") -# 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 "#{discourse_path}/log/unicorn.stderr.log" -stdout_path "#{discourse_path}/log/unicorn.stdout.log" +if ENV["RAILS_ENV"] == "development" || !ENV["RAILS_ENV"] + logger Logger.new($stdout) +else + # 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 "#{discourse_path}/log/unicorn.stderr.log" + stdout_path "#{discourse_path}/log/unicorn.stdout.log" +end # important for Ruby 2.0 preload_app true diff --git a/plugins/discourse-narrative-bot/autoload/jobs/narrative_init.rb b/plugins/discourse-narrative-bot/autoload/jobs/narrative_init.rb index 68fb003f112..226ec14ae8f 100644 --- a/plugins/discourse-narrative-bot/autoload/jobs/narrative_init.rb +++ b/plugins/discourse-narrative-bot/autoload/jobs/narrative_init.rb @@ -1,4 +1,3 @@ -require_dependency 'i18n' require_dependency 'user' module Jobs