phantom-manager 0.0.2

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.
Files changed (49) hide show
  1. checksums.yaml +15 -0
  2. data/.DS_Store +0 -0
  3. data/.gitignore +3 -0
  4. data/Gemfile +7 -0
  5. data/Gemfile.lock +36 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +32 -0
  8. data/Rakefile +1 -0
  9. data/bin/phantom_monitor +55 -0
  10. data/config/config.yml +27 -0
  11. data/lib/.DS_Store +0 -0
  12. data/lib/monitors/base.rb +39 -0
  13. data/lib/monitors/memory.rb +27 -0
  14. data/lib/monitors/processes.rb +33 -0
  15. data/lib/monitors/restart_listener.rb +39 -0
  16. data/lib/monitors/violations_recorders/base.rb +56 -0
  17. data/lib/monitors/violations_recorders/memory.rb +23 -0
  18. data/lib/monitors/violations_recorders/processes.rb +21 -0
  19. data/lib/nginx/manager.rb +59 -0
  20. data/lib/phantom/.DS_Store +0 -0
  21. data/lib/phantom/collector.rb +36 -0
  22. data/lib/phantom/manager/version.rb +5 -0
  23. data/lib/phantom/manager.rb +34 -0
  24. data/lib/phantom/process.rb +54 -0
  25. data/lib/utils/cfg.rb +18 -0
  26. data/lib/utils/limited_array.rb +31 -0
  27. data/lib/utils/lock.rb +29 -0
  28. data/lib/utils/logger.rb +3 -0
  29. data/lib/utils/shell.rb +12 -0
  30. data/phantom-manager.gemspec +24 -0
  31. data/spec/files/config.yml +12 -0
  32. data/spec/files/nginx.conf +26 -0
  33. data/spec/lib/monitors/base_spec.rb +14 -0
  34. data/spec/lib/monitors/memory_spec.rb +33 -0
  35. data/spec/lib/monitors/processes_spec.rb +45 -0
  36. data/spec/lib/monitors/restart_listener_spec.rb +27 -0
  37. data/spec/lib/monitors/violations_recorders/base_spec.rb +126 -0
  38. data/spec/lib/monitors/violations_recorders/memory_spec.rb +73 -0
  39. data/spec/lib/monitors/violations_recorders/processes_spec.rb +51 -0
  40. data/spec/lib/nginx/manager_spec.rb +80 -0
  41. data/spec/lib/phantom/collector_spec.rb +59 -0
  42. data/spec/lib/phantom/manager_spec.rb +25 -0
  43. data/spec/lib/phantom/process_spec.rb +47 -0
  44. data/spec/lib/utils/limited_array_spec.rb +73 -0
  45. data/spec/lib/utils/lock_spec.rb +69 -0
  46. data/spec/lib/utils/shell_spec.rb +18 -0
  47. data/spec/shared_spec.rb +45 -0
  48. data/spec/spec_helper.rb +29 -0
  49. metadata +152 -0
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'phantom/process'
3
+
4
+ module Phantom
5
+ describe Process do
6
+ describe :initialization do
7
+
8
+ it "should initialize with new" do
9
+ p = Phantom::Process.new(1, 2, "cmd", 4)
10
+ p.pid.should eq 1
11
+ p.memory_usage.should eq 2
12
+ p.command.should eq "cmd"
13
+ p.port.should eq 4
14
+ end
15
+
16
+ it "should initialize with string" do
17
+ p = Phantom::Process.from_string("1 2 phantomjs rndrme.js 4")
18
+ p.pid.should eq 1
19
+ p.memory_usage.should eq 2
20
+ p.command.should eq "phantomjs rndrme.js 4"
21
+ p.port.should eq 4
22
+ end
23
+
24
+ it "should initialize empty" do
25
+ p = Phantom::Process.new
26
+ p.should_not be_nil
27
+ end
28
+
29
+ end
30
+
31
+ describe :kill do
32
+ it "should send system kill call" do
33
+ p = generate_process
34
+ Utils::Shell.should_receive(:execute).with("kill #{p.pid}")
35
+ p.kill
36
+ end
37
+ end
38
+
39
+ describe :start do
40
+ it "should send system kill call" do
41
+ p = generate_process
42
+ Utils::Shell.should_receive(:execute).with(p.send(:start_command))
43
+ p.start
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+ require 'utils/limited_array'
3
+
4
+ describe LimitedArray do
5
+
6
+ describe :initialize do
7
+ it "should have stated limit" do
8
+ arr = LimitedArray.new(5)
9
+ arr.limit.should eq 5
10
+ end
11
+ end
12
+
13
+ describe :full? do
14
+ context "zero length array" do
15
+ it "should return true" do
16
+ LimitedArray.new(0).should be_full
17
+ end
18
+ end
19
+
20
+ context "non-zero length array" do
21
+ subject { LimitedArray.new(1) }
22
+
23
+ it "should return false" do
24
+ subject.should_not be_full
25
+ end
26
+
27
+ context "filled array" do
28
+ it "should return true" do
29
+ subject << :item
30
+ subject.should be_full
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+
37
+ describe :<< do
38
+ subject { LimitedArray.new(3) }
39
+
40
+ it "should allow 3 items" do
41
+ 3.times {subject << :item}
42
+ subject.should =~ [:item, :item, :item ]
43
+ end
44
+
45
+ it "should keep only 3 items" do
46
+ 5.times { |i| subject << "item#{i}"}
47
+ subject.should =~ ["item2", "item3", "item4"]
48
+ end
49
+ end
50
+
51
+ describe :sum do
52
+ subject {LimitedArray.new(3)}
53
+
54
+ it "sum correctly" do
55
+ subject << 5
56
+ subject << 4
57
+ subject << 3
58
+ subject.sum.should eq 12
59
+ end
60
+ end
61
+
62
+ describe :average do
63
+
64
+ subject {LimitedArray.new(3)}
65
+
66
+ it "average correctly" do
67
+ subject << 5
68
+ subject << 4
69
+ subject << 3
70
+ subject.average.should eq 4
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+ require 'utils/lock'
3
+
4
+ module Utils
5
+
6
+ describe Lock do
7
+ describe :initialize do
8
+ it "should be unlocked" do
9
+ subject.should_not be_locked
10
+ end
11
+ end
12
+
13
+ describe :lock do
14
+ it "should be locked" do
15
+ subject.lock.should be_locked
16
+ end
17
+ end
18
+
19
+ describe :unlock do
20
+ it "should be unlocked" do
21
+ subject.lock.unlock.should_not be_locked
22
+ end
23
+ end
24
+
25
+ describe :acquire do
26
+ before do
27
+ @lock = Lock.new
28
+ end
29
+ context "new lock" do
30
+ it "should run method" do
31
+ passed = false
32
+ @lock.acquire do
33
+ passed = true
34
+ end
35
+
36
+ passed.should be_true
37
+ end
38
+ end
39
+
40
+ context "acquired lock" do
41
+ it "should not run method" do
42
+ passed = false
43
+ @lock.acquire do
44
+ @lock.acquire do
45
+ passed = true
46
+ end
47
+ end
48
+
49
+ passed.should be_false
50
+ end
51
+ end
52
+
53
+ context "unlocking after done" do
54
+ it "should be available" do
55
+ passed = false
56
+ @lock.acquire do
57
+ passed = false
58
+ end
59
+
60
+ @lock.acquire do
61
+ passed = true
62
+ end
63
+
64
+ passed.should be_true
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ require 'utils/shell'
3
+
4
+ module Utils
5
+ describe Shell do
6
+ subject {Shell}
7
+
8
+ describe :execute do
9
+ it "should run command" do
10
+ cmd = "echo blah"
11
+ Shell.should_receive(:system).with(cmd).once
12
+
13
+ Shell.execute cmd
14
+ end
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ require 'utils/logger'
2
+ require 'utils/cfg'
3
+ $cfg.nginx_conf = File.expand_path('../files/nginx.conf', __FILE__)
4
+ $cfg.new_nginx_conf = File.expand_path('../files/nginx.conf.new', __FILE__)
5
+
6
+ $logger = Logger.new(nil)
7
+
8
+ def generate_memory
9
+ 50000 + rand(50000)
10
+ end
11
+
12
+ def generate_process
13
+ Phantom::Process.new(rand(1000), generate_memory, "phantomjs rndrme.js #{rand(2000)}", 8000 + rand(10))
14
+ end
15
+
16
+ def phantoms_data
17
+ [
18
+ {
19
+ pid: 1000,
20
+ memory_usage: 100000,
21
+ command: "phantomjs rndrme.js 8002",
22
+ port: 8002
23
+ },
24
+ {
25
+ pid: 2000,
26
+ memory_usage: 130000,
27
+ command: "phantomjs rndrme.js 8003",
28
+ port: 8003
29
+ },
30
+ {
31
+ pid: 3000,
32
+ memory_usage: 80000,
33
+ command: "phantomjs rndrme.js 8006",
34
+ port: 8006
35
+ }
36
+ ]
37
+ end
38
+
39
+ def data_to_ps(p)
40
+ "#{p[:pid]} #{p[:memory_usage]} #{p[:command]}"
41
+ end
42
+
43
+ def phantoms_ps_shell_output
44
+ phantoms_data.map {|p| data_to_ps(p) }.join("\n")
45
+ end
@@ -0,0 +1,29 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ lib = File.expand_path('../../lib', __FILE__)
9
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
10
+
11
+ $options = {}
12
+ $options[:env] = "test"
13
+ $options[:config] = File.expand_path("../files/config.yml", __FILE__)
14
+
15
+ require 'shared_spec'
16
+
17
+ RSpec.configure do |config|
18
+ config.treat_symbols_as_metadata_keys_with_true_values = true
19
+ config.run_all_when_everything_filtered = true
20
+ #config.filter_run :focus
21
+
22
+ # Run specs in random order to surface order dependencies. If you find an
23
+ # order dependency and want to debug it, you can fix the order by providing
24
+ # the seed, which is printed after each run.
25
+ # --seed 1234
26
+ config.order = 'random'
27
+
28
+ config.before(:each) { Utils::Shell.stub(:system) }
29
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phantom-manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Erez Rabih
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Uses Nginx as multiple phantomjs workers load balancer
56
+ email:
57
+ - erez.rabih@gmail.com
58
+ executables:
59
+ - phantom_monitor
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .DS_Store
64
+ - .gitignore
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/phantom_monitor
71
+ - config/config.yml
72
+ - lib/.DS_Store
73
+ - lib/monitors/base.rb
74
+ - lib/monitors/memory.rb
75
+ - lib/monitors/processes.rb
76
+ - lib/monitors/restart_listener.rb
77
+ - lib/monitors/violations_recorders/base.rb
78
+ - lib/monitors/violations_recorders/memory.rb
79
+ - lib/monitors/violations_recorders/processes.rb
80
+ - lib/nginx/manager.rb
81
+ - lib/phantom/.DS_Store
82
+ - lib/phantom/collector.rb
83
+ - lib/phantom/manager.rb
84
+ - lib/phantom/manager/version.rb
85
+ - lib/phantom/process.rb
86
+ - lib/utils/cfg.rb
87
+ - lib/utils/limited_array.rb
88
+ - lib/utils/lock.rb
89
+ - lib/utils/logger.rb
90
+ - lib/utils/shell.rb
91
+ - phantom-manager.gemspec
92
+ - spec/files/config.yml
93
+ - spec/files/nginx.conf
94
+ - spec/lib/monitors/base_spec.rb
95
+ - spec/lib/monitors/memory_spec.rb
96
+ - spec/lib/monitors/processes_spec.rb
97
+ - spec/lib/monitors/restart_listener_spec.rb
98
+ - spec/lib/monitors/violations_recorders/base_spec.rb
99
+ - spec/lib/monitors/violations_recorders/memory_spec.rb
100
+ - spec/lib/monitors/violations_recorders/processes_spec.rb
101
+ - spec/lib/nginx/manager_spec.rb
102
+ - spec/lib/phantom/collector_spec.rb
103
+ - spec/lib/phantom/manager_spec.rb
104
+ - spec/lib/phantom/process_spec.rb
105
+ - spec/lib/utils/limited_array_spec.rb
106
+ - spec/lib/utils/lock_spec.rb
107
+ - spec/lib/utils/shell_spec.rb
108
+ - spec/shared_spec.rb
109
+ - spec/spec_helper.rb
110
+ homepage: https://github.com/FTBpro/phantom-manager
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.0.5
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Write a gem summary
134
+ test_files:
135
+ - spec/files/config.yml
136
+ - spec/files/nginx.conf
137
+ - spec/lib/monitors/base_spec.rb
138
+ - spec/lib/monitors/memory_spec.rb
139
+ - spec/lib/monitors/processes_spec.rb
140
+ - spec/lib/monitors/restart_listener_spec.rb
141
+ - spec/lib/monitors/violations_recorders/base_spec.rb
142
+ - spec/lib/monitors/violations_recorders/memory_spec.rb
143
+ - spec/lib/monitors/violations_recorders/processes_spec.rb
144
+ - spec/lib/nginx/manager_spec.rb
145
+ - spec/lib/phantom/collector_spec.rb
146
+ - spec/lib/phantom/manager_spec.rb
147
+ - spec/lib/phantom/process_spec.rb
148
+ - spec/lib/utils/limited_array_spec.rb
149
+ - spec/lib/utils/lock_spec.rb
150
+ - spec/lib/utils/shell_spec.rb
151
+ - spec/shared_spec.rb
152
+ - spec/spec_helper.rb