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 +8 -8
- data/Gemfile.lock +1 -1
- data/lib/nginx/manager.rb +23 -12
- data/lib/phantom/collector.rb +15 -2
- data/lib/phantom/manager.rb +15 -12
- data/lib/phantom/manager/version.rb +1 -1
- data/lib/phantom/process.rb +7 -0
- data/spec/lib/nginx/manager_spec.rb +23 -0
- data/spec/lib/phantom/collector_spec.rb +23 -0
- data/spec/lib/phantom/manager_spec.rb +6 -6
- data/spec/lib/phantom/process_spec.rb +7 -0
- data/spec/shared_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MTE0YmJjYjE5Yzc0ZjAyYWI4ZjI1M2E4OTA0MzY3YTAyYjQ4MjU1ZQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MjM5ZWFhMTA3MWE5NDY3ZjY1MWNlMTY2NmY4NDM2MjllZWQyNjk2ZQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZmFhNzlmZjAzNTk5MzU0YTYxZWIxZDc2MTUzZGE2ZTMzMzdjN2JhNGQxMDQ1
|
10
|
+
MDZjN2VhZTRkYzM0MjE1N2I3MDQzYzE3NTBmMjk2ODE1OTgzMmZlNDQ1NjBj
|
11
|
+
M2VmY2UwNmYzYzNjNGZhMjBlMzcxM2VkMWYwNzgwZGZkMTA0OWY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZjM2MmMyZGFjYjRlNDVmZWM1ZTllYWI3M2Y0OTI4OWNmZWQ4ZGYzZDE1Mjg2
|
14
|
+
YTgxOTFjY2RiMmRkMWRkZWNhMGM1YWU1ZWFmZDc2MjVhZTdhN2ZkOTVmODRk
|
15
|
+
NDQzY2UyMjVlZjZiOTBhNWRiMTYzMDI0MGY1OGRlOTE2OTJiYzk=
|
data/Gemfile.lock
CHANGED
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(
|
9
|
-
|
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)
|
13
|
+
ofile.puts(iline) if !line_matches_ports(iline, ports)
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
15
|
-
def add(
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
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
|
data/lib/phantom/collector.rb
CHANGED
@@ -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
|
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
|
data/lib/phantom/manager.rb
CHANGED
@@ -7,25 +7,28 @@ module Phantom
|
|
7
7
|
module Manager
|
8
8
|
class << self
|
9
9
|
|
10
|
-
def restart(
|
11
|
-
|
12
|
-
|
13
|
-
|
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(
|
17
|
-
|
18
|
-
process
|
19
|
-
|
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(
|
25
|
-
|
26
|
-
|
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
|
-
|
31
|
+
processes.each(&:kill)
|
29
32
|
end
|
30
33
|
|
31
34
|
end
|
data/lib/phantom/process.rb
CHANGED
@@ -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(
|
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(
|
20
|
-
subject.start(
|
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
|
-
|
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
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.
|
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-
|
11
|
+
date: 2013-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|