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/lib/eye/loader.rb ADDED
@@ -0,0 +1,23 @@
1
+ # mini bundler, for embedded server gem installation
2
+
3
+ gem 'celluloid', '~> 0.13.0'
4
+ gem 'celluloid-io', '~> 0.13.0'
5
+ gem 'nio4r'
6
+ gem 'timers'
7
+
8
+ gem 'state_machine', '< 1.2'
9
+
10
+ # activesupport
11
+ gem 'activesupport', '~> 3.2.0'
12
+ gem 'i18n'
13
+ gem 'multi_json'
14
+
15
+ # reel
16
+ gem 'reel', '~> 0.4.0.pre'
17
+ gem 'rack'
18
+ gem 'http'
19
+ gem 'http_parser.rb'
20
+ gem 'certified'
21
+ gem 'websocket_parser'
22
+
23
+ gem 'cuba'
data/lib/eye/logger.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'logger'
2
+
3
+ class Eye::Logger
4
+ attr_accessor :prefix, :subprefix
5
+
6
+ class InnerLogger < Logger
7
+ FORMAT = '%d.%m.%Y %H:%M:%S'
8
+
9
+ def initialize(*args)
10
+ super
11
+
12
+ self.formatter = Proc.new do |s, d, p, m|
13
+ "#{d.strftime(FORMAT)} #{s.ljust(5)} -- #{m}\n"
14
+ end
15
+ end
16
+ end
17
+
18
+ module Helpers
19
+ attr_reader :logger
20
+
21
+ Logger::Severity.constants.each do |level|
22
+ method_name = level.to_s.downcase
23
+ define_method method_name do |msg|
24
+ @logger.send(method_name, msg)
25
+ end
26
+ end
27
+ end
28
+
29
+ Logger::Severity.constants.each do |level|
30
+ method_name = level.to_s.downcase
31
+ define_method method_name do |msg|
32
+ self.class.inner_logger.send(method_name, "#{prefix_str}#{msg}")
33
+ end
34
+ end
35
+
36
+ def initialize(prefix = nil, subprefix = nil)
37
+ @prefix = prefix
38
+ @subprefix = subprefix
39
+ end
40
+
41
+ class << self
42
+ attr_reader :dev, :log_level
43
+
44
+ def link_logger(dev)
45
+ @dev = dev ? dev.to_s.downcase : nil
46
+ @dev_fd = @dev
47
+
48
+ @dev_fd = STDOUT if @dev == 'stdout'
49
+ @dev_fd = STDERR if @dev == 'stderr'
50
+
51
+ @inner_logger = InnerLogger.new(@dev_fd)
52
+ @inner_logger.level = self.log_level || Logger::INFO
53
+ end
54
+
55
+ def log_level=(level)
56
+ @log_level = level
57
+ @inner_logger.level = self.log_level if @inner_logger
58
+ end
59
+
60
+ def inner_logger
61
+ @inner_logger ||= InnerLogger.new(nil)
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ def prefix_str
68
+ @pref_string ||= begin
69
+ pref_string = ''
70
+
71
+ if @prefix
72
+ pref_string = "[#{@prefix}] "
73
+ pref_string += "#{@subprefix} " if @subprefix
74
+ end
75
+
76
+ pref_string
77
+ end
78
+ end
79
+
80
+ end
data/lib/eye/notify.rb ADDED
@@ -0,0 +1,86 @@
1
+ class Eye::Notify
2
+ include Celluloid
3
+ include Eye::Logger::Helpers
4
+ extend Eye::Dsl::Validation
5
+
6
+ autoload :Mail, 'eye/notify/mail'
7
+ autoload :Jabber, 'eye/notify/jabber'
8
+
9
+ TYPES = {:mail => "Mail", :jabber => "Jabber"}
10
+
11
+ def self.get_class(type)
12
+ klass = eval("Eye::Notify::#{TYPES[type]}") rescue nil
13
+ raise "Unknown notify #{type}" unless klass
14
+ klass
15
+ end
16
+
17
+ def self.validate!(options)
18
+ get_class(options[:type]).validate(options)
19
+ end
20
+
21
+ def self.notify(contact, message_h)
22
+ settings = Eye::Control.settings
23
+ needed_hash = (settings[:contacts] || {})[contact.to_s]
24
+
25
+ return if needed_hash.blank?
26
+
27
+ create_proc = lambda do |nh|
28
+ type = nh[:type]
29
+ config = (settings[type] || {}).merge(nh[:opts] || {}).merge(:contact => nh[:contact])
30
+ klass = get_class(type)
31
+ notify = klass.new(config, message_h)
32
+ notify.async_notify if notify
33
+ end
34
+
35
+ if needed_hash.is_a?(Array)
36
+ needed_hash.each{|nh| create_proc[nh] }
37
+ else
38
+ create_proc[needed_hash]
39
+ end
40
+ end
41
+
42
+ TIMEOUT = 1.minute
43
+
44
+ def initialize(options = {}, message_h = {})
45
+ @logger = Eye::Logger.new("#{self.class.name.downcase} - #{options[:contact]}")
46
+ debug "created notifier #{options}"
47
+
48
+ @message_h = message_h
49
+ @options = options
50
+ end
51
+
52
+ def async_notify
53
+ async.notify
54
+ after(TIMEOUT){ terminate }
55
+ end
56
+
57
+ def notify
58
+ debug "start notify #{@message_h}"
59
+ execute
60
+ debug "end notify #{@message_h}"
61
+ terminate
62
+ end
63
+
64
+ def execute
65
+ raise "realize me"
66
+ end
67
+
68
+ param :contact, [String]
69
+
70
+ def message_subject
71
+ "[#{msg_host}] [#{msg_full_name}] #{msg_message}"
72
+ end
73
+
74
+ def message_body
75
+ "#{message_subject} at #{msg_at.to_s(:short)}"
76
+ end
77
+
78
+ private
79
+
80
+ %w{at host message name full_name pid level}.each do |name|
81
+ define_method("msg_#{name}") do
82
+ @message_h[name.to_sym]
83
+ end
84
+ end
85
+
86
+ end
@@ -0,0 +1,30 @@
1
+ class Eye::Notify::Jabber < Eye::Notify
2
+
3
+ # Eye.config do
4
+ # jabber :host => "some.host", :port => 12345, :user => "eye@some.host", :password => "123456"
5
+ # contact :vasya, :jabber, "vasya@some.host"
6
+ # end
7
+
8
+ param :host, String, true
9
+ param :port, [String, Fixnum], true
10
+ param :user, String, true
11
+ param :password, String
12
+
13
+ def execute
14
+ require 'xmpp4r'
15
+
16
+ debug "send jabber #{[host, port, user, password]} - #{[contact, message_body]}"
17
+
18
+ mes = ::Jabber::Message.new(contact, message_body)
19
+ mes.set_type(:normal)
20
+ mes.set_id('1')
21
+ mes.set_subject(message_subject)
22
+
23
+ client = ::Jabber::Client.new(::Jabber::JID.new("#{user}/Eye"))
24
+ client.connect(host, port)
25
+ client.auth(password)
26
+ client.send(mes)
27
+ client.close
28
+ end
29
+
30
+ end
@@ -0,0 +1,44 @@
1
+ require 'net/smtp'
2
+
3
+ class Eye::Notify::Mail < Eye::Notify
4
+
5
+ # Eye.config do
6
+ # mail :host => "some.host", :port => 12345, :user => "eye@some.host", :password => "123456", :domain => "some.host"
7
+ # contact :vasya, :mail, "vasya@some.host"
8
+ # end
9
+
10
+ param :host, String, true
11
+ param :port, [String, Fixnum], true
12
+
13
+ param :domain, String
14
+ param :user, String
15
+ param :password, String
16
+ param :auth, Symbol, nil, nil, [:plain, :login, :cram_md5]
17
+
18
+ param :from_mail, String
19
+ param :from_name, String, nil, 'eye'
20
+
21
+ def execute
22
+ smtp
23
+ end
24
+
25
+ def smtp
26
+ args = [host, port, domain, user, password, auth]
27
+ debug "called smtp with #{args}"
28
+
29
+ Net::SMTP.start(*args) do |smtp|
30
+ smtp.send_message(message, from_mail || user, contact)
31
+ end
32
+ end
33
+
34
+ def message
35
+ h = []
36
+ h << "From: #{from_name} <#{from_mail || user}>" if from_mail || user
37
+ h << "To: <#{contact}>"
38
+ h << "Subject: #{message_subject}"
39
+ h << "Date: #{msg_at.httpdate}"
40
+ h << "Message-Id: <#{rand(1000000000).to_s(36)}.#{$$}.#{contact}>"
41
+ "#{h * "\n"}\n#{message_body}"
42
+ end
43
+
44
+ end
@@ -0,0 +1,86 @@
1
+ require 'celluloid'
2
+
3
+ class Eye::Process
4
+ include Celluloid
5
+
6
+ autoload :Config, 'eye/process/config'
7
+ autoload :Commands, 'eye/process/commands'
8
+ autoload :Data, 'eye/process/data'
9
+ autoload :Watchers, 'eye/process/watchers'
10
+ autoload :Monitor, 'eye/process/monitor'
11
+ autoload :System, 'eye/process/system'
12
+ autoload :Controller, 'eye/process/controller'
13
+ autoload :StatesHistory, 'eye/process/states_history'
14
+ autoload :Child, 'eye/process/child'
15
+ autoload :Trigger, 'eye/process/trigger'
16
+ autoload :Notify, 'eye/process/notify'
17
+ autoload :Scheduler, 'eye/process/scheduler'
18
+ autoload :Validate, 'eye/process/validate'
19
+
20
+ attr_accessor :pid, :watchers, :config, :states_history,
21
+ :childs, :triggers, :name, :state_reason
22
+
23
+ def initialize(config)
24
+ raise 'pid file should be' unless config[:pid_file]
25
+
26
+ @config = prepare_config(config)
27
+ @logger = Eye::Logger.new(full_name)
28
+
29
+ @watchers = {}
30
+ @childs = {}
31
+ @triggers = []
32
+ @name = @config[:name]
33
+
34
+ @states_history = Eye::Process::StatesHistory.new(100)
35
+ @states_history << :unmonitored
36
+
37
+ debug "create with config: #{@config.inspect}"
38
+
39
+ add_triggers
40
+
41
+ super() # for statemachine
42
+ end
43
+
44
+ # c(), self[]
45
+ include Eye::Process::Config
46
+
47
+ # full_name, status_data
48
+ include Eye::Process::Data
49
+
50
+ # commands:
51
+ # start_process, stop_process, restart_process
52
+ include Eye::Process::Commands
53
+
54
+ # start, stop, restart, monitor, unmonit, delete
55
+ include Eye::Process::Controller
56
+
57
+ # add_watchers, remove_watchers:
58
+ include Eye::Process::Watchers
59
+
60
+ # check alive, crash methods:
61
+ include Eye::Process::Monitor
62
+
63
+ # system methods:
64
+ include Eye::Process::System
65
+
66
+ # manage childs methods
67
+ include Eye::Process::Child
68
+
69
+ # manage triggers methods
70
+ include Eye::Process::Trigger
71
+
72
+ # manage notify methods
73
+ include Eye::Process::Notify
74
+
75
+ # logger methods
76
+ include Eye::Logger::Helpers
77
+
78
+ # scheduler
79
+ include Eye::Process::Scheduler
80
+
81
+ # validate
82
+ extend Eye::Process::Validate
83
+ end
84
+
85
+ # include state_machine states
86
+ require_relative 'process/states'
@@ -0,0 +1,58 @@
1
+ module Eye::Process::Child
2
+
3
+ def add_childs
4
+ add_or_update_childs
5
+ end
6
+
7
+ def add_or_update_childs
8
+ return unless self[:monitor_children]
9
+
10
+ return unless self.up?
11
+
12
+ unless self.pid
13
+ warn 'Cant add childs, because no pid'
14
+ return
15
+ end
16
+
17
+ now_childs = Eye::SystemResources.childs(self.pid)
18
+ new_childs = []
19
+ exist_childs = []
20
+
21
+ now_childs.each do |child_pid|
22
+ if self.childs[child_pid]
23
+ exist_childs << child_pid
24
+ else
25
+ new_childs << child_pid
26
+ end
27
+ end
28
+
29
+ removed_childs = self.childs.keys - now_childs
30
+
31
+ if new_childs.present?
32
+ new_childs.each do |child_pid|
33
+ self.childs[child_pid] = Eye::ChildProcess.new(child_pid, self[:monitor_children], logger.prefix)
34
+ end
35
+ end
36
+
37
+ if removed_childs.present?
38
+ removed_childs.each{|child_pid| remove_child(child_pid) }
39
+ end
40
+
41
+ h = {:new => new_childs.size, :removed => removed_childs.size, :exists => exist_childs.size }
42
+ debug "childs info: #{ h.inspect }"
43
+
44
+ h
45
+ end
46
+
47
+ def remove_childs
48
+ if childs.present?
49
+ childs.keys.each{|child_pid| remove_child(child_pid) }
50
+ end
51
+ end
52
+
53
+ def remove_child(child_pid)
54
+ child = self.childs.delete(child_pid)
55
+ child.destroy if child && child.alive?
56
+ end
57
+
58
+ end
@@ -0,0 +1,256 @@
1
+ module Eye::Process::Commands
2
+
3
+ def start_process
4
+ debug 'start_process command'
5
+
6
+ switch :starting
7
+
8
+ unless self[:start_command]
9
+ warn 'no start command, so unmonitoring'
10
+ switch :unmonitoring
11
+ return :no_start_command
12
+ end
13
+
14
+ result = self[:daemonize] ? daemonize_process : execute_process
15
+
16
+ if !result[:error]
17
+ debug "process (#{self.pid}) ok started"
18
+ switch :started
19
+ else
20
+ error "process (#{self.pid}) failed to start (#{result[:error].inspect})"
21
+ if process_realy_running?
22
+ warn "kill, what remains from process (#{self.pid}), because its failed to start (without pid_file impossible to monitoring)"
23
+ send_signal(:KILL)
24
+ sleep 0.2 # little grace
25
+ end
26
+
27
+ self.pid = nil
28
+ switch :crashed
29
+ end
30
+
31
+ result
32
+
33
+ rescue StateMachine::InvalidTransition => e
34
+ warn "wrong switch '#{e.message}'"
35
+
36
+ :state_error
37
+ end
38
+
39
+ def stop_process
40
+ debug 'stop_process command'
41
+
42
+ switch :stopping
43
+
44
+ kill_process
45
+
46
+ if process_realy_running?
47
+ warn 'NOT STOPPED, check command/signals, or tune stop_timeout/stop_grace, seems it was really soft'
48
+
49
+ switch :unmonitoring
50
+ nil
51
+
52
+ else
53
+ switch :stopped
54
+
55
+ if control_pid?
56
+ info "delete pid_file: #{self[:pid_file_ex]}"
57
+ clear_pid_file
58
+ end
59
+
60
+ true
61
+
62
+ end
63
+
64
+ rescue StateMachine::InvalidTransition => e
65
+ warn "wrong switch '#{e.message}'"
66
+ nil
67
+ end
68
+
69
+ def restart_process
70
+ debug 'restart_process command'
71
+
72
+ switch :restarting
73
+
74
+ if self[:restart_command]
75
+ execute_restart_command
76
+ sleep self[:restart_timeout].to_f
77
+ result = check_alive_with_refresh_pid_if_needed
78
+ switch(result ? :restarted : :crashed)
79
+ else
80
+ stop_process
81
+ start_process
82
+ end
83
+
84
+ true
85
+
86
+ rescue StateMachine::InvalidTransition => e
87
+ warn "wrong switch '#{e.message}'"
88
+ nil
89
+ end
90
+
91
+ private
92
+
93
+ def kill_process
94
+ return unless self.pid
95
+
96
+ if self[:stop_command]
97
+ cmd = prepare_command(self[:stop_command])
98
+ res = execute(cmd, config.merge(:timeout => self[:stop_timeout]))
99
+ info "executing: `#{cmd}` with stop_timeout: #{self[:stop_timeout].to_f}s and stop_grace: #{self[:stop_grace].to_f}s"
100
+
101
+ if res[:error]
102
+ error "raised with #{res[:error].inspect}"
103
+
104
+ if res[:error].class == Timeout::Error
105
+ error 'you should tune stop_timeout setting'
106
+ end
107
+ end
108
+
109
+ sleep self[:stop_grace].to_f
110
+
111
+ elsif self[:stop_signals]
112
+ info "executing stop_signals #{self[:stop_signals].inspect}"
113
+ stop_signals = self[:stop_signals].clone
114
+
115
+ signal = stop_signals.shift
116
+ send_signal(signal)
117
+
118
+ while stop_signals.present?
119
+ delay = stop_signals.shift
120
+ signal = stop_signals.shift
121
+
122
+ if wait_for_condition(delay.to_f, 0.1){ !process_realy_running? }
123
+ info 'has terminated'
124
+ break
125
+ end
126
+
127
+ send_signal(signal)
128
+ end
129
+
130
+ sleep self[:stop_grace].to_f
131
+
132
+ else # default command
133
+ info "executing: `kill -TERM #{self.pid}` with stop_grace: #{self[:stop_grace].to_f}s"
134
+ send_signal(:TERM)
135
+
136
+ sleep self[:stop_grace].to_f
137
+
138
+ # if process not die here, by default we force kill it
139
+ if process_realy_running?
140
+ warn "process not die after TERM and stop_grace #{self[:stop_grace].to_f}s, so send KILL"
141
+ send_signal(:KILL)
142
+ sleep 0.1 # little grace
143
+ end
144
+ end
145
+ end
146
+
147
+ def execute_restart_command
148
+ cmd = prepare_command(self[:restart_command])
149
+ info "executing: `#{cmd}` with restart_timeout: #{self[:restart_timeout].to_f}s and restart_grace: #{self[:restart_grace].to_f}s"
150
+
151
+ res = execute(cmd, config.merge(:timeout => self[:restart_timeout]))
152
+
153
+ if res[:error]
154
+ error "restart raised with #{res[:error].inspect}"
155
+
156
+ if res[:error].class == Timeout::Error
157
+ error 'you should tune restart_timeout setting'
158
+ end
159
+ end
160
+
161
+ res
162
+ end
163
+
164
+ def daemonize_process
165
+ time_before = Time.now
166
+ res = Eye::System.daemonize(self[:start_command], config)
167
+ start_time = Time.now - time_before
168
+
169
+ info "daemonizing: `#{self[:start_command]}` with start_grace: #{self[:start_grace].to_f}s, env: #{self[:environment].inspect}, working_dir: #{self[:working_dir]} (pid:#{res[:pid]})"
170
+
171
+ if res[:error]
172
+ error "raised with #{res[:error].inspect}"
173
+
174
+ if res[:error].message == 'Permission denied - open'
175
+ error 'seems stdout/err/all files is not writable'
176
+ end
177
+
178
+ return {:error => res[:error].inspect}
179
+ end
180
+
181
+ self.pid = res[:pid]
182
+
183
+ unless self.pid
184
+ error 'returned empty pid, WTF O_o'
185
+ return {:error => :empty_pid}
186
+ end
187
+
188
+ sleep self[:start_grace].to_f
189
+
190
+ unless process_realy_running?
191
+ error "process with pid(#{self.pid}) not found, may be crashed (#{check_logs_str})"
192
+ return {:error => :not_realy_running}
193
+ end
194
+
195
+ unless failsafe_save_pid
196
+ return {:error => :cant_write_pid}
197
+ end
198
+
199
+ res
200
+ end
201
+
202
+ def execute_process
203
+ info "executing: `#{self[:start_command]}` with start_timeout: #{config[:start_timeout].to_f}s, start_grace: #{self[:start_grace].to_f}s, env: #{self[:environment].inspect}, working_dir: #{self[:working_dir]}"
204
+ time_before = Time.now
205
+
206
+ res = execute(self[:start_command], config.merge(:timeout => config[:start_timeout]))
207
+ start_time = Time.now - time_before
208
+
209
+ if res[:error]
210
+ error "raised with #{res[:error].inspect}"
211
+
212
+ if res[:error].message == 'Permission denied - open'
213
+ error 'seems stdout/err/all files is not writable'
214
+ end
215
+
216
+ if res[:error].class == Timeout::Error
217
+ error "try to increase start_timeout interval (current #{self[:start_timeout]} seems too small, for process self-daemonization)"
218
+ end
219
+
220
+ return {:error => res[:error].inspect}
221
+ end
222
+
223
+ sleep self[:start_grace].to_f
224
+
225
+ unless set_pid_from_file
226
+ error "pid_file(#{self[:pid_file_ex]}) does not appears after start_grace #{self[:start_grace].to_f}, check start_command, or tune start_grace (eye dont know what to monitor without pid)"
227
+ return {:error => :pid_not_found}
228
+ end
229
+
230
+ unless process_realy_running?
231
+ error "process in pid_file(#{self[:pid_file_ex]})(#{self.pid}) not found, maybe process do not write there actual pid, or just crashed (#{check_logs_str})"
232
+ return {:error => :not_realy_running}
233
+ end
234
+
235
+ res[:pid] = self.pid
236
+ info "process get pid:#{res[:pid]}, pid_file #{self[:pid_file_ex]}"
237
+ res
238
+ end
239
+
240
+ def check_logs_str
241
+ if !self[:stdout] && !self[:stderr]
242
+ 'maybe should add stdout/err/all logs'
243
+ else
244
+ "check also it stdout/err/all logs #{[self[:stdout], self[:stderr]].inspect}"
245
+ end
246
+ end
247
+
248
+ def prepare_command(command)
249
+ if self.pid
250
+ command.to_s.gsub('{{PID}}', self.pid.to_s).gsub('{PID}', self.pid.to_s)
251
+ else
252
+ command
253
+ end
254
+ end
255
+
256
+ end