Commit c4c521b4 authored by Stan Hu's avatar Stan Hu
Browse files

Fix git-upload-pack processes always showing 1 process

When launched via Ruby, `pgrep` is launched via a shell (e.g. `sh -c
pgrep foo bar`). When the `-f` flag is used with pgrep with a regex that
has multiple words, it appears that that the shell is always matched.

We can work around this issue by getting back the process name and
filtering out `sh` from the list.

Closes https://gitlab.com/gitlab-org/gitlab-monitor/issues/44
parent 0b574f14
Pipeline #99627 passed with stage
in 1 minute and 11 seconds
# frozen_string_literal: true
module GitLab
module Monitor
# Simple time wrapper that provides a to_i and wraps the execution result
......@@ -48,10 +50,19 @@ module GitLab
module_function :deep_transform_keys_in_object
def pgrep(pattern)
`pgrep -f "#{pattern}"`.split("\n")
# pgrep will include the PID of the shell, so strip that out
exec_pgrep(pattern).split("\n").each_with_object([]) do |line, arr|
pid, name = line.split(" ")
arr << pid if name != "sh"
end
end
module_function :pgrep
def exec_pgrep(pattern)
`pgrep -fl "#{pattern}"`
end
module_function :exec_pgrep
def system_uptime
File.read("/proc/uptime").split(" ")[0].to_f
end
......
......@@ -5,3 +5,11 @@ describe GitLab::Monitor::TimeTracker do
expect(subject.track { sleep 0.1 }.time).to satisfy { |v| v >= 0.1 }
end
end
describe GitLab::Monitor::Utils do
it "excludes extraneous PIDs" do
allow(described_class).to receive(:exec_pgrep).and_return("12345 my-process\n98765 sh\n")
expect(described_class.pgrep("some-process")).to eq(["12345"])
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment