Jon*_*han 6 ruby exception-handling ruby-on-rails heroku
我正在使用delayed_job来处理heroku的后台作业.偶尔我会超越我的内存分配,我会得到这样的东西:
2011-11-16T02:41:25 + 00:00的Heroku [worker.1]:错误R14(存储器配额超标)2011-11-16T02:41:45 + 00:00的Heroku [worker.1]:流程运行MEM = 542M(106.0%)
我想优雅地处理这个问题.有没有办法找出我何时要超越我的记忆限制?
像机架超时这样的东西会很棒
谢谢!
我认为通过从Oink gem中窃取一些代码,我找到了一个很好的解决方案.特别是这个文件:memory_snapshot.rb,你应该阅读.它概述了四种不同的方法来消除内存使用
因此无法在Rack级别上进行此操作,您需要在导致内存问题的进程中添加内存检查(在我的情况下,它正在构建一个csv文件).
所以在那个循环中它看起来像:
def build_string_io(collection)
csv_io = StringIO.new
csv_io << collection.first.to_comma_headers.join(',') + "\n"
collection.each do |imp|
csv_io << imp.to_comma.join(',') + "\n"
check_memory!
end
csv_io.rewind
csv_io
end
def check_memory!
raise 'AboutToRunOutOfMemory' if memory > 400.megabytes #Or whatever size your worried about
end
# Taken from Oink
def memory
pages = File.read("/proc/self/statm")
pages.to_i * self.class.statm_page_size
end
def self.statm_page_size
@statm_page_size ||= begin
sys_call = SystemCall.execute("getconf PAGESIZE")
if sys_call.success?
sys_call.stdout.strip.to_i / 1024
else
4
end
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2520 次 |
| 最近记录: |