diff --git a/nginx_stage/lib/nginx_stage.rb b/nginx_stage/lib/nginx_stage.rb index 3574ffaf8a..c7f5387c25 100644 --- a/nginx_stage/lib/nginx_stage.rb +++ b/nginx_stage/lib/nginx_stage.rb @@ -5,6 +5,7 @@ require_relative "nginx_stage/pid_file" require_relative "nginx_stage/socket_file" require_relative "nginx_stage/secret_key_base_file" +require_relative "nginx_stage/session_finder" require_relative "nginx_stage/views/pun_config_view" require_relative "nginx_stage/views/app_config_view" require_relative "nginx_stage/generator" diff --git a/nginx_stage/lib/nginx_stage/generators/nginx_clean_generator.rb b/nginx_stage/lib/nginx_stage/generators/nginx_clean_generator.rb index f745e460f5..60c2ea67ee 100644 --- a/nginx_stage/lib/nginx_stage/generators/nginx_clean_generator.rb +++ b/nginx_stage/lib/nginx_stage/generators/nginx_clean_generator.rb @@ -2,6 +2,9 @@ module NginxStage # This generator cleans all running per-user NGINX processes that are # inactive (i.e., not active connections). class NginxCleanGenerator < Generator + + include NginxStage::SessionFinder + desc 'Clean all user running PUNs with no active connections' footer <<-EOF.gsub(/^ {4}/, '') @@ -59,8 +62,9 @@ class NginxCleanGenerator < Generator next if (user && user != u.to_s) pid_path = PidFile.new NginxStage.pun_pid_path(user: u) socket = SocketFile.new NginxStage.pun_socket_path(user: u) - cleanup_stale_files(pid_path, socket) unless pid_path.running_process? - if socket.sessions.zero? || force + sessions = session_count(u) + cleanup_stale_files(pid_path, socket) unless pid_path.running_process? + if sessions.zero? || force puts u if !skip_nginx NginxStage.clean_nginx_env(user: user) diff --git a/nginx_stage/lib/nginx_stage/session_finder.rb b/nginx_stage/lib/nginx_stage/session_finder.rb new file mode 100644 index 0000000000..2047b7d5d3 --- /dev/null +++ b/nginx_stage/lib/nginx_stage/session_finder.rb @@ -0,0 +1,9 @@ +module NginxStage + module SessionFinder + def session_count(user) + `ps -o cmd -u #{user}`.split("\n").select do |command| + command.match?(/Passenger [\w]+App:/) + end.count + end + end +end \ No newline at end of file