eye 0.8.celluloid15 → 0.8.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -5
  3. data/CHANGES.md +3 -7
  4. data/README.md +2 -5
  5. data/Rakefile +6 -6
  6. data/bin/leye +4 -9
  7. data/bin/loader_eye +15 -14
  8. data/examples/delayed_job.eye +3 -3
  9. data/examples/dependency.eye +11 -10
  10. data/examples/notify.eye +4 -3
  11. data/examples/plugin/main.eye +5 -5
  12. data/examples/plugin/plugin.rb +2 -10
  13. data/examples/process_thin.rb +8 -8
  14. data/examples/processes/em.rb +12 -18
  15. data/examples/processes/forking.rb +5 -5
  16. data/examples/processes/sample.rb +44 -46
  17. data/examples/puma.eye +8 -9
  18. data/examples/rbenv.eye +5 -5
  19. data/examples/sidekiq.eye +3 -3
  20. data/examples/stress_test.eye +4 -4
  21. data/examples/syslog.eye +1 -1
  22. data/examples/test.eye +2 -1
  23. data/examples/thin-farm.eye +8 -7
  24. data/examples/triggers.eye +15 -13
  25. data/examples/unicorn.eye +13 -12
  26. data/eye.gemspec +14 -16
  27. data/lib/eye.rb +3 -2
  28. data/lib/eye/application.rb +6 -5
  29. data/lib/eye/checker.rb +25 -44
  30. data/lib/eye/checker/children_count.rb +1 -1
  31. data/lib/eye/checker/file_ctime.rb +1 -1
  32. data/lib/eye/checker/http.rb +15 -13
  33. data/lib/eye/checker/nop.rb +0 -1
  34. data/lib/eye/checker/socket.rb +63 -60
  35. data/lib/eye/checker/ssl_socket.rb +5 -5
  36. data/lib/eye/child_process.rb +4 -6
  37. data/lib/eye/cli.rb +46 -74
  38. data/lib/eye/cli/commands.rb +5 -4
  39. data/lib/eye/cli/render.rb +41 -61
  40. data/lib/eye/cli/server.rb +16 -19
  41. data/lib/eye/client.rb +0 -1
  42. data/lib/eye/config.rb +33 -36
  43. data/lib/eye/controller.rb +3 -2
  44. data/lib/eye/controller/commands.rb +1 -1
  45. data/lib/eye/controller/helpers.rb +2 -2
  46. data/lib/eye/controller/load.rb +17 -19
  47. data/lib/eye/controller/options.rb +5 -1
  48. data/lib/eye/controller/send_command.rb +23 -21
  49. data/lib/eye/controller/status.rb +15 -17
  50. data/lib/eye/dsl.rb +1 -6
  51. data/lib/eye/dsl/application_opts.rb +3 -4
  52. data/lib/eye/dsl/chain.rb +2 -2
  53. data/lib/eye/dsl/child_process_opts.rb +3 -3
  54. data/lib/eye/dsl/config_opts.rb +7 -7
  55. data/lib/eye/dsl/group_opts.rb +3 -3
  56. data/lib/eye/dsl/helpers.rb +1 -1
  57. data/lib/eye/dsl/main.rb +3 -4
  58. data/lib/eye/dsl/opts.rb +28 -31
  59. data/lib/eye/dsl/process_opts.rb +7 -13
  60. data/lib/eye/dsl/pure_opts.rb +9 -13
  61. data/lib/eye/dsl/validation.rb +35 -48
  62. data/lib/eye/group.rb +8 -23
  63. data/lib/eye/group/chain.rb +6 -6
  64. data/lib/eye/loader.rb +3 -3
  65. data/lib/eye/local.rb +4 -9
  66. data/lib/eye/logger.rb +4 -11
  67. data/lib/eye/notify.rb +6 -10
  68. data/lib/eye/notify/jabber.rb +1 -1
  69. data/lib/eye/notify/mail.rb +2 -2
  70. data/lib/eye/notify/slack.rb +3 -4
  71. data/lib/eye/process.rb +0 -2
  72. data/lib/eye/process/children.rb +4 -4
  73. data/lib/eye/process/commands.rb +39 -38
  74. data/lib/eye/process/config.rb +16 -22
  75. data/lib/eye/process/controller.rb +19 -5
  76. data/lib/eye/process/data.rb +9 -11
  77. data/lib/eye/process/monitor.rb +76 -86
  78. data/lib/eye/process/notify.rb +10 -10
  79. data/lib/eye/process/scheduler.rb +31 -36
  80. data/lib/eye/process/states.rb +5 -7
  81. data/lib/eye/process/states_history.rb +3 -9
  82. data/lib/eye/process/system.rb +20 -35
  83. data/lib/eye/process/trigger.rb +5 -1
  84. data/lib/eye/process/watchers.rb +9 -12
  85. data/lib/eye/reason.rb +1 -4
  86. data/lib/eye/server.rb +1 -2
  87. data/lib/eye/system.rb +15 -22
  88. data/lib/eye/system_resources.rb +9 -18
  89. data/lib/eye/trigger.rb +16 -18
  90. data/lib/eye/trigger/check_dependency.rb +4 -7
  91. data/lib/eye/trigger/flapping.rb +7 -24
  92. data/lib/eye/trigger/starting_guard.rb +6 -7
  93. data/lib/eye/trigger/stop_children.rb +2 -2
  94. data/lib/eye/trigger/transition.rb +1 -1
  95. data/lib/eye/trigger/wait_dependency.rb +2 -3
  96. data/lib/eye/utils.rb +3 -4
  97. data/lib/eye/utils/alive_array.rb +4 -9
  98. data/lib/eye/utils/celluloid_chain.rb +10 -12
  99. data/lib/eye/utils/leak_19.rb +10 -0
  100. data/lib/eye/utils/mini_active_support.rb +16 -16
  101. data/lib/eye/utils/pmap.rb +0 -2
  102. data/lib/eye/utils/tail.rb +2 -2
  103. metadata +8 -39
  104. data/.rubocop.yml +0 -141
  105. data/examples/custom_check.eye +0 -24
  106. data/examples/custom_trigger.eye +0 -30
  107. data/examples/leye_example/Eyefile +0 -10
@@ -45,7 +45,7 @@ class Eye::SystemResources
45
45
  end
46
46
 
47
47
  def deep_children(pid)
48
- Array(pid_or_children(pid)).flatten.sort_by(&:-@)
48
+ Array(pid_or_children(pid)).flatten.sort_by { |pid| -pid }
49
49
  end
50
50
 
51
51
  def pid_or_children(pid)
@@ -57,25 +57,20 @@ class Eye::SystemResources
57
57
  end
58
58
  end
59
59
 
60
- def args(pid)
61
- Eye::Sigar.proc_args(pid).join(' ').strip rescue '-'
62
- end
63
-
64
60
  def resources(pid)
65
- { memory: memory(pid),
66
- cpu: cpu(pid),
67
- start_time: start_time(pid),
68
- pid: pid }
61
+ { :memory => memory(pid),
62
+ :cpu => cpu(pid),
63
+ :start_time => start_time(pid),
64
+ :pid => pid
65
+ }
69
66
  end
70
67
 
71
68
  def cache
72
69
  Celluloid::Actor[:system_resources_cache]
73
70
  end
74
-
75
71
  end
76
72
 
77
73
  class Cache
78
-
79
74
  include Celluloid
80
75
 
81
76
  attr_reader :expire
@@ -100,15 +95,13 @@ class Eye::SystemResources
100
95
  def proc_mem(pid)
101
96
  @memory[pid] ||= Eye::Sigar.proc_mem(pid) if pid
102
97
 
103
- rescue ArgumentError
104
- # when incorrect PID, just skip
98
+ rescue ArgumentError # when incorrect PID
105
99
  end
106
100
 
107
101
  def proc_cpu(pid)
108
102
  @cpu[pid] ||= Eye::Sigar.proc_cpu(pid) if pid
109
103
 
110
- rescue ArgumentError
111
- # when incorrect PID, just skip
104
+ rescue ArgumentError # when incorrect PID
112
105
  end
113
106
 
114
107
  def children(pid)
@@ -118,10 +111,8 @@ class Eye::SystemResources
118
111
  []
119
112
  end
120
113
  end
121
-
122
114
  end
123
115
 
124
116
  # Setup global sigar singleton here
125
- Cache.supervise_as(:system_resources_cache)
126
-
117
+ Cache.supervise(as: :system_resources_cache)
127
118
  end
@@ -1,5 +1,4 @@
1
1
  class Eye::Trigger
2
-
3
2
  include Eye::Dsl::Validation
4
3
 
5
4
  autoload :Flapping, 'eye/trigger/flapping'
@@ -9,18 +8,19 @@ class Eye::Trigger
9
8
  autoload :CheckDependency, 'eye/trigger/check_dependency'
10
9
  autoload :StartingGuard, 'eye/trigger/starting_guard'
11
10
 
12
- TYPES = { flapping: 'Flapping', transition: 'Transition', stop_children: 'StopChildren',
13
- wait_dependency: 'WaitDependency', check_dependency: 'CheckDependency', starting_guard: 'StartingGuard' }
11
+ TYPES = {:flapping => 'Flapping', :transition => 'Transition', :stop_children => 'StopChildren',
12
+ :wait_dependency => 'WaitDependency', :check_dependency => 'CheckDependency', :starting_guard => 'StartingGuard'
13
+ }
14
14
 
15
15
  attr_reader :message, :options, :process
16
16
 
17
17
  def self.name_and_class(type)
18
18
  type = type.to_sym
19
- return { name: type, type: type } if TYPES[type]
19
+ return {:name => type, :type => type} if TYPES[type]
20
20
 
21
- if type =~ %r[\A(.*?)_?[0-9]+\z]
22
- ctype = Regexp.last_match(1).to_sym
23
- return { name: type, type: ctype } if TYPES[ctype]
21
+ if type =~ /\A(.*?)_?[0-9]+\z/
22
+ ctype = $1.to_sym
23
+ return {:name => type, :type => ctype} if TYPES[ctype]
24
24
  end
25
25
  end
26
26
 
@@ -36,7 +36,7 @@ class Eye::Trigger
36
36
  def self.create(process, options = {})
37
37
  get_class(options[:type]).new(process, options)
38
38
 
39
- rescue Object => ex
39
+ rescue Exception, Timeout::Error => ex
40
40
  log_ex(ex)
41
41
  nil
42
42
  end
@@ -72,7 +72,7 @@ class Eye::Trigger
72
72
 
73
73
  check(transition) if filter_transition(transition)
74
74
 
75
- rescue Object => ex
75
+ rescue Exception, Timeout::Error => ex
76
76
  if ex.class == Eye::Process::StateError
77
77
  raise ex
78
78
  else
@@ -92,7 +92,7 @@ class Eye::Trigger
92
92
  compare_state(trans.event, event)
93
93
  end
94
94
 
95
- def check(_transition)
95
+ def check(transition)
96
96
  raise NotImplementedError
97
97
  end
98
98
 
@@ -126,24 +126,22 @@ class Eye::Trigger
126
126
  end
127
127
 
128
128
  class Custom < Eye::Trigger
129
-
130
129
  def self.inherited(base)
131
130
  super
132
131
  register(base)
133
132
  end
134
-
135
133
  end
136
134
 
137
135
  private
138
136
 
139
137
  def compare_state(state_name, condition)
140
138
  case condition
141
- when Symbol
142
- state_name == condition
143
- when Array
144
- condition.include?(state_name)
145
- else
146
- true
139
+ when Symbol
140
+ state_name == condition
141
+ when Array
142
+ condition.include?(state_name)
143
+ else
144
+ true
147
145
  end
148
146
  end
149
147
 
@@ -1,5 +1,4 @@
1
1
  class Eye::Trigger::CheckDependency < Eye::Trigger
2
-
3
2
  param :names, [Array], true, 5
4
3
 
5
4
  def check(transition)
@@ -11,16 +10,14 @@ private
11
10
  def check_dependency(to)
12
11
  processes = names.map do |name|
13
12
  Eye::Control.find_nearest_process(name, process.group_name_pure, process.app_name)
14
- end
15
-
16
- processes = processes.compact.select { |p| p.state_name != :unmonitored }
13
+ end.compact.select { |p| p.state_name != :unmonitored }
17
14
  return if processes.empty?
18
15
  processes = Eye::Utils::AliveArray.new(processes)
19
16
 
20
17
  act = case to
21
- when :down, :restarting then :restart
22
- when :stopping then :stop
23
- when :unmonitored then :unmonitor
18
+ when :down, :restarting; :restart
19
+ when :stopping; :stop
20
+ when :unmonitored; :unmonitor
24
21
  end
25
22
 
26
23
  if act
@@ -7,12 +7,10 @@ class Eye::Trigger::Flapping < Eye::Trigger
7
7
  param :within, [Float, Fixnum], true
8
8
  param :retry_in, [Float, Fixnum]
9
9
  param :retry_times, [Fixnum]
10
- param :reretry_in, [Float, Fixnum]
11
- param :reretry_times, [Fixnum]
12
10
 
13
11
  def initialize(*args)
14
12
  super
15
- clear_counters
13
+ @flapping_times = 0
16
14
  end
17
15
 
18
16
  def check(transition)
@@ -21,16 +19,9 @@ class Eye::Trigger::Flapping < Eye::Trigger
21
19
 
22
20
  private
23
21
 
24
- def clear_counters
25
- @retry_times = 0
26
- @reretry_times = 0
27
- end
28
-
29
22
  def good?
30
- down_count = 0
31
- process.states_history.states_for_period(within, @last_at) do |s|
32
- down_count += 1 if s[:state] == :down
33
- end
23
+ states = process.states_history.states_for_period( within, @last_at )
24
+ down_count = states.count{|st| st == :down }
34
25
 
35
26
  if down_count >= times
36
27
  @last_at = process.states_history.last_state_changed_at
@@ -47,18 +38,10 @@ private
47
38
  process.schedule :unmonitor, Eye::Reason::Flapping.new(:flapping)
48
39
 
49
40
  return unless retry_in
50
- if !retry_times || (retry_times && @retry_times < retry_times)
51
- @retry_times += 1
52
- process.schedule_in(retry_in.to_f, :conditional_start, Eye::Reason::Flapping.new('retry start after flapping'))
53
- else
54
- if reretry_in
55
- if !reretry_times || (reretry_times && @reretry_times < reretry_times)
56
- @retry_times = 0
57
- @reretry_times += 1
58
- process.schedule_in(reretry_in.to_f, :conditional_start, Eye::Reason::Flapping.new('reretry start after flapping'))
59
- end
60
- end
61
- end
41
+ return if retry_times && @flapping_times >= retry_times
42
+
43
+ @flapping_times += 1
44
+ process.schedule_in(retry_in.to_f, :conditional_start, Eye::Reason::Flapping.new('retry start after flapping'))
62
45
  end
63
46
 
64
47
  end
@@ -33,33 +33,32 @@ class Eye::Trigger::StartingGuard < Eye::Trigger
33
33
  @reretry_count = 0
34
34
  return
35
35
  else
36
- info 'false executed condition'
36
+ info "false executed condition"
37
37
  end
38
38
 
39
39
  new_time = nil
40
40
  if every
41
41
  if times
42
- if @retry_count < times
42
+ if (@retry_count < times)
43
43
  new_time = Time.now + every
44
- process.schedule_in every, :conditional_start, Eye::Reason::StartingGuard.new('starting_guard, retry start')
44
+ process.schedule_in every, :conditional_start, Eye::Reason::StartingGuard.new("starting_guard, retry start")
45
45
  else
46
46
  @retry_count = 0
47
47
  @reretry_count += 1
48
48
  if retry_in && (!retry_times || (@reretry_count < retry_times))
49
49
  new_time = Time.now + retry_in
50
- process.schedule_in retry_in, :conditional_start, Eye::Reason::StartingGuard.new('starting_guard, reretry start')
50
+ process.schedule_in retry_in, :conditional_start, Eye::Reason::StartingGuard.new("starting_guard, reretry start")
51
51
  end
52
52
  end
53
53
  else
54
54
  new_time = Time.now + every
55
- process.schedule_in every, :conditional_start, Eye::Reason::StartingGuard.new('starting_guard, retry start')
55
+ process.schedule_in every, :conditional_start, Eye::Reason::StartingGuard.new("starting_guard, retry start")
56
56
  end
57
57
  end
58
58
 
59
59
  retry_msg = new_time ? ", retry at '#{Eye::Utils.human_time2(new_time.to_i)}'" : ''
60
60
  process.switch :unmonitoring, Eye::Reason::StartingGuard.new("starting_guard, failed condition#{retry_msg}")
61
61
 
62
- raise Eye::Process::StateError, 'starting_guard, refused to start'
62
+ raise Eye::Process::StateError.new("starting_guard, refused to start")
63
63
  end
64
-
65
64
  end
@@ -9,9 +9,9 @@ class Eye::Trigger::StopChildren < Eye::Trigger
9
9
  # default on stopped, crashed
10
10
  param_default :event, [:stopped, :crashed]
11
11
 
12
- def check(_trans)
12
+ def check(trans)
13
13
  debug { 'stopping children' }
14
- process.children.pmap { |_pid, c| c.stop }
14
+ process.children.pmap { |pid, c| c.stop }
15
15
  end
16
16
 
17
17
  end
@@ -4,7 +4,7 @@ class Eye::Trigger::Transition < Eye::Trigger
4
4
 
5
5
  param :do, [Proc, Symbol]
6
6
 
7
- def check(_trans)
7
+ def check(trans)
8
8
  exec_proc :do
9
9
  end
10
10
 
@@ -1,5 +1,4 @@
1
1
  class Eye::Trigger::WaitDependency < Eye::Trigger
2
-
3
2
  param :names, [Array], true
4
3
  param :wait_timeout, [Numeric], nil, 15.seconds
5
4
  param :retry_after, [Numeric], nil, 1.minute
@@ -19,7 +18,7 @@ private
19
18
  processes = Eye::Utils::AliveArray.new(processes)
20
19
 
21
20
  processes.each do |p|
22
- if p.state_name != :up && (should_start != false)
21
+ if p.state_name != :up && (should_start == nil || should_start)
23
22
  p.schedule :start, Eye::Reason.new(:start_dependency)
24
23
  end
25
24
  end
@@ -43,7 +42,7 @@ private
43
42
  process.schedule_in retry_after, :start, Eye::Reason.new(:wait_dependency)
44
43
  end
45
44
 
46
- raise Eye::Process::StateError, 'stop transition because dependency is not up'
45
+ raise Eye::Process::StateError.new('stop transition because dependency is not up')
47
46
  end
48
47
  end
49
48
 
@@ -1,15 +1,14 @@
1
1
  require 'date'
2
2
 
3
3
  module Eye::Utils
4
-
5
4
  autoload :Tail, 'eye/utils/tail'
6
5
  autoload :AliveArray, 'eye/utils/alive_array'
7
6
  autoload :CelluloidChain, 'eye/utils/celluloid_chain'
8
7
 
9
8
  def self.deep_clone(value)
10
9
  case
11
- when value.is_a?(Array) then value.map { |v| deep_clone(v) }
12
- when value.is_a?(Hash) then value.each_with_object({}) { |(k, v), r| r[deep_clone(k)] = deep_clone(v) }
10
+ when value.is_a?(Array) then value.map{|v| deep_clone(v) }
11
+ when value.is_a?(Hash) then value.inject({}){|r, (k, v)| r[ deep_clone(k) ] = deep_clone(v); r }
13
12
  else value
14
13
  end
15
14
  end
@@ -48,7 +47,7 @@ module Eye::Utils
48
47
  env_vars = content.split("\n")
49
48
  h = {}
50
49
  env_vars.each do |e|
51
- e = e.gsub(%r[#.+$], '').strip
50
+ e = e.gsub(/#.+$/, '').strip
52
51
  next unless e.include?('=')
53
52
  k, v = e.split('=', 2)
54
53
  h[k] = v
@@ -1,21 +1,20 @@
1
1
  class Eye::Utils::AliveArray
2
-
3
2
  extend Forwardable
4
3
  include Enumerable
5
4
 
6
5
  def_delegators :@arr, :[], :<<, :clear, :delete, :size, :empty?, :push,
7
- :flatten, :present?, :uniq!, :select!
6
+ :flatten, :present?, :uniq!, :select!
8
7
 
9
8
  def initialize(arr = [])
10
9
  @arr = arr
11
10
  end
12
11
 
13
12
  def each(&block)
14
- @arr.each { |elem| elem && elem.alive? && block[elem] }
13
+ @arr.each{|elem| elem && elem.alive? && block[elem] }
15
14
  end
16
15
 
17
16
  def to_a
18
- map { |x| x }
17
+ map{|x| x }
19
18
  end
20
19
 
21
20
  def full_size
@@ -34,10 +33,6 @@ class Eye::Utils::AliveArray
34
33
  self.class.new super
35
34
  end
36
35
 
37
- def sort!
38
- @arr.sort!
39
- end
40
-
41
36
  def +(other)
42
37
  if other.is_a?(Eye::Utils::AliveArray)
43
38
  @arr += other.pure
@@ -59,4 +54,4 @@ class Eye::Utils::AliveArray
59
54
  end
60
55
  end
61
56
 
62
- end
57
+ end
@@ -1,7 +1,6 @@
1
1
  require 'celluloid'
2
2
 
3
3
  class Eye::Utils::CelluloidChain
4
-
5
4
  include Celluloid
6
5
 
7
6
  def initialize(target)
@@ -11,21 +10,21 @@ class Eye::Utils::CelluloidChain
11
10
  @target_class = @target.class
12
11
  end
13
12
 
14
- def add(method_name, *args)
15
- @calls << { method_name: method_name, args: args }
13
+ def add(method_name, *args, &block)
14
+ @calls << {:method_name => method_name, :args => args, :block => block}
16
15
  ensure_process
17
16
  end
18
17
 
19
- def add_wo_dups(method_name, *args)
20
- h = { method_name: method_name, args: args }
18
+ def add_wo_dups(method_name, *args, &block)
19
+ h = {:method_name => method_name, :args => args, :block => block}
21
20
  if @calls[-1] != h
22
21
  @calls << h
23
22
  ensure_process
24
23
  end
25
24
  end
26
25
 
27
- def add_wo_dups_current(method_name, *args)
28
- h = { method_name: method_name, args: args }
26
+ def add_wo_dups_current(method_name, *args, &block)
27
+ h = {:method_name => method_name, :args => args, :block => block}
29
28
  if !@calls.include?(h) && @call != h
30
29
  @calls << h
31
30
  ensure_process
@@ -37,14 +36,14 @@ class Eye::Utils::CelluloidChain
37
36
  end
38
37
 
39
38
  def names_list
40
- list.map { |el| el[:method_name].to_sym }
39
+ list.map{|el| el[:method_name].to_sym }
41
40
  end
42
41
 
43
42
  def clear
44
43
  @calls = []
45
44
  end
46
45
 
47
- alias_method :clear_pending_list, :clear
46
+ alias :clear_pending_list :clear
48
47
 
49
48
  # need, because of https://github.com/celluloid/celluloid/issues/22
50
49
  def inspect
@@ -65,9 +64,8 @@ private
65
64
  def process
66
65
  while @call = @calls.shift
67
66
  @running = true
68
- @target.send(@call[:method_name], *@call[:args]) if @target.alive?
67
+ @target.send(@call[:method_name], *@call[:args], &@call[:block]) if @target.alive?
69
68
  end
70
69
  @running = false
71
70
  end
72
-
73
- end
71
+ end