Class: DeisInteractive::Rails::Logs
- Inherits:
-
Base
- Object
- Base
- DeisInteractive::Rails::Logs
show all
- Defined in:
- lib/deis-interactive/rails/logs.rb
Instance Attribute Summary collapse
Attributes inherited from Base
#app, #process
Instance Method Summary
collapse
Methods inherited from Base
#pod_ids, #processes_pattern
Constructor Details
#initialize(app, process, follow: false) ⇒ Logs
Returns a new instance of Logs.
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/deis-interactive/rails/logs.rb', line 13
def initialize(app, process, follow: false)
super(app, process)
@follow = follow
@pids = Concurrent::Array.new
@outputs = Concurrent::Array.new
at_exit do
pids.each do |pid|
Process.kill("KILL", pid) if pid_alive?(pid)
end
end
end
|
Instance Attribute Details
#follow ⇒ Object
Returns the value of attribute follow.
9
10
11
|
# File 'lib/deis-interactive/rails/logs.rb', line 9
def follow
@follow
end
|
#outputs ⇒ Object
Returns the value of attribute outputs.
11
12
13
|
# File 'lib/deis-interactive/rails/logs.rb', line 11
def outputs
@outputs
end
|
#pids ⇒ Object
Returns the value of attribute pids.
10
11
12
|
# File 'lib/deis-interactive/rails/logs.rb', line 10
def pids
@pids
end
|
Instance Method Details
#any_pid_alive? ⇒ Boolean
45
46
47
48
49
50
51
52
53
|
# File 'lib/deis-interactive/rails/logs.rb', line 45
def any_pid_alive?
20.times {
break if pids.count > 0
sleep 0.1
}
return false if pids.count == 0
pids.any? { |pid| pid_alive?(pid) }
end
|
#follow_option ⇒ Object
55
56
57
58
59
|
# File 'lib/deis-interactive/rails/logs.rb', line 55
def follow_option
if follow
"-f"
end
end
|
#log_pod(pod_id) ⇒ Object
78
79
80
81
82
83
84
85
86
|
# File 'lib/deis-interactive/rails/logs.rb', line 78
def log_pod(pod_id)
Thread.new do
cmd = "kubectl logs #{follow_option} --tail 20 #{pod_id} --namespace #{app}"
Open3.popen2e(cmd) do |_, out_err, wait_thr|
pids << wait_thr.pid
out_err.each { |line| outputs << line }
end
end
end
|
#log_pods ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/deis-interactive/rails/logs.rb', line 61
def log_pods
pod_ids.each do |pod_id|
log_pod(pod_id)
end
loop do
while (outputs.count > 0)
puts outputs.shift
end
if any_pid_alive?
sleep 0.01
else
break
end
end
end
|
26
27
28
29
30
31
32
33
34
|
# File 'lib/deis-interactive/rails/logs.rb', line 26
def perform
if process.nil?
puts "Logging on all pods"
else
puts "Logging on pod of process #{process}"
end
log_pods
end
|
#pid_alive?(pid) ⇒ Boolean
36
37
38
39
40
41
42
43
|
# File 'lib/deis-interactive/rails/logs.rb', line 36
def pid_alive?(pid)
begin
Process.getpgid( pid )
true
rescue Errno::ESRCH
false
end
end
|