phantom-manager 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MmU1OGFhZGQzODQ2NGJkNjkwN2FkNmM3ZWI0NTE3ZWI4NWY5NzEzNA==
4
+ MTMwOTMzODZlNjhlMzhiYTZiODM1ZTZlZGNkMTk4Y2JmODY4MjlmMg==
5
5
  data.tar.gz: !binary |-
6
- OGJjMTI5Yzk1NjVkOTczM2E4MWEyMWM2NGMxMmQ2NmUzMTdmYjM4MQ==
6
+ MmI1Nzk1ZWQwNDcwMzA5OWJmZDE1YTk1MDg4Y2MzYjIxNGNlYzBjMA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MjI3NTQwNDNjMWVmZGRkYzk3NTYzZmE5MDE4YmFiNjkwNGFjMmJmMTkxOWE0
10
- ODQ1MTRlYzY3NjFmMzg0MjZiZGM5ODIyOWY2NWY0NjgyNWQ0M2UyZTI1NDcx
11
- MDMxZmUzZTM0OTVlMzFmYWVkNGI5MGNiZDVjNzEyYTU2Yjk3MmE=
9
+ MTU4MDg4MGUyOTFhZDE2YjFmYjA1OTQ2NjQyMTQzNjQxMWI2ZWI4ZTMxNDAz
10
+ YWJlYjRhNjM5OTg5NTJhYzQ1MWZhYzMyNTE3ZWU5Zjc2NzBmMTAyYzQyYzE1
11
+ Njg3YzA4ZGI5MGQyZmM1ZmE0ZjEwZGEwNjM3ZGZjY2U4YjVlMGQ=
12
12
  data.tar.gz: !binary |-
13
- MzdkODY4MjYyNDc4MzFmYjlhNTc2NGE2MDg5MmMwOWI2ZTA4ZjI4ZTkwYTA2
14
- YTU2OTdlMjI4MmJlNmUyZmEyYzAxOWE5ZWQyMzQzZDdiNWRmYWFmNWM5ODRj
15
- ZDJiYjUzNTU4YzcyYmYyZTlhZTdiZmEzZDhmOGUzYTQwZjcyMGQ=
13
+ ZDRjYmYzNjdkMWUyODkxZGVhNzRkMjkwMjIxMmUxNjNkMDE3YjM1YWRiZGUw
14
+ ZDY1ODZiY2RhOTE3ODE1YTA1ODVmY2ZkMTMwMGMyOGE4ODVmYWViMmFkMjVj
15
+ ODI5ZjViMTBmOTUyYWExOGUzYzNmOTBmMDU3NTIzOTViMGJhYmE=
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  .rspec
2
2
  tags
3
3
  log/*
4
+ phantom-manager*.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phantom-manager (0.0.1)
4
+ phantom-manager (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -19,7 +19,7 @@ module Monitors
19
19
  end
20
20
 
21
21
  def check_interval
22
- $cfg.memory_check_interval
22
+ Cfg.memory_check_interval
23
23
  end
24
24
  end
25
25
 
@@ -25,7 +25,7 @@ module Monitors
25
25
  end
26
26
 
27
27
  def check_interval
28
- $cfg.processes_check_interval
28
+ Cfg.processes_check_interval
29
29
  end
30
30
  end
31
31
 
@@ -22,7 +22,7 @@ module Monitors
22
22
  $logger.info "Restarting process on port #{p.port}"
23
23
  Phantom::Manager.restart(p)
24
24
 
25
- sleep $cfg.phantom_termination_grace
25
+ sleep Cfg.phantom_termination_grace
26
26
  end
27
27
 
28
28
  end
@@ -6,7 +6,7 @@ module Monitors
6
6
  class << self
7
7
 
8
8
  def retries_limit
9
- $cfg.memory_retries
9
+ Cfg.memory_retries
10
10
  end
11
11
 
12
12
  def process_attr
@@ -14,7 +14,7 @@ module Monitors
14
14
  end
15
15
 
16
16
  def process_is_violating?(process)
17
- process.memory_usage > $cfg.memory_limit
17
+ process.memory_usage > Cfg.memory_limit
18
18
  end
19
19
 
20
20
  end
@@ -5,7 +5,7 @@ module Monitors
5
5
  class Processes < Base
6
6
  class << self
7
7
  def retries_limit
8
- $cfg.processes_check_retries
8
+ Cfg.processes_check_retries
9
9
  end
10
10
 
11
11
  def process_attr
data/lib/nginx/manager.rb CHANGED
@@ -31,12 +31,12 @@ module Nginx
31
31
  end
32
32
 
33
33
  def port_defined?(port)
34
- File.readlines($cfg.nginx_conf).grep(/#{port}/).size > 0
34
+ File.readlines(Cfg.nginx_conf).grep(/#{port}/).size > 0
35
35
  end
36
36
 
37
37
  def switch_nginx_configs
38
38
  $logger.info "switching nginx configurations"
39
- `mv #{$cfg.new_nginx_conf} #{$cfg.nginx_conf}`
39
+ `mv #{Cfg.new_nginx_conf} #{Cfg.nginx_conf}`
40
40
  end
41
41
 
42
42
  def reload_nginx
@@ -45,8 +45,8 @@ module Nginx
45
45
  end
46
46
 
47
47
  def modify_nginx
48
- File.open($cfg.new_nginx_conf, "w") do |ofile|
49
- File.foreach($cfg.nginx_conf) do |iline|
48
+ File.open(Cfg.new_nginx_conf, "w") do |ofile|
49
+ File.foreach(Cfg.nginx_conf) do |iline|
50
50
  yield ofile, iline
51
51
  end
52
52
  end
@@ -18,7 +18,7 @@ module Phantom
18
18
  end
19
19
 
20
20
  def required_ports
21
- ($cfg.phantom_base_port..($cfg.phantom_base_port+$cfg.phantom_processes_number-1)).to_a
21
+ (Cfg.phantom_base_port..(Cfg.phantom_base_port+Cfg.phantom_processes_number-1)).to_a
22
22
  end
23
23
 
24
24
  private
@@ -1,5 +1,5 @@
1
1
  module Phantom
2
2
  module Manager
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -24,7 +24,7 @@ module Phantom
24
24
  def stop(process)
25
25
  $logger.info "stopping process #{process}"
26
26
  Nginx::Manager.remove(process.port)
27
- sleep $cfg.phantom_termination_grace
27
+ sleep Cfg.phantom_termination_grace
28
28
  process.kill
29
29
  end
30
30
 
@@ -47,7 +47,7 @@ module Phantom
47
47
  private
48
48
 
49
49
  def start_command
50
- "cd #{$cfg.rails_root} && phantomjs rndrme.js #{port} >>#{$cfg.phantom_log_path} 2>&1 &"
50
+ "cd #{Cfg.rails_root} && phantomjs rndrme.js #{port} >>#{Cfg.phantom_log_path} 2>&1 &"
51
51
  end
52
52
 
53
53
  end
data/lib/utils/cfg.rb CHANGED
@@ -2,17 +2,27 @@ require 'ostruct'
2
2
  require 'yaml'
3
3
 
4
4
  class Cfg
5
- def self.load
6
- cfg = File.expand_path($options[:config])
7
- env = $options[:env]
8
- hash = YAML.load(File.open(cfg))[env]
9
- hash.each do |k,v|
10
- hash[k] = v.to_i if v =~ /^\d+$/
5
+ class << self
6
+
7
+ def method_missing(method_sym, *arguments, &block)
8
+ cfg.send(method_sym, *arguments, &block)
9
+ end
10
+
11
+ def cfg
12
+ @cfg ||= Cfg.load
11
13
  end
12
- obj = OpenStruct.new hash
13
- obj.new_nginx_conf = "#{obj.nginx_conf}.new"
14
14
 
15
- $cfg = obj
15
+ def load
16
+ cfg = File.expand_path($options[:config])
17
+ env = $options[:env]
18
+ hash = YAML.load(File.open(cfg))[env]
19
+ hash.each do |k,v|
20
+ hash[k] = v.to_i if v =~ /^\d+$/
21
+ end
22
+ obj = OpenStruct.new hash
23
+ obj.new_nginx_conf = "#{obj.nginx_conf}.new"
24
+
25
+ @cfg = obj
26
+ end
16
27
  end
17
28
  end
18
- Cfg.load
@@ -9,22 +9,22 @@ module Monitors
9
9
 
10
10
  before do
11
11
  Phantom::Collector.stub(:running_phantoms_shell_output).and_return(phantoms_ps_shell_output)
12
- $cfg.memory_limit = 110000
13
- $cfg.memory_retries = 3
12
+ Cfg.memory_limit = 110000
13
+ Cfg.memory_retries = 3
14
14
  end
15
15
 
16
16
  describe :perform_check do
17
17
  context "below memory_retries" do
18
18
  it "should not restart processes" do
19
19
  Phantom::Manager.should_not_receive(:restart)
20
- ( $cfg.memory_retries - 1 ).times { subject.perform_check }
20
+ ( Cfg.memory_retries - 1 ).times { subject.perform_check }
21
21
  end
22
22
  end
23
23
 
24
24
  context "at memory_retries" do
25
25
  it "should restart process" do
26
26
  Phantom::Manager.should_receive(:restart).once
27
- $cfg.memory_retries.times { subject.perform_check }
27
+ Cfg.memory_retries.times { subject.perform_check }
28
28
  end
29
29
  end
30
30
  end
@@ -35,7 +35,7 @@ module Monitors
35
35
 
36
36
  it "should create two new instances" do
37
37
  Phantom::Manager.should_receive(:start).twice
38
- $cfg.processes_check_retries.times {subject.perform_check}
38
+ Cfg.processes_check_retries.times {subject.perform_check}
39
39
  end
40
40
  end
41
41
 
@@ -13,8 +13,8 @@ module Monitors
13
13
 
14
14
  describe :is_violating? do
15
15
 
16
- let(:violating_memory) { $cfg.memory_limit + 10}
17
- let(:valid_memory) { $cfg.memory_limit - 10}
16
+ let(:violating_memory) { Cfg.memory_limit + 10}
17
+ let(:valid_memory) { Cfg.memory_limit - 10}
18
18
 
19
19
  context "violating process" do
20
20
 
@@ -24,7 +24,7 @@ module Monitors
24
24
  end
25
25
 
26
26
  it "should return true" do
27
- ($cfg.memory_retries-1).times do
27
+ (Cfg.memory_retries-1).times do
28
28
  subject.is_violating?(@p).should be_false
29
29
  end
30
30
 
@@ -40,7 +40,7 @@ module Monitors
40
40
  end
41
41
 
42
42
  it "should return false" do
43
- $cfg.memory_retries.times do
43
+ Cfg.memory_retries.times do
44
44
  subject.is_violating?(@p).should be_false
45
45
  end
46
46
  end
@@ -49,7 +49,7 @@ module Monitors
49
49
 
50
50
  it "reset violations if valid memory detected one" do
51
51
  @p.memory_usage = violating_memory
52
- ($cfg.memory_retries-1).times do
52
+ (Cfg.memory_retries-1).times do
53
53
  subject.is_violating?(@p).should be_false
54
54
  end
55
55
 
@@ -58,7 +58,7 @@ module Monitors
58
58
  subject.is_violating?(@p).should be_false
59
59
 
60
60
  @p.memory_usage = violating_memory
61
- ($cfg.memory_retries-1).times do
61
+ (Cfg.memory_retries-1).times do
62
62
  subject.is_violating?(@p).should be_false
63
63
  end
64
64
  subject.is_violating?(@p).should be_true
@@ -18,11 +18,11 @@ module Monitors
18
18
  Phantom::Collector.stub(:missing_ports).and_return([8004])
19
19
  @p = Phantom::Process.new
20
20
  @p.port = 8004
21
- $cfg.processes_check_retries = 3
21
+ Cfg.processes_check_retries = 3
22
22
  end
23
23
 
24
24
  context "below retries limit" do
25
- let(:retries) {$cfg.processes_check_retries - 1}
25
+ let(:retries) {Cfg.processes_check_retries - 1}
26
26
 
27
27
  it "should return false" do
28
28
  retries.times do
@@ -34,7 +34,7 @@ module Monitors
34
34
 
35
35
  context "equals retries limit" do
36
36
 
37
- let(:retries) {$cfg.processes_check_retries}
37
+ let(:retries) {Cfg.processes_check_retries}
38
38
 
39
39
  it "should return false" do
40
40
  (retries-1).times do
@@ -36,11 +36,11 @@ module Nginx
36
36
  subject {Manager}
37
37
 
38
38
  def port_defined?(port)
39
- File.readlines($cfg.nginx_conf).grep(/#{port}/).size > 0
39
+ File.readlines(Cfg.nginx_conf).grep(/#{port}/).size > 0
40
40
  end
41
41
 
42
42
  before do
43
- File.open($cfg.nginx_conf, "w") do |f|
43
+ File.open(Cfg.nginx_conf, "w") do |f|
44
44
  f.puts(INITIAL_CONF)
45
45
  end
46
46
 
@@ -6,8 +6,8 @@ module Phantom
6
6
  subject {Collector}
7
7
 
8
8
  before do
9
- $cfg.phantom_processes_number = 5
10
- $cfg.phantom_base_port = 8002
9
+ Cfg.phantom_processes_number = 5
10
+ Cfg.phantom_base_port = 8002
11
11
  subject.stub(:running_phantoms_shell_output).and_return(phantoms_ps_shell_output)
12
12
  end
13
13
 
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+ require 'utils/cfg'
3
+
4
+ module Utils
5
+ describe Cfg do
6
+ subject {Cfg}
7
+
8
+ it "should be able to set" do
9
+ Cfg.rails_root= "some path"
10
+ Cfg.rails_root.should eq "some path"
11
+ Cfg.rails_root= "another path"
12
+ Cfg.rails_root.should eq "another path"
13
+ end
14
+ end
15
+ end
data/spec/shared_spec.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'utils/logger'
2
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__)
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
5
 
6
6
  $logger = Logger.new(nil)
7
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phantom-manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erez Rabih
@@ -102,6 +102,7 @@ files:
102
102
  - spec/lib/phantom/collector_spec.rb
103
103
  - spec/lib/phantom/manager_spec.rb
104
104
  - spec/lib/phantom/process_spec.rb
105
+ - spec/lib/utils/cfg_spec.rb
105
106
  - spec/lib/utils/limited_array_spec.rb
106
107
  - spec/lib/utils/lock_spec.rb
107
108
  - spec/lib/utils/shell_spec.rb
@@ -145,6 +146,7 @@ test_files:
145
146
  - spec/lib/phantom/collector_spec.rb
146
147
  - spec/lib/phantom/manager_spec.rb
147
148
  - spec/lib/phantom/process_spec.rb
149
+ - spec/lib/utils/cfg_spec.rb
148
150
  - spec/lib/utils/limited_array_spec.rb
149
151
  - spec/lib/utils/lock_spec.rb
150
152
  - spec/lib/utils/shell_spec.rb