lib/puma/launcher.rb in piesync-puma-3.12.6.1 vs lib/puma/launcher.rb in piesync-puma-5.4.0.1

- old
+ new

@@ -1,15 +1,12 @@ # frozen_string_literal: true require 'puma/events' require 'puma/detect' - require 'puma/cluster' require 'puma/single' - require 'puma/const' - require 'puma/binder' module Puma # Puma::Launcher is the single entry point for starting a Puma server based on user # configuration. It is responsible for taking user supplied arguments and resolving them @@ -48,51 +45,59 @@ @events = launcher_args[:events] || Events::DEFAULT @argv = launcher_args[:argv] || [] @original_argv = @argv.dup @config = conf - @binder = Binder.new(@events) - @binder.import_from_env + @binder = Binder.new(@events, conf) + @binder.create_inherited_fds(ENV).each { |k| ENV.delete k } + @binder.create_activated_fds(ENV).each { |k| ENV.delete k } @environment = conf.environment # Advertise the Configuration Puma.cli_config = @config if defined?(Puma.cli_config) @config.load + if @config.options[:bind_to_activated_sockets] + @config.options[:binds] = @binder.synthesize_binds_from_activated_fs( + @config.options[:binds], + @config.options[:bind_to_activated_sockets] == 'only' + ) + end + @options = @config.options @config.clamp + @events.formatter = Events::PidFormatter.new if clustered? + @events.formatter = options[:log_formatter] if @options[:log_formatter] + generate_restart_data if clustered? && !Process.respond_to?(:fork) unsupported "worker mode not supported on #{RUBY_ENGINE} on this platform" end - if @options[:daemon] && Puma.windows? - unsupported 'daemon mode not supported on Windows' - end - Dir.chdir(@restart_dir) prune_bundler if prune_bundler? @environment = @options[:environment] if @options[:environment] set_rack_environment if clustered? - @events.formatter = Events::PidFormatter.new @options[:logger] = @events @runner = Cluster.new(self, @events) else @runner = Single.new(self, @events) end Puma.stats_object = @runner @status = :run + + log_config if ENV['PUMA_LOG_CONFIG'] end attr_reader :binder, :events, :config, :options, :restart_dir # Return stats about the server @@ -104,41 +109,30 @@ # the server def write_state write_pid path = @options[:state] + permission = @options[:state_permission] return unless path require 'puma/state_file' sf = StateFile.new sf.pid = Process.pid sf.control_url = @options[:control_url] sf.control_auth_token = @options[:control_auth_token] + sf.running_from = File.expand_path('.') - sf.save path + sf.save path, permission end # Delete the configured pidfile def delete_pidfile path = @options[:pidfile] File.unlink(path) if path && File.exist?(path) end - # If configured, write the pid of the current process out - # to a file. - def write_pid - path = @options[:pidfile] - return unless path - - File.open(path, 'w') { |f| f.puts Process.pid } - cur = Process.pid - at_exit do - delete_pidfile if cur == Process.pid - end - end - # Begin async shutdown of the server def halt @status = :halt @runner.halt end @@ -181,49 +175,90 @@ @config.plugins.fire_starts self setup_signals set_process_title + integrate_with_systemd @runner.run case @status when :halt log "* Stopping immediately!" + @runner.stop_control when :run, :stop graceful_stop when :restart log "* Restarting..." ENV.replace(previous_env) - @runner.before_restart + @runner.stop_control restart! when :exit # nothing end + close_binder_listeners unless @status == :restart end - # Return which tcp port the launcher is using, if it's using TCP - def connected_port - @binder.connected_port + # Return all tcp ports the launcher may be using, TCP or SSL + # @!attribute [r] connected_ports + # @version 5.0.0 + def connected_ports + @binder.connected_ports end + # @!attribute [r] restart_args def restart_args cmd = @options[:restart_cmd] if cmd cmd.split(' ') + @original_argv else @restart_argv end end + def close_binder_listeners + @runner.close_control_listeners + @binder.close_listeners + unless @status == :restart + log "=== puma shutdown: #{Time.now} ===" + log "- Goodbye!" + end + end + + # @!attribute [r] thread_status + # @version 5.0.0 + def thread_status + Thread.list.each do |thread| + name = "Thread: TID-#{thread.object_id.to_s(36)}" + name += " #{thread['label']}" if thread['label'] + name += " #{thread.name}" if thread.respond_to?(:name) && thread.name + backtrace = thread.backtrace || ["<no backtrace available>"] + + yield name, backtrace + end + end + private + # If configured, write the pid of the current process out + # to a file. + def write_pid + path = @options[:pidfile] + return unless path + cur_pid = Process.pid + File.write path, cur_pid, mode: 'wb:UTF-8' + at_exit do + delete_pidfile if cur_pid == Process.pid + end + end + def reload_worker_directory @runner.reload_worker_directory if @runner.respond_to?(:reload_worker_directory) end def restart! - @config.run_hooks :on_restart, self + @events.fire_on_restart! + @config.run_hooks :on_restart, self, @events if Puma.jruby? close_binder_listeners require 'puma/jruby_restart' @@ -233,52 +268,102 @@ argv = restart_args Dir.chdir(@restart_dir) Kernel.exec(*argv) else - redirects = {:close_others => true} - @binder.listeners.each_with_index do |(l, io), i| - ENV["PUMA_INHERIT_#{i}"] = "#{io.to_i}:#{l}" - redirects[io.to_i] = io.to_i - end - argv = restart_args Dir.chdir(@restart_dir) - argv += [redirects] if RUBY_VERSION >= '1.9' + ENV.update(@binder.redirects_for_restart_env) + argv += [@binder.redirects_for_restart] Kernel.exec(*argv) end end - def prune_bundler - return unless defined?(Bundler) - puma = Bundler.rubygems.loaded_specs("puma") - dirs = puma.require_paths.map { |x| File.join(puma.full_gem_path, x) } + # @!attribute [r] files_to_require_after_prune + def files_to_require_after_prune + puma = spec_for_gem("puma") + + require_paths_for_gem(puma) + extra_runtime_deps_directories + end + + # @!attribute [r] extra_runtime_deps_directories + def extra_runtime_deps_directories + Array(@options[:extra_runtime_dependencies]).map do |d_name| + if (spec = spec_for_gem(d_name)) + require_paths_for_gem(spec) + else + log "* Could not load extra dependency: #{d_name}" + nil + end + end.flatten.compact + end + + # @!attribute [r] puma_wild_location + def puma_wild_location + puma = spec_for_gem("puma") + dirs = require_paths_for_gem(puma) puma_lib_dir = dirs.detect { |x| File.exist? File.join(x, '../bin/puma-wild') } + File.expand_path(File.join(puma_lib_dir, "../bin/puma-wild")) + end - unless puma_lib_dir + def prune_bundler + return if ENV['PUMA_BUNDLER_PRUNED'] + return unless defined?(Bundler) + require_rubygems_min_version!(Gem::Version.new("2.2"), "prune_bundler") + unless puma_wild_location log "! Unable to prune Bundler environment, continuing" return end - deps = puma.runtime_dependencies.map do |d| - spec = Bundler.rubygems.loaded_specs(d.name) - "#{d.name}:#{spec.version.to_s}" - end + dirs = files_to_require_after_prune log '* Pruning Bundler environment' home = ENV['GEM_HOME'] - Bundler.with_clean_env do + bundle_gemfile = Bundler.original_env['BUNDLE_GEMFILE'] + with_unbundled_env do ENV['GEM_HOME'] = home + ENV['BUNDLE_GEMFILE'] = bundle_gemfile ENV['PUMA_BUNDLER_PRUNED'] = '1' - wild = File.expand_path(File.join(puma_lib_dir, "../bin/puma-wild")) - args = [Gem.ruby, wild, '-I', dirs.join(':'), deps.join(',')] + @original_argv + args = [Gem.ruby, puma_wild_location, '-I', dirs.join(':')] + @original_argv # Ruby 2.0+ defaults to true which breaks socket activation - args += [{:close_others => false}] if RUBY_VERSION >= '2.0' + args += [{:close_others => false}] Kernel.exec(*args) end end + # + # Puma's systemd integration allows Puma to inform systemd: + # 1. when it has successfully started + # 2. when it is starting shutdown + # 3. periodically for a liveness check with a watchdog thread + # + + def integrate_with_systemd + return unless ENV["NOTIFY_SOCKET"] + + begin + require 'puma/systemd' + rescue LoadError + log "Systemd integration failed. It looks like you're trying to use systemd notify but don't have sd_notify gem installed" + return + end + + log "* Enabling systemd notification integration" + + systemd = Systemd.new(@events) + systemd.hook_events + systemd.start_watchdog + end + + def spec_for_gem(gem_name) + Bundler.rubygems.loaded_specs(gem_name) + end + + def require_paths_for_gem(gem_spec) + gem_spec.full_require_paths + end + def log(str) @events.log str end def clustered? @@ -289,19 +374,19 @@ @events.error(str) raise UnsupportedOption end def graceful_stop + @events.fire_on_stopped! @runner.stop_blocked - log "=== puma shutdown: #{Time.now} ===" - log "- Goodbye!" end def set_process_title Process.respond_to?(:setproctitle) ? Process.setproctitle(title) : $0 = title end + # @!attribute [r] title def title buffer = "puma #{Puma::Const::VERSION} (#{@options[:binds].join(',')})" buffer += " [#{@options[:tag]}]" if @options[:tag] && !@options[:tag].empty? buffer end @@ -309,28 +394,19 @@ def set_rack_environment @options[:environment] = environment ENV['RACK_ENV'] = environment end + # @!attribute [r] environment def environment @environment end def prune_bundler? @options[:prune_bundler] && clustered? && !@options[:preload_app] end - def close_binder_listeners - @binder.listeners.each do |l, io| - io.close - uri = URI.parse(l) - next unless uri.scheme == 'unix' - File.unlink("#{uri.host}#{uri.path}") - end - end - - def generate_restart_data if dir = @options[:directory] @restart_dir = dir elsif Puma.windows? @@ -395,24 +471,18 @@ begin Signal.trap "SIGTERM" do graceful_stop - raise SignalException, "SIGTERM" + raise(SignalException, "SIGTERM") if @options[:raise_exception_on_sigterm] end rescue Exception log "*** SIGTERM not implemented, signal based gracefully stopping unavailable!" end begin Signal.trap "SIGINT" do - if Puma.jruby? - @status = :exit - graceful_stop - exit - end - stop end rescue Exception log "*** SIGINT not implemented, signal based gracefully stopping unavailable!" end @@ -426,8 +496,48 @@ end end rescue Exception log "*** SIGHUP not implemented, signal based logs reopening unavailable!" end + + begin + unless Puma.jruby? # INFO in use by JVM already + Signal.trap "SIGINFO" do + thread_status do |name, backtrace| + @events.log name + @events.log backtrace.map { |bt| " #{bt}" } + end + end + end + rescue Exception + # Not going to log this one, as SIGINFO is *BSD only and would be pretty annoying + # to see this constantly on Linux. + end + end + + def require_rubygems_min_version!(min_version, feature) + return if min_version <= Gem::Version.new(Gem::VERSION) + + raise "#{feature} is not supported on your version of RubyGems. " \ + "You must have RubyGems #{min_version}+ to use this feature." + end + + # @version 5.0.0 + def with_unbundled_env + bundler_ver = Gem::Version.new(Bundler::VERSION) + if bundler_ver < Gem::Version.new('2.1.0') + Bundler.with_clean_env { yield } + else + Bundler.with_unbundled_env { yield } + end + end + + def log_config + log "Configuration:" + + @config.final_options + .each { |config_key, value| log "- #{config_key}: #{value}" } + + log "\n" end end end