reel-eye 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +32 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +3 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +22 -0
  7. data/README.md +170 -0
  8. data/Rakefile +20 -0
  9. data/bin/eye +322 -0
  10. data/bin/loader_eye +58 -0
  11. data/examples/notify.eye +18 -0
  12. data/examples/process_thin.rb +29 -0
  13. data/examples/processes/em.rb +57 -0
  14. data/examples/processes/forking.rb +20 -0
  15. data/examples/processes/sample.rb +144 -0
  16. data/examples/processes/thin.ru +12 -0
  17. data/examples/puma.eye +34 -0
  18. data/examples/rbenv.eye +11 -0
  19. data/examples/sidekiq.eye +23 -0
  20. data/examples/test.eye +81 -0
  21. data/examples/thin-farm.eye +29 -0
  22. data/examples/unicorn.eye +31 -0
  23. data/eye.gemspec +42 -0
  24. data/lib/eye.rb +28 -0
  25. data/lib/eye/application.rb +74 -0
  26. data/lib/eye/checker.rb +138 -0
  27. data/lib/eye/checker/cpu.rb +27 -0
  28. data/lib/eye/checker/file_ctime.rb +25 -0
  29. data/lib/eye/checker/file_size.rb +34 -0
  30. data/lib/eye/checker/http.rb +98 -0
  31. data/lib/eye/checker/memory.rb +27 -0
  32. data/lib/eye/checker/socket.rb +152 -0
  33. data/lib/eye/child_process.rb +101 -0
  34. data/lib/eye/client.rb +32 -0
  35. data/lib/eye/config.rb +88 -0
  36. data/lib/eye/control.rb +2 -0
  37. data/lib/eye/controller.rb +53 -0
  38. data/lib/eye/controller/commands.rb +73 -0
  39. data/lib/eye/controller/helpers.rb +61 -0
  40. data/lib/eye/controller/load.rb +214 -0
  41. data/lib/eye/controller/options.rb +48 -0
  42. data/lib/eye/controller/send_command.rb +115 -0
  43. data/lib/eye/controller/show_history.rb +62 -0
  44. data/lib/eye/controller/status.rb +131 -0
  45. data/lib/eye/dsl.rb +48 -0
  46. data/lib/eye/dsl/application_opts.rb +33 -0
  47. data/lib/eye/dsl/chain.rb +12 -0
  48. data/lib/eye/dsl/child_process_opts.rb +8 -0
  49. data/lib/eye/dsl/config_opts.rb +48 -0
  50. data/lib/eye/dsl/group_opts.rb +27 -0
  51. data/lib/eye/dsl/helpers.rb +12 -0
  52. data/lib/eye/dsl/main.rb +40 -0
  53. data/lib/eye/dsl/opts.rb +140 -0
  54. data/lib/eye/dsl/process_opts.rb +21 -0
  55. data/lib/eye/dsl/pure_opts.rb +110 -0
  56. data/lib/eye/dsl/validation.rb +59 -0
  57. data/lib/eye/group.rb +134 -0
  58. data/lib/eye/group/chain.rb +81 -0
  59. data/lib/eye/http.rb +31 -0
  60. data/lib/eye/http/router.rb +25 -0
  61. data/lib/eye/loader.rb +23 -0
  62. data/lib/eye/logger.rb +80 -0
  63. data/lib/eye/notify.rb +86 -0
  64. data/lib/eye/notify/jabber.rb +30 -0
  65. data/lib/eye/notify/mail.rb +44 -0
  66. data/lib/eye/process.rb +86 -0
  67. data/lib/eye/process/child.rb +58 -0
  68. data/lib/eye/process/commands.rb +256 -0
  69. data/lib/eye/process/config.rb +70 -0
  70. data/lib/eye/process/controller.rb +76 -0
  71. data/lib/eye/process/data.rb +47 -0
  72. data/lib/eye/process/monitor.rb +95 -0
  73. data/lib/eye/process/notify.rb +32 -0
  74. data/lib/eye/process/scheduler.rb +78 -0
  75. data/lib/eye/process/states.rb +86 -0
  76. data/lib/eye/process/states_history.rb +66 -0
  77. data/lib/eye/process/system.rb +97 -0
  78. data/lib/eye/process/trigger.rb +54 -0
  79. data/lib/eye/process/validate.rb +23 -0
  80. data/lib/eye/process/watchers.rb +69 -0
  81. data/lib/eye/reason.rb +20 -0
  82. data/lib/eye/server.rb +52 -0
  83. data/lib/eye/settings.rb +46 -0
  84. data/lib/eye/system.rb +154 -0
  85. data/lib/eye/system_resources.rb +86 -0
  86. data/lib/eye/trigger.rb +53 -0
  87. data/lib/eye/trigger/flapping.rb +28 -0
  88. data/lib/eye/utils.rb +14 -0
  89. data/lib/eye/utils/alive_array.rb +31 -0
  90. data/lib/eye/utils/celluloid_chain.rb +70 -0
  91. data/lib/eye/utils/leak_19.rb +7 -0
  92. data/lib/eye/utils/tail.rb +20 -0
  93. metadata +390 -0
@@ -0,0 +1,70 @@
1
+ module Eye::Process::Config
2
+
3
+ DEFAULTS = {
4
+ :keep_alive => true, # restart when crashed
5
+ :check_alive_period => 5.seconds,
6
+
7
+ :start_timeout => 15.seconds,
8
+ :stop_timeout => 10.seconds,
9
+ :restart_timeout => 10.seconds,
10
+
11
+ :start_grace => 2.5.seconds,
12
+ :stop_grace => 0.5.seconds,
13
+ :restart_grace => 0.5.seconds,
14
+
15
+ :daemonize => false,
16
+ :auto_start => true, # auto start on monitor action
17
+
18
+ :childs_update_period => 30.seconds
19
+ }
20
+
21
+ def prepare_config(new_config)
22
+ h = DEFAULTS.merge(new_config)
23
+ h[:pid_file_ex] = Eye::System.normalized_file(h[:pid_file], h[:working_dir]) if h[:pid_file]
24
+ h[:checks] = {} if h[:checks].blank?
25
+ h[:triggers] = {} if h[:triggers].blank?
26
+ h[:childs_update_period] = h[:monitor_children][:childs_update_period] if h[:monitor_children] && h[:monitor_children][:childs_update_period]
27
+
28
+ # check speedy flapping by default
29
+ if h[:triggers].blank? || !h[:triggers][:flapping]
30
+ h[:triggers] ||= {}
31
+ h[:triggers][:flapping] = {:type => :flapping, :times => 10, :within => 10.seconds}
32
+ end
33
+
34
+ h[:stdout] = Eye::System.normalized_file(h[:stdout], h[:working_dir]) if h[:stdout]
35
+ h[:stderr] = Eye::System.normalized_file(h[:stderr], h[:working_dir]) if h[:stderr]
36
+
37
+ h
38
+ end
39
+
40
+ def c(name)
41
+ @config[name]
42
+ end
43
+
44
+ def [](name)
45
+ @config[name]
46
+ end
47
+
48
+ def update_config(new_config = {})
49
+ new_config = prepare_config(new_config)
50
+ @config = new_config
51
+ @full_name = nil
52
+
53
+ debug "update config to: #{@config.inspect}"
54
+
55
+ remove_triggers
56
+ add_triggers
57
+
58
+ if up?
59
+ # rebuild checks for this process
60
+ from_up; on_up
61
+ end
62
+ end
63
+
64
+ # is pid_file under Eye::Process control, or not
65
+ def control_pid?
66
+ return self[:control_pid] unless self[:control_pid].nil?
67
+ !!self[:daemonize]
68
+ end
69
+
70
+ end
@@ -0,0 +1,76 @@
1
+ module Eye::Process::Controller
2
+
3
+ def send_command(command, *args)
4
+ schedule command, *args, Eye::Reason::User.new(command)
5
+ end
6
+
7
+ def start
8
+ res = if set_pid_from_file
9
+ if process_realy_running?
10
+ info "process from pid_file(#{self.pid}) found and already running, so :up"
11
+ switch :already_running
12
+ :ok
13
+ else
14
+ info "pid_file found, but process in pid_file(#{self.pid}) not found, starting..."
15
+ start_process
16
+ end
17
+ else
18
+ info 'pid_file not found, so starting process...'
19
+ start_process
20
+ end
21
+
22
+ res
23
+ end
24
+
25
+ def stop
26
+ stop_process
27
+ switch :unmonitoring
28
+ end
29
+
30
+ def restart
31
+ unless pid # unmonitored case
32
+ try_update_pid_from_file
33
+ end
34
+
35
+ restart_process
36
+ end
37
+
38
+ def monitor
39
+ if self[:auto_start]
40
+ start
41
+ else
42
+ if try_update_pid_from_file
43
+ info "process from pid_file(#{self.pid}) found and already running, so :up"
44
+ switch :already_running
45
+ else
46
+ warn "process not found, so :unmonitor"
47
+ schedule :unmonitor, Eye::Reason.new(:'not found')
48
+ end
49
+ end
50
+ end
51
+
52
+ def unmonitor
53
+ switch :unmonitoring
54
+ end
55
+
56
+ def delete
57
+ if self[:stop_on_delete]
58
+ info 'process has stop_on_delete option, so sync-stop it first'
59
+ stop
60
+ end
61
+
62
+ remove_watchers
63
+ remove_childs
64
+ remove_triggers
65
+
66
+ terminate
67
+ end
68
+
69
+ def signal(sig = 0)
70
+ if self.pid
71
+ res = send_signal(sig)
72
+ info "send signal #{sig} to #{self.pid} = #{res}"
73
+ end
74
+ end
75
+
76
+ end
@@ -0,0 +1,47 @@
1
+ module Eye::Process::Data
2
+
3
+ # logger tag
4
+ def full_name
5
+ @full_name ||= [self[:application], (self[:group] == '__default__') ? nil : self[:group], self[:name]].compact.join(':')
6
+ end
7
+
8
+ def status_data(debug = false)
9
+ p_st = self_status_data(debug)
10
+
11
+ if childs.present?
12
+ p_st.merge(:subtree => Eye::Utils::AliveArray.new(childs.values).map{|c| c.status_data(debug) } )
13
+ elsif self[:monitor_children] && self.up?
14
+ p_st.merge(:subtree => [{name: '=loading childs='}])
15
+ else
16
+ # common state
17
+ p_st
18
+ end
19
+ end
20
+
21
+ def self_status_data(debug = false)
22
+ h = { name: name, state: state,
23
+ type: (self.class == Eye::ChildProcess ? :child_process : :process),
24
+ resources: Eye::SystemResources.resources(pid) }
25
+
26
+ if @states_history
27
+ h.merge!( state_changed_at: @states_history.last_state_changed_at,
28
+ state_reason: @states_history.last_reason )
29
+ end
30
+
31
+ h.merge!(debug: debug_data) if debug
32
+ h.merge!(current_command: current_scheduled_command) if current_scheduled_command
33
+
34
+ h
35
+ end
36
+
37
+ def debug_data
38
+ { queue: scheduler_actions_list, watchers: @watchers.keys }
39
+ end
40
+
41
+ def sub_object?(obj)
42
+ return false if self.class == Eye::ChildProcess
43
+ self.childs.each { |_, child| return true if child == obj }
44
+ false
45
+ end
46
+
47
+ end
@@ -0,0 +1,95 @@
1
+ module Eye::Process::Monitor
2
+
3
+ private
4
+
5
+ def check_alive_with_refresh_pid_if_needed
6
+ if process_realy_running?
7
+ return true
8
+
9
+ else
10
+ warn 'process not realy running'
11
+ try_update_pid_from_file
12
+ end
13
+ end
14
+
15
+ def try_update_pid_from_file
16
+ # if pid file was rewrited
17
+ newpid = load_pid_from_file
18
+ if newpid != self.pid
19
+ info "process changed pid to #{newpid}, updating..." if self.pid
20
+ self.pid = newpid
21
+
22
+ if process_realy_running?
23
+ return true
24
+ else
25
+ warn "process with new_pid #{newpid} not found"
26
+ return false
27
+ end
28
+ else
29
+ debug 'process not found'
30
+ return false
31
+ end
32
+ end
33
+
34
+ REWRITE_FACKUP_PIDFILE_PERIOD = 2.minutes
35
+
36
+ def check_alive
37
+ if up?
38
+
39
+ # check that process runned
40
+ unless process_realy_running?
41
+ warn "check_alive: process(#{self.pid}) not found!"
42
+ notify :info, 'crashed!'
43
+ switch :crashed, Eye::Reason.new(:crashed)
44
+ else
45
+ # check that pid_file still here
46
+ ppid = failsafe_load_pid
47
+
48
+ if ppid != self.pid
49
+ msg = "check_alive: pid_file(#{self[:pid_file]}) changes by itself (pid:#{self.pid}) => (pid:#{ppid})"
50
+ if control_pid?
51
+ msg += ", not correct, pid_file is under eye control, so rewrited back pid:#{self.pid}"
52
+ unless failsafe_save_pid
53
+ msg += ', (Can`t rewrite pid_file O_o)'
54
+ end
55
+ else
56
+ if ppid == nil
57
+ msg += ', rewrited because empty'
58
+ unless failsafe_save_pid
59
+ msg += ', (Can`t rewrite pid_file O_o)'
60
+ end
61
+ elsif (Time.now - pid_file_ctime > REWRITE_FACKUP_PIDFILE_PERIOD)
62
+ msg += ", > #{REWRITE_FACKUP_PIDFILE_PERIOD.inspect} ago, so rewrited (even if pid_file not under eye control)"
63
+ unless failsafe_save_pid
64
+ msg += ', (Can`t rewrite pid_file O_o)'
65
+ end
66
+ else
67
+ msg += ', not under eye control, so ignored'
68
+ end
69
+ end
70
+
71
+ warn msg
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ def check_crash
78
+ if down?
79
+ if self[:keep_alive]
80
+ warn 'check crashed: process is down'
81
+ schedule :restore, Eye::Reason.new(:crashed)
82
+ else
83
+ warn 'check crashed: process without keep_alive'
84
+ schedule :unmonitor, Eye::Reason.new(:crashed)
85
+ end
86
+ else
87
+ debug 'check crashed: skipped, process is not in down'
88
+ end
89
+ end
90
+
91
+ def restore
92
+ start if down?
93
+ end
94
+
95
+ end
@@ -0,0 +1,32 @@
1
+ module Eye::Process::Notify
2
+
3
+ # notify to user:
4
+ # 1) process crashed by itself, and we restart it [:info]
5
+ # 2) checker bounded to restart process [:warn]
6
+ # 3) flapping + switch to unmonitored [:error]
7
+
8
+ LEVELS = {:debug => 0, :info => 1, :warn => 2, :error => 3, :fatal => 4}
9
+
10
+ def notify(level, msg)
11
+ # logging it
12
+ error "NOTIFY: #{msg}" if ilevel(level) > ilevel(:info)
13
+
14
+ # send notifies
15
+ if self[:notify].present?
16
+ message = {:message => msg, :name => name,
17
+ :full_name => full_name, :pid => pid, :host => Eye::System.host, :level => level,
18
+ :at => Time.now }
19
+
20
+ self[:notify].each do |contact, not_level|
21
+ Eye::Notify.notify(contact, message) if ilevel(level) >= ilevel(not_level)
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def ilevel(level)
29
+ LEVELS[level].to_i
30
+ end
31
+
32
+ end
@@ -0,0 +1,78 @@
1
+ module Eye::Process::Scheduler
2
+
3
+ # ex: schedule :update_config, config, "reason: update_config"
4
+ def schedule(command, *args, &block)
5
+ if scheduler.alive?
6
+ unless self.respond_to?(command, true)
7
+ warn "object not support :#{command} to schedule"
8
+ return
9
+ end
10
+
11
+ reason = if args.present? && args[-1].kind_of?(Eye::Reason)
12
+ args.pop
13
+ end
14
+
15
+ info "schedule :#{command} #{reason ? "(reason: #{reason})" : nil}"
16
+
17
+ if reason.class == Eye::Reason
18
+ # for auto reasons
19
+ # skip already running commands and all in chain
20
+ scheduler.add_wo_dups_current(:scheduled_action, command, {:args => args, :reason => reason}, &block)
21
+ else
22
+ # for manual, or without reason
23
+ # skip only for last in chain
24
+ scheduler.add_wo_dups(:scheduled_action, command, {:args => args, :reason => reason}, &block)
25
+ end
26
+ end
27
+ end
28
+
29
+ def schedule_in(interval, command, *args, &block)
30
+ debug "schedule_in #{interval} :#{command} #{args}"
31
+ after(interval.to_f) do
32
+ debug "scheduled_in #{interval} :#{command} #{args}"
33
+ schedule(command, *args, &block)
34
+ end
35
+ end
36
+
37
+ def scheduled_action(command, h = {}, &block)
38
+ reason = h.delete(:reason)
39
+ info "=> #{command} #{h[:args].present? ? "#{h[:args]*',' }" : nil} #{reason ? "(reason: #{reason})" : nil}"
40
+
41
+ @current_scheduled_command = command
42
+ @last_scheduled_command = command
43
+ @last_scheduled_reason = reason
44
+ @last_scheduled_at = Time.now
45
+
46
+ send(command, *h[:args], &block)
47
+ @current_scheduled_command = nil
48
+ info "<= #{command}"
49
+
50
+ schedule_history.push(command, reason, @last_scheduled_at.to_i)
51
+ end
52
+
53
+ def scheduler_actions_list
54
+ scheduler.list.map{|c| c[:args].first rescue nil }.compact
55
+ end
56
+
57
+ def self.included(base)
58
+ base.finalizer :remove_scheduler
59
+ end
60
+
61
+ attr_accessor :current_scheduled_command
62
+ attr_accessor :last_scheduled_command, :last_scheduled_reason, :last_scheduled_at
63
+
64
+ def schedule_history
65
+ @schedule_history ||= Eye::Process::StatesHistory.new(50)
66
+ end
67
+
68
+ private
69
+
70
+ def remove_scheduler
71
+ @scheduler.terminate if @scheduler && @scheduler.alive?
72
+ end
73
+
74
+ def scheduler
75
+ @scheduler ||= Eye::Utils::CelluloidChain.new(current_actor)
76
+ end
77
+
78
+ end
@@ -0,0 +1,86 @@
1
+ require 'state_machine'
2
+
3
+ class Eye::Process
4
+
5
+ # do transition
6
+ def switch(name, reason = nil)
7
+ @state_reason = reason || last_scheduled_reason
8
+ self.send("#{name}!")
9
+ end
10
+
11
+ state_machine :state, :initial => :unmonitored do
12
+ state :unmonitored, :up, :down
13
+ state :starting, :stopping, :restarting
14
+
15
+ event :starting do
16
+ transition [:unmonitored, :down] => :starting
17
+ end
18
+
19
+ event :already_running do
20
+ transition [:unmonitored, :down, :up] => :up
21
+ end
22
+
23
+ event :started do
24
+ transition :starting => :up
25
+ end
26
+
27
+ event :crashed do
28
+ transition [:starting, :restarting, :up] => :down
29
+ end
30
+
31
+ event :stopping do
32
+ transition [:up, :restarting] => :stopping
33
+ end
34
+
35
+ event :stopped do
36
+ transition :stopping => :down
37
+ end
38
+
39
+ event :cant_kill do
40
+ transition :stopping => :up
41
+ end
42
+
43
+ event :restarting do
44
+ transition [:unmonitored, :up, :down] => :restarting
45
+ end
46
+
47
+ event :restarted do
48
+ transition :restarting => :up
49
+ end
50
+
51
+ event :unmonitoring do
52
+ transition any => :unmonitored
53
+ end
54
+
55
+ after_transition any => :unmonitored, :do => :on_unmonitored
56
+ after_transition any-:up => :up, :do => :on_up
57
+ after_transition :up => any-:up, :do => :from_up
58
+ after_transition any => any, :do => :log_transition
59
+ after_transition any => any, :do => :check_triggers
60
+ after_transition :on => :crashed, :do => :on_crashed
61
+ end
62
+
63
+ def on_crashed
64
+ schedule :check_crash, Eye::Reason.new(:crashed)
65
+ end
66
+
67
+ def on_unmonitored
68
+ self.pid = nil
69
+ end
70
+
71
+ def on_up
72
+ add_watchers
73
+ add_childs
74
+ end
75
+
76
+ def from_up
77
+ remove_watchers
78
+ remove_childs
79
+ end
80
+
81
+ def log_transition(transition)
82
+ @states_history.push transition.to_name, @state_reason
83
+ info "switch :#{transition.event} [:#{transition.from_name} => :#{transition.to_name}] #{@state_reason ? "(reason: #{@state_reason})" : nil}"
84
+ end
85
+
86
+ end