Last active
August 29, 2015 14:06
-
-
Save KieranP/d7b09698dfbb8ba1554c to your computer and use it in GitHub Desktop.
DelayedJob BaseJob Template - With Memory Usage Logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseJob | |
def self.queue(*args) | |
options = args.extract_options! | |
Delayed::Job.enqueue(new(*args), options) | |
end | |
def initialize(*args) | |
@args = args | |
end | |
def error(job, exception) | |
raise exception if Rails.env.test? | |
logger.error "#{Time.now.utc} : #{exception.class.name} : #{exception}" | |
Airbrake.notify(exception) | |
end | |
def before(job) | |
@starting_memory = fetch_memory_usage | |
@starting_time = Time.now.utc | |
end | |
def after(job) | |
@args = nil | |
GC.start unless Rails.env.test? | |
@finishing_memory = fetch_memory_usage | |
@finishing_time = Time.now.utc | |
diff = (@finishing_memory - @starting_memory).round(3) | |
time_taken = (@finishing_time - @starting_time).round(1) | |
job_name = (job.handler =~ /:(\w+)\n/) && $1 | |
memory_logger.warn "#{Time.now.utc} : #{Process.pid} : MEMORY READOUT : JOB=#{job_name}, TIME=#{time_taken}s, BEFORE=#{@starting_memory}Mb, AFTER=#{@finishing_memory}Mb, DIFF=#{diff}Mb" if diff > 0.1 | |
end | |
def logger | |
@logger ||= Logger.new(Rails.root.join('log/delayed_job.log')) | |
end | |
def memory_logger | |
@memory_logger ||= Logger.new(Rails.root.join('log/delayed_job_memory.log')) | |
end | |
def fetch_memory_usage | |
(`ps -o rss= -p #{Process.pid}`.chomp.to_f / 1024).round(3) rescue 0.0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment