Skip to content

Instantly share code, notes, and snippets.

@rlburkes
Last active August 29, 2015 14:08
Show Gist options
  • Save rlburkes/2c3e06be0c2a8ea3340f to your computer and use it in GitHub Desktop.
Save rlburkes/2c3e06be0c2a8ea3340f to your computer and use it in GitHub Desktop.
Example of what Delayed::Plugins::YouthInAsiaPlugin might look like
# Automatically kill jobs' worker when it finishes
# if it used more memory than allotted.
# Ali-G: Lets talk about doctors who is ending the life of people who is suffering.
# Ali-G What’s it got to do with the youth in Asia I mean it aiint their fault these peoples is dying, they’s thousands of miles away.
# Doctor: Euthanasia means mercy killing, it literally means dying well. We're not talking about the youth in Asia, (or) the youth in Africa.
# Ali-G: But why is you blaming that on the asian kids or whatever?
module Delayed
module Plugins
class YouthInAsiaPlugin < Delayed::Plugin
# Add an additional pad factor to the memory sample.
MEMORY_THRESHOLD_FACTOR = 1.10
callbacks do |lifecycle|
lifecycle.after(:perform) do |worker, job|
after_perform_sample = memory_sample
# Heroku workers don’t have knowledge of their dyno size by default. We wrote a mixin to
# extend a worker to have knowledge of its underlying dyno metadata. We fetch the metadata from the
# Heroku API when the worker is started. If you arent using heroku you should be able to access
# memory capacity via a shell command, or java vm argument.
worker.stop if worker.exceeds_memory_ceiling?(after_perform_sample * MEMORY_THRESHOLD_FACTOR)
end
end
private
# Sample is environment dependent
# JRuby would use something like:
# java.lang.Runtime.getRuntime.totalMemory / (1024 * 1024).to_f
# Most unix systems will use some variant of the process status ‘ps’ command
# `ps -o rss #{$$}`.split("\n")[1].to_f / 1024.0
# Heroku is using a linux container so we’ll fetch it from the proc status file
def self.memory_sample
if File.open("/proc/#{$$}/status", "r") {|f| f.read } =~ /RSS:\s*(\d+) kB/i
return $1.to_f / 1024.0
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment