Skip to content

Commit

Permalink
remove dep. Use Sidekiq approach
Browse files Browse the repository at this point in the history
  • Loading branch information
noma4i committed Oct 14, 2024
1 parent 2060aa6 commit 924ef82
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ PATH
activerecord (>= 6.1.0)
concurrent-ruby (>= 1.3.1)
fugit (>= 1.11.0)
get_process_mem (>= 1.0.0)
railties (>= 6.1.0)
thor (>= 1.0.0)

Expand Down Expand Up @@ -194,9 +193,6 @@ GEM
et-orbi (~> 1, >= 1.2.11)
raabro (~> 1.4)
gem-release (2.2.2)
get_process_mem (1.0.0)
bigdecimal (>= 2.0)
ffi (~> 1.0)
github_changelog_generator (1.16.4)
activesupport
async (>= 1.25.0)
Expand Down
23 changes: 21 additions & 2 deletions app/models/good_job/process.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require 'socket'
require 'get_process_mem'

module GoodJob # :nodoc:
# Active Record model that represents a GoodJob capsule/process (either async or CLI).
Expand All @@ -13,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|
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 @@ -57,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 @@ -84,7 +103,7 @@ def self.process_state
{
hostname: Socket.gethostname,
pid: ::Process.pid,
memory: GetProcessMem.new.mb.to_i,
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 Down
1 change: 0 additions & 1 deletion good_job.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ Gem::Specification.new do |spec|
spec.add_dependency "fugit", ">= 1.11.0"
spec.add_dependency "railties", ">= 6.1.0"
spec.add_dependency "thor", ">= 1.0.0"
spec.add_dependency "get_process_mem", ">= 1.0.0"

spec.add_development_dependency "capybara"
spec.add_development_dependency "kramdown"
Expand Down

0 comments on commit 924ef82

Please sign in to comment.