weasel-the-memory-watcher 1.0.4 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.4
1
+ 1.1.0
data/bin/weasel CHANGED
@@ -13,6 +13,9 @@ OptionParser.new do |opts|
13
13
  eval(v)
14
14
  }
15
15
  end
16
+ opts.on("--callback_message string",String, "message to write when callback fired") do |v|
17
+ options[:callback_message] = v
18
+ end
16
19
 
17
20
  opts.on("--num_over_marks N",String, "how many times the proces memory must go over to be a candidate") do |v|
18
21
  options[:num_over_marks] = v.to_i
data/lib/memory_watch.rb CHANGED
@@ -1,14 +1,20 @@
1
1
 
2
2
  class MemoryWatch
3
- attr_accessor :watch_string,:high_water_pids,:callback,:high_water_mb,:delay,:num_cycles,:num_over_marks,:pids
3
+ attr_accessor :watch_string,:high_water_pids,:callback,:high_water_mb,
4
+ :delay,:num_cycles,:num_over_marks,:pids,:callback_message
5
+
4
6
  def initialize(options)
5
- self.callback = options[:callback] || lambda {|pid| p pid}
6
- self.watch_string = (options[:watch] || "this poem is a pomme").strip
7
- self.delay = options[:delay] || 60
8
- self.num_cycles = options[:num_cycles] || 3
9
- self.num_over_marks = options[:num_over_marks] || 2
10
- self.high_water_mb = options[:high_water_mb] || 700
11
- self.high_water_pids = {}
7
+ self.callback = options[:callback] ||
8
+ lambda {|pid| p pid}
9
+ self.callback_message = (options[:callback_message] ||
10
+ "Callback Triggered").strip
11
+ self.watch_string = (options[:watch] ||
12
+ "this poem is a pomme").strip
13
+ self.delay = options[:delay] || 60
14
+ self.num_cycles = options[:num_cycles] || 3
15
+ self.num_over_marks = options[:num_over_marks] || 2
16
+ self.high_water_mb = options[:high_water_mb] || 700
17
+ self.high_water_pids = {}
12
18
  end
13
19
  public
14
20
  def cycle
@@ -23,18 +29,19 @@ class MemoryWatch
23
29
  def _trigger_callback
24
30
  self.high_water_pids.each do |pid,over_marks|
25
31
  if over_marks.size >= self.num_over_marks
32
+ puts "[#{Time.now}] #{pid} #{self.callback_message}"
26
33
  self.callback.call(pid)
27
34
  end
28
35
  end
29
36
  end
30
37
  def _run
31
- p cmd = "ps x |grep '#{self.watch_string}' |grep -v grep | awk '{print $1}'"
38
+ cmd = "ps x |grep '#{self.watch_string}' |grep -v grep | awk '{print $1}'"
32
39
  self.pids = %x{#{cmd}}.split()
33
40
  self.pids
34
41
  end
35
42
  def _check_pids
36
43
  _run.each { |pid|
37
- p memory_usage = %x{ps -o rss= -p #{pid}}.to_i # KB
44
+ memory_usage = %x{ps -o rss= -p #{pid}}.to_i # KB
38
45
  if memory_usage > self.high_water_mb * 1024 # 750MB
39
46
  puts "WARNING - Process #{pid} hit high water mark"
40
47
  self.high_water_pids[pid] ||= []
@@ -1,7 +1,7 @@
1
1
  require 'helper'
2
2
  class TestMemoryWatch < Test::Unit::TestCase
3
3
  def test_watches_self
4
- cmd = %q{ruby ./lib/memory_watch.rb --delay 999999 --num_cycles 9900000099}
4
+ cmd = %q{ruby ./bin/weasel --delay 999999 --num_cycles 9900000099}
5
5
  pid = fork { %x{#{cmd}} }
6
6
  Process.detach(pid)
7
7
  wm = MemoryWatch.new(:watch => cmd , :high_water_mb => 0.01,:delay => 0.1, :num_cycles => 1)
@@ -22,7 +22,8 @@ class TestMemoryWatch < Test::Unit::TestCase
22
22
  :num_over_marks => 0,
23
23
  :callback => lambda {|pid|
24
24
  test_magiggy = pid
25
- })
25
+ },
26
+ :callback_message => 'Message')
26
27
  wm.cycle
27
28
  assert {
28
29
  wm.high_water_pids.include? test_magiggy
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weasel-the-memory-watcher
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 4
10
- version: 1.0.4
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Curtis Schofield
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-11-19 00:00:00 -08:00
19
+ date: 2010-11-22 00:00:00 -08:00
20
20
  default_executable: weasel
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency