Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Process memory usage and fix process state update #1516

Merged
merged 6 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion app/models/good_job/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ class Process < BaseRecord
STALE_INTERVAL = 30.seconds
# Interval until the process record is treated as expired
EXPIRED_INTERVAL = 5.minutes
PROCESS_MEMORY = case RUBY_PLATFORM
when /linux/
->(pid) {
File.readlines("/proc/#{pid}/status").each do |line|
noma4i marked this conversation as resolved.
Show resolved Hide resolved
next unless line.start_with?('VmRSS:')

break line.split[1].to_i
end
}
when /darwin|bsd/
->(pid) {
`ps -o pid,rss -p #{pid}`.lines.last.split.last.to_i
}
else
->(_pid) { 0 }
end

self.table_name = 'good_job_processes'
self.implicit_order_column = 'created_at'
Expand Down Expand Up @@ -56,6 +72,10 @@ def self.cleanup
end
end

def self.memory_usage(pid)
(PROCESS_MEMORY.call(pid) / 1024).to_i
end

def self.find_or_create_record(id:, with_advisory_lock: false)
attributes = {
id: id,
Expand Down Expand Up @@ -83,6 +103,7 @@ def self.process_state
{
hostname: Socket.gethostname,
pid: ::Process.pid,
memory: memory_usage(::Process.pid),
proctitle: $PROGRAM_NAME,
preserve_job_records: GoodJob.preserve_job_records,
retry_on_unhandled_error: GoodJob.retry_on_unhandled_error,
Expand All @@ -98,8 +119,8 @@ def self.process_state
end

def refresh
self.state = self.class.process_state
reload # verify the record still exists in the database
self.state = self.class.process_state
update(state: state, updated_at: Time.current)
rescue ActiveRecord::RecordNotFound
@new_record = true
Expand Down
1 change: 1 addition & 0 deletions app/views/good_job/processes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<span class="badge rounded-pill bg-body-secondary text-secondary"><%= process.state["pid"] %></span>
<span class="text-muted small">@</span>
<span class="badge rounded-pill bg-body-secondary text-secondary"><%= process.state["hostname"] %></span>
<span class="badge rounded-pill bg-body-secondary text-secondary"><%= process.state["memory"] %>mb</span>
</div>
</div>
<div class="col">
Expand Down
3 changes: 2 additions & 1 deletion spec/app/models/good_job/process_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
process = described_class.create! state: {}, updated_at: 1.day.ago
expect do
expect(process.refresh).to be true
end.to change(process, :updated_at).to within(1.second).of(Time.current)
end.to change(process, :state)
.and change(process, :updated_at).to within(1.second).of(Time.current)
end

context 'when the record has been deleted elsewhere' do
Expand Down