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
data/bin/loader_eye ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib]))
3
+ require 'eye/loader'
4
+ require 'optparse'
5
+ require 'eye'
6
+
7
+ options = {:debug => false}
8
+
9
+ OptionParser.new do |opts|
10
+ opts.on( '-h', '--help', 'Display this screen' ) do
11
+ puts opts
12
+ exit
13
+ end
14
+
15
+ opts.on( '-c', '--config CONFIG', 'load with config' ) do |config_path|
16
+ options[:config] = config_path
17
+ end
18
+
19
+ opts.on( '-s', '--socket SOCKET', 'start listen on socket' ) do |socket_path|
20
+ options[:socket_path] = socket_path
21
+ end
22
+
23
+ opts.on( '-l', '--logger LOGGER', 'custom logger' ) do |logger|
24
+ options[:logger] = logger
25
+ end
26
+
27
+ opts.on( '-d', '--debug', 'debug info to logger' ) do
28
+ options[:debug] = true
29
+ end
30
+
31
+ end.parse!
32
+
33
+ Eye::Settings.ensure_eye_dir
34
+
35
+ socket_path = options[:socket_path] || Eye::Settings.socket_path
36
+ server = Eye::Server.new(socket_path)
37
+
38
+ Eye::Logger.log_level = options[:debug] ? Logger::DEBUG : Logger::INFO
39
+ Eye::Logger.link_logger(options[:logger]) if options[:logger]
40
+
41
+ config = options[:config]
42
+ config = File.expand_path(config) if config && !config.empty?
43
+
44
+ Eye::Control # preload
45
+
46
+ if config
47
+ res = server.command('load', config)
48
+ exit if res[:error]
49
+ end
50
+
51
+ Eye::Control.set_proc_line
52
+
53
+ server.async.run
54
+
55
+ trap("INT"){ exit }
56
+ trap("USR2"){ GC.start }
57
+
58
+ sleep
@@ -0,0 +1,18 @@
1
+ # Notify example
2
+
3
+ Eye.config do
4
+ mail :host => "mx.some.host", :port => 25, :domain => "some.host"
5
+ contact :errors, :mail, 'error@some.host'
6
+ contact :dev, :mail, 'dev@some.host'
7
+ end
8
+
9
+ Eye.application :some do
10
+ notify :errors
11
+
12
+ process :some_process do
13
+ notify :dev, :info
14
+
15
+ ...
16
+ end
17
+
18
+ end
@@ -0,0 +1,29 @@
1
+
2
+ def thin(proxy, port)
3
+ name = "thin-#{port}"
4
+
5
+ opts = [
6
+ "-l thins.log",
7
+ "-p #{port}",
8
+ "-P #{name}.pid",
9
+ "-d",
10
+ "-R thin.ru",
11
+ "--tag #{proxy.app.name}.#{proxy.name}",
12
+ "-t 60",
13
+ "-e #{proxy.env["RAILS_ENV"]}",
14
+ "-c #{proxy.working_dir}",
15
+ "-a 127.0.0.1"
16
+ ]
17
+
18
+ proxy.process(name) do
19
+ pid_file "#{name}.pid"
20
+
21
+ start_command "#{BUNDLE} exec thin start #{opts * ' '}"
22
+ stop_signals [:QUIT, 2.seconds, :TERM, 1.seconds, :KILL]
23
+
24
+ stdall "thin.stdall.log"
25
+
26
+ checks :http, :url => "http://127.0.0.1:#{port}/hello", :pattern => /World/,
27
+ :every => 5.seconds, :times => [2, 3], :timeout => 1.second
28
+ end
29
+ end
@@ -0,0 +1,57 @@
1
+ require 'bundler/setup'
2
+ require 'eventmachine'
3
+
4
+ def answer(data)
5
+ case data
6
+ when 'ping' then "pong\n"
7
+ when 'bad' then "what\n"
8
+ when 'timeout' then sleep 5; "ok\n"
9
+ when 'exception' then raise 'haha'
10
+ when 'quit' then EM.stop
11
+ when 'big' then 'a' * 10_000_000
12
+ end
13
+ end
14
+
15
+ class Echo < EM::Connection
16
+ def post_init
17
+ puts "-- someone connected to the echo server!"
18
+ end
19
+
20
+ def receive_data data
21
+ puts "receive #{data.inspect} "
22
+ send_data(answer(data))
23
+ end
24
+
25
+ def unbind
26
+ puts "-- someone disconnected from the echo server!"
27
+ end
28
+ end
29
+
30
+ class EchoObj < EM::Connection
31
+ include EM::P::ObjectProtocol
32
+
33
+ def post_init
34
+ puts "-- someone connected to the echo server!"
35
+ end
36
+
37
+ def receive_object obj # {:command => 'ping'}
38
+ puts "receive #{obj.inspect}"
39
+ send_object(answer(obj[:command]).chop)
40
+ end
41
+
42
+ def unbind
43
+ puts "-- someone disconnected from the echo server!"
44
+ end
45
+ end
46
+
47
+ trap "QUIT" do
48
+ puts "quit signal, stopping"
49
+ EM.stop
50
+ end
51
+
52
+ EM.run do
53
+ EM.start_server '127.0.0.1', 33221, Echo
54
+ EM.start_server '127.0.0.1', 33222, EchoObj
55
+ EM.start_server "/tmp/em_test_sock", nil, Echo
56
+ puts 'started'
57
+ end
@@ -0,0 +1,20 @@
1
+ require 'bundler/setup'
2
+ require 'forking'
3
+
4
+ root = File.expand_path(File.dirname(__FILE__))
5
+
6
+ f = Forking.new(:name => 'forking', :working_dir => root,
7
+ :log_file => "#{root}/forking.log",
8
+ :pid_file => "#{root}/forking.pid", :sync_log => true)
9
+
10
+ 3.times do |i|
11
+ f.spawn(:log_file => "#{root}/child#{i}.log", :sync_log => true) do
12
+ $0 = "forking child"
13
+ loop do
14
+ p "#{Time.now} - #{Time.now.to_f} - #{i} - tick"
15
+ sleep 0.1
16
+ end
17
+ end
18
+ end
19
+
20
+ f.run!
@@ -0,0 +1,144 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+
4
+ # This hash will hold all of the options
5
+ # parsed from the command-line by
6
+ # OptionParser.
7
+ options = {}
8
+
9
+ optparse = OptionParser.new do|opts|
10
+ # This displays the help screen, all programs are
11
+ # assumed to have this option.
12
+ opts.on( '-h', '--help', 'Display this screen' ) do
13
+ puts opts
14
+ exit
15
+ end
16
+
17
+ opts.on( '-p', '--pid FILE', 'pid_file' ) do |a|
18
+ options[:pid_file] = a
19
+ end
20
+
21
+ opts.on( '-l', '--log FILE', 'log_file' ) do |a|
22
+ options[:log_file] = a
23
+ end
24
+
25
+ opts.on( '-L', '--lock FILE', 'lock_file' ) do |a|
26
+ options[:lock_file] = a
27
+ end
28
+
29
+ opts.on( '-d', '--daemonize', 'Daemonize' ) do
30
+ options[:daemonize] = true
31
+ end
32
+
33
+ opts.on( '-s', '--daemonize_delay DELAY', 'Daemonized time' ) do |d|
34
+ options[:daemonize_delay] = d
35
+ end
36
+
37
+ opts.on( '-r', '--raise', 'Raised execution' ) do
38
+ options[:raise] = true
39
+ end
40
+
41
+ opts.on( '-w', '--watch_file FILE', 'Exit on touched file' ) do |w|
42
+ options[:watch_file] = w
43
+ end
44
+
45
+ opts.on( '-W', '--watch_file_delay DELAY', 'Exit on touched file, after delay' ) do |w|
46
+ options[:watch_file_delay] = w
47
+ end
48
+
49
+ end
50
+
51
+ optparse.parse!
52
+
53
+ module Sample
54
+ def puts(mes = "")
55
+ tm = Time.now
56
+ STDOUT.puts "#{tm.to_s} (#{tm.to_f}) - #{mes}"
57
+ STDOUT.flush
58
+ end
59
+
60
+ def daemonize(pid_file, log_file, daemonize_delay = 0)
61
+ puts "daemonize start #{pid_file}, #{log_file}, #{daemonize_delay}"
62
+
63
+ if daemonize_delay && daemonize_delay.to_f > 0
64
+ puts "daemonize delay start #{daemonize_delay}"
65
+ sleep daemonize_delay.to_f
66
+ puts "daemonize delay end"
67
+ end
68
+
69
+ daemon
70
+ STDOUT.reopen(log_file, "a")
71
+ STDERR.reopen(log_file, "a")
72
+ File.open(pid_file, 'w'){|f| f.write $$.to_s}
73
+
74
+ puts "daemonized"
75
+ end
76
+
77
+ def daemon
78
+ exit if fork # Parent exits, child continues.
79
+ Process.setsid # Become session leader.
80
+ exit if fork # Zap session leader. See [1].
81
+
82
+ STDIN.reopen "/dev/null" # Free file descriptors and
83
+ STDOUT.reopen "/dev/null", "a" # point them somewhere sensible.
84
+ STDERR.reopen '/dev/null', 'a'
85
+ return 0
86
+ end
87
+ end
88
+
89
+ extend Sample
90
+
91
+ if options[:daemonize]
92
+ daemonize(options[:pid_file], options[:log_file], options[:daemonize_delay])
93
+ end
94
+
95
+ puts "Started #{ARGV.inspect}, #{options.inspect}, #{ENV["ENV1"]}"
96
+
97
+ if options[:lock_file]
98
+ if File.exists?(options[:lock_file])
99
+ puts "Lock file exists, exiting"
100
+ exit 1
101
+ else
102
+ File.open(options[:lock_file], 'w'){|f| f.write $$ }
103
+ end
104
+ end
105
+
106
+ if options[:raise]
107
+ puts "Raised"
108
+ File.unlink(options[:lock_file]) if options[:lock_file]
109
+ exit 1
110
+ end
111
+
112
+ trap("USR1") do
113
+ puts "USR1 signal!"
114
+ end
115
+
116
+ trap("USR2") do
117
+ puts "USR2 start memory leak"
118
+ $ar = []
119
+ 300_000.times{|i| $ar << "memory leak #{i}" * 10}
120
+ end
121
+
122
+ loop do
123
+ sleep 0.1
124
+ puts "tick"
125
+
126
+ if options[:watch_file]
127
+ if File.exists?(options[:watch_file])
128
+ puts "watch file finded"
129
+ File.unlink(options[:watch_file])
130
+
131
+ if options[:watch_file_delay]
132
+ puts "watch_file delay start"
133
+ sleep options[:watch_file_delay].to_f
134
+ puts "watch_file delay end"
135
+ end
136
+
137
+ break
138
+ end
139
+ end
140
+ end
141
+
142
+ puts "exit"
143
+ File.unlink(options[:lock_file]) if options[:lock_file]
144
+ exit 0
@@ -0,0 +1,12 @@
1
+ require 'bundler/setup'
2
+ require 'sinatra'
3
+
4
+ class Test < Sinatra::Base
5
+
6
+ get '/hello' do
7
+ sleep 0.5
8
+ "Hello World!"
9
+ end
10
+ end
11
+
12
+ run Test.new
data/examples/puma.eye ADDED
@@ -0,0 +1,34 @@
1
+ RUBY = '/usr/local/ruby/1.9.3-p392/bin/ruby'
2
+ RAILS_ENV = 'production'
3
+ ROOT = '/var/www/super_app'
4
+ CURRENT = File.expand_path(File.join(ROOT, %w{current}))
5
+ LOGS = File.expand_path(File.join(ROOT, %w{shared log}))
6
+ PIDS = File.expand_path(File.join(ROOT, %w{shared pids}))
7
+
8
+ Eye.config do
9
+ logger "#{LOGS}/eye.log"
10
+ logger_level Logger::ERROR
11
+ end
12
+
13
+ Eye.application :super_app do
14
+ env 'RAILS_ENV' => RAILS_ENV
15
+ working_dir CURRENT
16
+ triggers :flapping, :times => 10, :within => 1.minute
17
+
18
+ process :puma do
19
+ daemonize true
20
+ pid_file "#{PIDS}/puma.pid"
21
+ stdall "#{LOGS}/#{RAILS_ENV}.log"
22
+
23
+ start_command "#{RUBY} bin/puma --port 80 --pidfile #{PIDS}/puma.pid --environment #{RAILS_ENV} config.ru"
24
+ stop_command "kill -TERM {{PID}}"
25
+ restart_command "kill -USR2 {{PID}}"
26
+
27
+ start_timeout 15.seconds
28
+ stop_grace 10.seconds
29
+ restart_grace 10.seconds
30
+
31
+ checks :cpu, :every => 30, :below => 80, :times => 3
32
+ checks :memory, :every => 30, :below => 70.megabytes, :times => [3,5]
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ Eye.application "rbenv_example" do
2
+ env 'RBENV_ROOT' => '/usr/local/rbenv', 'PATH' => "/usr/local/rbenv/shims:/usr/local/rbenv/bin:#{ENV['PATH']}"
3
+ working_dir "/projects/some_project"
4
+
5
+ process "some_process" do
6
+ pid_file "some.pid"
7
+ start_command "ruby some.rb"
8
+ daemonize true
9
+ stdall "some.log"
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ # Example: how to run sidekiq daemon
2
+
3
+ def sidekiq_process(proxy, name)
4
+ rails_env = proxy.env['RAILS_ENV']
5
+
6
+ proxy.process(name) do
7
+ start_command "ruby ./bin/sidekiq -e #{rails_env} -C ./config/sidekiq.#{rails_env}.yml"
8
+ pid_file "tmp/pids/#{name}.pid"
9
+ stdall "log/#{name}.log"
10
+ daemonize true
11
+ stop_signals [:QUIT, 5.seconds, :TERM, 5.seconds, :KILL]
12
+
13
+ checks :cpu, :every => 30, :below => 100, :times => 5
14
+ checks :memory, :every => 30, :below => 300.megabytes, :times => 5
15
+ end
16
+ end
17
+
18
+ Eye.application :sidekiq_test do
19
+ working_dir '/some_dir'
20
+ env "RAILS_ENV" => 'production'
21
+
22
+ sidekiq_process self, :sidekiq
23
+ end
data/examples/test.eye ADDED
@@ -0,0 +1,81 @@
1
+ # load submodules, here just for example
2
+ Eye.load("./eye/*.rb")
3
+
4
+ # Eye self-configuration section
5
+ Eye.config do
6
+ logger "/tmp/eye.log"
7
+ end
8
+
9
+ # Adding application
10
+ Eye.application "test" do
11
+ # All options inherits down to the config leafs.
12
+ # except `env`, which merging down
13
+
14
+ working_dir File.expand_path(File.join(File.dirname(__FILE__), %w[ processes ]))
15
+ stdall "trash.log" # stdout,err logs for processes by default
16
+ env "APP_ENV" => "production" # global env for each processes
17
+ triggers :flapping, :times => 10, :within => 1.minute, :retry_in => 10.minutes
18
+ checks :cpu, :below => 100, :times => 3 # global check for all processes
19
+
20
+ group "samples" do
21
+ chain :grace => 5.seconds # chained start-restart with 5s interval, one by one.
22
+
23
+ # eye daemonized process
24
+ process :sample1 do
25
+ pid_file "1.pid" # pid_path will be expanded with the working_dir
26
+ start_command "ruby ./sample.rb"
27
+ daemonize true
28
+ stdall "sample1.log"
29
+
30
+ checks :cpu, :below => 30, :times => [3, 5]
31
+ end
32
+
33
+ # self daemonized process
34
+ process :sample2 do
35
+ pid_file "2.pid"
36
+ start_command "ruby ./sample.rb -d --pid 2.pid --log sample2.log"
37
+ stop_command "kill -9 {PID}"
38
+
39
+ checks :memory, :below => 300.megabytes, :times => 3
40
+ end
41
+ end
42
+
43
+ # daemon with 3 childs
44
+ process :forking do
45
+ pid_file "forking.pid"
46
+ start_command "ruby ./forking.rb start"
47
+ stop_command "ruby forking.rb stop"
48
+ stdall "forking.log"
49
+
50
+ start_timeout 5.seconds
51
+ stop_grace 5.seconds
52
+
53
+ monitor_children do
54
+ restart_command "kill -2 {PID}" # for this child process
55
+ checks :memory, :below => 300.megabytes, :times => 3
56
+ end
57
+ end
58
+
59
+ # eventmachine process, daemonized with eye
60
+ process :event_machine do |p|
61
+ pid_file 'em.pid'
62
+ start_command 'ruby em.rb'
63
+ stdout 'em.log'
64
+ daemonize true
65
+ stop_signals [:QUIT, 2.seconds, :KILL]
66
+
67
+ checks :socket, :addr => "tcp://127.0.0.1:33221", :every => 10.seconds, :times => 2,
68
+ :timeout => 1.second, :send_data => "ping", :expect_data => /pong/
69
+ end
70
+
71
+ # thin process, self daemonized
72
+ process :thin do
73
+ pid_file "thin.pid"
74
+ start_command "bundle exec thin start -R thin.ru -p 33233 -d -l thin.log -P thin.pid"
75
+ stop_signals [:QUIT, 2.seconds, :TERM, 1.seconds, :KILL]
76
+
77
+ checks :http, :url => "http://127.0.0.1:33233/hello", :pattern => /World/, :every => 5.seconds,
78
+ :times => [2, 3], :timeout => 1.second
79
+ end
80
+
81
+ end