class Qless::Middleware::MemoryUsageMonitor

Monitors the memory usage of the Qless worker, instructing it to shutdown when memory exceeds the given :max_memory threshold.

Constants

SHELL_OUT_FOR_MEMORY

Public Class Methods

current_usage_in_kb() click to toggle source

OS X tends to return maxrss in Bytes.

# File lib/qless/middleware/memory_usage_monitor.rb, line 49
def self.current_usage_in_kb
  Process.rusage.maxrss / 1024
end
new(options) click to toggle source
Calls superclass method
# File lib/qless/middleware/memory_usage_monitor.rb, line 7
def initialize(options)
  max_memory = options.fetch(:max_memory)

  module_eval do
    job_counter = 0

    define_method :around_perform do |job|
      job_counter += 1

      begin
        super(job)
      ensure
        current_mem = MemoryUsageMonitor.current_usage_in_kb
        if current_mem > max_memory
          log(:info, "Exiting after job #{job_counter} since current memory " \
                     "(#{current_mem} KB) has exceeded max allowed memory " \
                     "(#{max_memory} KB).")
          shutdown
        end
      end
    end
  end
end