Skip to content

Instantly share code, notes, and snippets.

@ilkelma
Created September 11, 2018 16:42
Show Gist options
  • Save ilkelma/1557c3751cc69a6cae322e20772a190d to your computer and use it in GitHub Desktop.
Save ilkelma/1557c3751cc69a6cae322e20772a190d to your computer and use it in GitHub Desktop.
interview coding exercise
#!/usr/bin/env ruby
passenger_status = %x( ./passenger-status.rb )
lines = passenger_status.split("*")
# lines = passenger_status.match /Uptime.*CPU/m
# puts lines
# /Uptime.*CPU/
# lines.select! { |line| line =~ /^\s*\*/ }
lines = lines.drop(1)
passenger_processes = lines.map do |line|
line = line.split(/:|\s/)
# {
# :pid => line[1].rstrip,
# :sessions => line[3].to_i,
# :cpu => line[10].gsub("%", "").to_i,
# :memory => line[13].gsub("M", "").to_i
# }
# if line[4].to_i > 5
# puts "Killing #{line[2]} with #{line[4]} active sessions"
# end
line.each_with_index do |token, i|
puts "#{token} #{i}"
end
end
passenger_processes.each do |process|
if process[:memory] > 700
puts "Killing process #{process[:pid]} with memory usage #{process[:memory]}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment