Skip to content

Commit

Permalink
use ps to count sessions instead of lsof
Browse files Browse the repository at this point in the history
  • Loading branch information
johrstrom committed Apr 12, 2024
1 parent 12375e0 commit 6a0b99a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions nginx_stage/lib/nginx_stage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}/, '')
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions nginx_stage/lib/nginx_stage/session_finder.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6a0b99a

Please sign in to comment.