phantom-manager 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MTMwOTMzODZlNjhlMzhiYTZiODM1ZTZlZGNkMTk4Y2JmODY4MjlmMg==
4
+ MTE0YmJjYjE5Yzc0ZjAyYWI4ZjI1M2E4OTA0MzY3YTAyYjQ4MjU1ZQ==
5
5
  data.tar.gz: !binary |-
6
- MmI1Nzk1ZWQwNDcwMzA5OWJmZDE1YTk1MDg4Y2MzYjIxNGNlYzBjMA==
6
+ MjM5ZWFhMTA3MWE5NDY3ZjY1MWNlMTY2NmY4NDM2MjllZWQyNjk2ZQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MTU4MDg4MGUyOTFhZDE2YjFmYjA1OTQ2NjQyMTQzNjQxMWI2ZWI4ZTMxNDAz
10
- YWJlYjRhNjM5OTg5NTJhYzQ1MWZhYzMyNTE3ZWU5Zjc2NzBmMTAyYzQyYzE1
11
- Njg3YzA4ZGI5MGQyZmM1ZmE0ZjEwZGEwNjM3ZGZjY2U4YjVlMGQ=
9
+ ZmFhNzlmZjAzNTk5MzU0YTYxZWIxZDc2MTUzZGE2ZTMzMzdjN2JhNGQxMDQ1
10
+ MDZjN2VhZTRkYzM0MjE1N2I3MDQzYzE3NTBmMjk2ODE1OTgzMmZlNDQ1NjBj
11
+ M2VmY2UwNmYzYzNjNGZhMjBlMzcxM2VkMWYwNzgwZGZkMTA0OWY=
12
12
  data.tar.gz: !binary |-
13
- ZDRjYmYzNjdkMWUyODkxZGVhNzRkMjkwMjIxMmUxNjNkMDE3YjM1YWRiZGUw
14
- ZDY1ODZiY2RhOTE3ODE1YTA1ODVmY2ZkMTMwMGMyOGE4ODVmYWViMmFkMjVj
15
- ODI5ZjViMTBmOTUyYWExOGUzYzNmOTBmMDU3NTIzOTViMGJhYmE=
13
+ ZjM2MmMyZGFjYjRlNDVmZWM1ZTllYWI3M2Y0OTI4OWNmZWQ4ZGYzZDE1Mjg2
14
+ YTgxOTFjY2RiMmRkMWRkZWNhMGM1YWU1ZWFmZDc2MjVhZTdhN2ZkOTVmODRk
15
+ NDQzY2UyMjVlZjZiOTBhNWRiMTYzMDI0MGY1OGRlOTE2OTJiYzk=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phantom-manager (0.0.2)
4
+ phantom-manager (0.0.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/nginx/manager.rb CHANGED
@@ -1,26 +1,29 @@
1
1
  require 'utils/logger'
2
2
  require 'utils/cfg'
3
+ require 'utils/shell'
3
4
 
4
5
  module Nginx
5
6
  class Manager
6
7
  class << self
7
8
 
8
- def remove(port)
9
- $logger.info "removing #{port} from nginx"
9
+ def remove(ports)
10
+ ports = [*ports]
11
+ $logger.info "removing #{ports} from nginx"
10
12
  modify_nginx do |ofile, iline|
11
- ofile.puts(iline) unless iline =~ /#{port}/
13
+ ofile.puts(iline) if !line_matches_ports(iline, ports)
12
14
  end
13
15
  end
14
16
 
15
- def add(port)
16
- $logger.info "adding #{port} to nginx"
17
- if !port_defined?(port)
18
- modify_nginx do |ofile, iline|
19
- ofile.puts(iline)
20
- ofile.puts(phantom_upstream(port)) if iline =~ /upstream phantomjs/
17
+ def add(ports)
18
+ ports = [*ports]
19
+ $logger.info "adding #{ports} to nginx"
20
+ modify_nginx do |ofile, iline|
21
+ ofile.puts(iline)
22
+ if iline =~ /upstream phantomjs/
23
+ ports.each do |port|
24
+ ofile.puts(phantom_upstream(port)) unless port_defined?(port)
25
+ end
21
26
  end
22
- else
23
- $logger.info "port #{port} already defined"
24
27
  end
25
28
  end
26
29
 
@@ -41,7 +44,7 @@ module Nginx
41
44
 
42
45
  def reload_nginx
43
46
  $logger.info "reloading nginx"
44
- `nginx -s reload`
47
+ Utils::Shell.execute "nginx -s reload"
45
48
  end
46
49
 
47
50
  def modify_nginx
@@ -54,6 +57,14 @@ module Nginx
54
57
  reload_nginx
55
58
  end
56
59
 
60
+ def line_matches_ports(line, ports)
61
+ line =~ ports_regexp(ports)
62
+ end
63
+
64
+ def ports_regexp(ports)
65
+ /#{ports.join("|")}/
66
+ end
67
+
57
68
  end
58
69
  end
59
70
  end
@@ -10,7 +10,7 @@ module Phantom
10
10
 
11
11
  def get_running_instances
12
12
  lines = running_phantoms_shell_output.split("\n")
13
- lines.map {|l| Phantom::Process.from_string(l) }
13
+ parse_processes lines
14
14
  end
15
15
 
16
16
  def missing_ports
@@ -23,12 +23,25 @@ module Phantom
23
23
 
24
24
  private
25
25
 
26
+ def parse_processes(lines)
27
+ processes = lines.map {|l| Phantom::Process.from_string(l) }
28
+ bad_processes = processes.select {|p| !required_ports.include?(p.port)}
29
+ log_error(bad_processes, lines) if bad_processes.any?
30
+ processes.select {|p| required_ports.include?(p.port)}
31
+ end
32
+
26
33
  def running_phantoms_shell_output
27
34
  `#{running_phantoms_shell_command}`
28
35
  end
29
36
 
30
37
  def running_phantoms_shell_command
31
- "ps -e -www -o pid,rss,command | grep 'phantomjs' | grep -v sh | grep -v grep"
38
+ "ps -e -www -o pid,rss,command | grep 'phantomjs rndrme.js' | grep -v sh | grep -v grep"
39
+ end
40
+
41
+ def log_error(processes, lines)
42
+ $logger.error "Collector got bad process!"
43
+ $logger.error "Processes were #{processes}"
44
+ $logger.error "lines were: #{lines}"
32
45
  end
33
46
 
34
47
  end
@@ -7,25 +7,28 @@ module Phantom
7
7
  module Manager
8
8
  class << self
9
9
 
10
- def restart(process)
11
- $logger.info "restarting process #{process}"
12
- stop process
13
- start process
10
+ def restart(processes)
11
+ processes = [*processes]
12
+ $logger.info "restarting process #{processes}"
13
+ stop processes
14
+ start processes
14
15
  end
15
16
 
16
- def start(process)
17
- $logger.info "starting process #{process.port}"
18
- process.start
19
- Nginx::Manager.add(process.port)
17
+ def start(processes)
18
+ processes = [*processes]
19
+ $logger.info "starting process #{processes}"
20
+ processes.each(&:start)
21
+ Nginx::Manager.add(processes.map(&:port))
20
22
  end
21
23
 
22
24
  private
23
25
 
24
- def stop(process)
25
- $logger.info "stopping process #{process}"
26
- Nginx::Manager.remove(process.port)
26
+ def stop(processes)
27
+ processes = [*processes]
28
+ $logger.info "stopping process #{processes}"
29
+ Nginx::Manager.remove(processes.map(&:port))
27
30
  sleep Cfg.phantom_termination_grace
28
- process.kill
31
+ processes.each(&:kill)
29
32
  end
30
33
 
31
34
  end
@@ -1,5 +1,5 @@
1
1
  module Phantom
2
2
  module Manager
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -44,6 +44,13 @@ module Phantom
44
44
  Utils::Shell.execute start_command
45
45
  end
46
46
 
47
+ def to_s
48
+ inspect
49
+ end
50
+
51
+ def inspect
52
+ "pid: #{pid}, port: #{port}, memory_usage: #{memory_usage}, command: #{command}"
53
+ end
47
54
  private
48
55
 
49
56
  def start_command
@@ -33,6 +33,10 @@ CONF
33
33
 
34
34
  module Nginx
35
35
  describe Manager do
36
+ let :ports do
37
+ (8002..8011).to_a
38
+ end
39
+
36
40
  subject {Manager}
37
41
 
38
42
  def port_defined?(port)
@@ -57,6 +61,16 @@ module Nginx
57
61
  }.to change{port_defined?(8003)}.from(true).to(false)
58
62
  end
59
63
  end
64
+
65
+ context "multiple ports" do
66
+ it "should be removed" do
67
+ ports_to_remove = [8002, 8003, 8006]
68
+ ports.each {|p| port_defined?(p).should be_true}
69
+ subject.remove(ports_to_remove)
70
+ ports_to_remove.each {|p| port_defined?(p).should be_false}
71
+ (ports-ports_to_remove).each {|p| port_defined?(p).should be_true}
72
+ end
73
+ end
60
74
  end
61
75
 
62
76
  describe :add do
@@ -75,6 +89,15 @@ module Nginx
75
89
  }.not_to change{port_defined?(8003)}
76
90
  end
77
91
  end
92
+
93
+ context "multiple ports" do
94
+ it "should add them all" do
95
+ ports_to_add = [8012, 8013]
96
+ ports.each {|p| port_defined?(p).should be_true}
97
+ subject.add(ports_to_add)
98
+ ports_to_add.each {|p| port_defined?(p).should be_true}
99
+ end
100
+ end
78
101
  end
79
102
  end
80
103
  end
@@ -54,6 +54,29 @@ module Phantom
54
54
  end
55
55
  end
56
56
 
57
+ describe "bad processes" do
58
+ before do
59
+ phantoms = [
60
+ {pid: 5555, memory_usage: 1000, command: "phantomjs rndrme.js 8020"},
61
+ {pid: 6666, memory_usage: 1000, command: "phantomjs rndrme.js 8003"}
62
+ ]
63
+ ps = phantoms_ps_shell_output(phantoms)
64
+ subject.stub(:running_phantoms_shell_output).and_return(ps)
65
+ end
66
+
67
+ it "should not return bad phantom processes" do
68
+ generated_phantoms = subject.get_running_instances
69
+ generated_phantoms.size.should eq 1
70
+ generated_phantoms.first.port.should eq 8003
71
+ end
72
+
73
+ it "should log error with bad phantoms" do
74
+ subject.should_receive(:log_error).once
75
+ subject.get_running_instances
76
+ end
77
+
78
+ end
79
+
57
80
 
58
81
  end
59
82
  end
@@ -8,18 +8,18 @@ module Phantom
8
8
 
9
9
  describe :start do
10
10
 
11
- let(:p) {generate_process}
11
+ let(:p) { generate_process }
12
+ let(:processes) {[generate_process, generate_process, generate_process ]}
12
13
 
13
14
  it "should start new phantom process" do
14
- p.should_receive :start
15
- subject.start(p)
15
+ processes.each {|p| p.should_receive :start}
16
+ subject.start(processes)
16
17
  end
17
18
 
18
19
  it "should add port to nginx conf" do
19
- Nginx::Manager.should_receive(:add).with(p.port).once
20
- subject.start(p)
20
+ Nginx::Manager.should_receive(:add).with(processes.map(&:port)).once
21
+ subject.start(processes)
21
22
  end
22
23
  end
23
-
24
24
  end
25
25
  end
@@ -43,5 +43,12 @@ module Phantom
43
43
  p.start
44
44
  end
45
45
  end
46
+
47
+ describe :inspect do
48
+ it "should present all data in string" do
49
+ p = generate_process
50
+ "#{p}".should eq "pid: #{p.pid}, port: #{p.port}, memory_usage: #{p.memory_usage}, command: #{p.command}"
51
+ end
52
+ end
46
53
  end
47
54
  end
data/spec/shared_spec.rb CHANGED
@@ -40,6 +40,6 @@ def data_to_ps(p)
40
40
  "#{p[:pid]} #{p[:memory_usage]} #{p[:command]}"
41
41
  end
42
42
 
43
- def phantoms_ps_shell_output
44
- phantoms_data.map {|p| data_to_ps(p) }.join("\n")
43
+ def phantoms_ps_shell_output(data = phantoms_data)
44
+ data.map {|p| data_to_ps(p) }.join("\n")
45
45
  end
data/spec/spec_helper.rb CHANGED
@@ -25,5 +25,6 @@ RSpec.configure do |config|
25
25
  # --seed 1234
26
26
  config.order = 'random'
27
27
 
28
+ require 'utils/shell'
28
29
  config.before(:each) { Utils::Shell.stub(:system) }
29
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phantom-manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erez Rabih
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-25 00:00:00.000000000 Z
11
+ date: 2013-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler