Skip to content
This repository has been archived by the owner on Nov 20, 2019. It is now read-only.

Commit

Permalink
Merge pull request #171 from CDL-Dryad/passenger-killer
Browse files Browse the repository at this point in the history
Take out the passenger startup to kill after request and script to kill bloat
  • Loading branch information
sfisher authored Nov 5, 2019
2 parents bc0119e + fb6d4d8 commit df8af90
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
5 changes: 3 additions & 2 deletions config/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@
# https://www.phusionpassenger.com/library/config/standalone/reference/#--max-requests-max_requests
execute "cd #{deploy_to}/current; bundle exec passenger start -d --environment #{fetch(:rails_env)} "\
"--pid-file #{fetch(:passenger_pid)} -p #{fetch(:passenger_port)} "\
"--log-file #{fetch(:passenger_log)} --pool-idle-time 86400 --max-pool-size=#{fetch(:passenger_pool)} "\
"--max-requests=500"
"--log-file #{fetch(:passenger_log)} --pool-idle-time 86400 --max-pool-size=#{fetch(:passenger_pool)} "
# "--max-requests=500" -- can't do this since it kills submissions, but we should manually kill if a process gets too
# big and seems to have a memory leak, unfortunately, this feature in passenger is a "pro" feature and isn't free.
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/tasks/dev_ops.rake
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'yaml'
require_relative 'dev_ops/passenger'

# rubocop:disable Metrics/BlockLength
namespace :dev_ops do
Expand Down Expand Up @@ -90,5 +91,17 @@ namespace :dev_ops do
exec command
end

desc 'Kill large memory usage passenger processes'
task kill_bloated_passengers: :environment do
passenger = DevOps::Passenger.new

passenger.kill_bloated_pids! unless passenger.items_submitting?

# puts "passenger.status: #{passenger.status}"
# puts "out: \n #{passenger.stdout}"
# puts passenger.bloated_pids
# puts passenger.items_submitting?
end

end
# rubocop:enable Metrics/BlockLength
52 changes: 52 additions & 0 deletions lib/tasks/dev_ops/passenger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'open3'
require 'byebug'

module DevOps
class Passenger

attr_reader :status, :stdout

BLOATED_MB = 600

def initialize
@stdout, @stderr, @status = Open3.capture3('passenger-status')
@bloated_pids = []
end

def not_running?
@stderr.include?("Phusion Passenger doesn't seem to be running")
end

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def bloated_pids
# return (empty) array if not running or return array it is already cached with bloated pids
return @bloated_pids if not_running? || @bloated_pids.length.positive?

process_sections = @stdout.split('* PID: ')
process_sections = process_sections[1..-1] # the second to the last of the split sections

process_sections.each do |section|
matches = section.match(/^(\d+).+Memory *: *(\S+)/m)
pid = matches[1]
memory = 0
memory = matches[2].to_i if matches[2].end_with?('M')
memory = matches[2].to_i * 1000 if matches[2].end_with?('G')
@bloated_pids.push(pid) if memory > BLOATED_MB
end
@bloated_pids
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

def kill_bloated_pids!
bloated_pids.each do |my_pid|
`kill #{my_pid}`
end
end

def items_submitting?
StashEngine::RepoQueueState.latest_per_resource.where(state: 'processing')
.where(hostname: StashEngine.repository.class.hostname).count.positive?
end

end
end

0 comments on commit df8af90

Please sign in to comment.