Class: ServiceInfo
- Inherits:
-
Object
- Object
- ServiceInfo
- Defined in:
- lib/runit-man/service_info.rb
Constant Summary collapse
- SPECIAL_LOG_FILES =
%w(lock config state newstate).freeze
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .[](name) ⇒ Object
- .all ⇒ Object
- .itself_or_parent?(name) ⇒ Boolean
- .log_location_cache ⇒ Object
- .real_data_from_file(file_name) ⇒ Object
Instance Method Summary collapse
- #active? ⇒ Boolean
- #allowed_signals ⇒ Object
- #down! ⇒ Object
- #down? ⇒ Boolean
- #files_to_view ⇒ Object
-
#initialize(a_name) ⇒ ServiceInfo
constructor
A new instance of ServiceInfo.
- #log_file_location ⇒ Object
- #log_file_path(file_name) ⇒ Object
- #log_files ⇒ Object
- #log_pid ⇒ Object
- #logged? ⇒ Boolean
- #logger_log_file_path(file_name) ⇒ Object
- #logger_log_files ⇒ Object
- #pid ⇒ Object
- #restart! ⇒ Object
- #run? ⇒ Boolean
- #send_signal(signal) ⇒ Object
- #sorted_log_files(log_files) ⇒ Object
- #started_at ⇒ Object
- #stat ⇒ Object
- #svlogd_log_file_path(file_name) ⇒ Object
- #svlogd_log_files ⇒ Object
- #switch_down! ⇒ Object
- #switch_up! ⇒ Object
- #switchable? ⇒ Boolean
- #to_hash ⇒ Object
- #to_json(*args) ⇒ Object
- #up! ⇒ Object
- #uptime ⇒ Object
- #urls_to_view ⇒ Object
Constructor Details
#initialize(a_name) ⇒ ServiceInfo
Returns a new instance of ServiceInfo.
10 11 12 13 14 15 16 |
# File 'lib/runit-man/service_info.rb', line 10 def initialize(a_name) @name = a_name @files = {} @status = ServiceStatus.new(data_from_file(File.join(supervise_folder, 'status'))) @log_status = ServiceStatus.new(data_from_file(File.join(log_supervise_folder, 'status'))) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/runit-man/service_info.rb', line 8 def name @name end |
Class Method Details
.[](name) ⇒ Object
284 285 286 |
# File 'lib/runit-man/service_info.rb', line 284 def [](name) all_service_names.include?(name) ? ServiceInfo.new(name) : nil end |
.all ⇒ Object
278 279 280 281 282 |
# File 'lib/runit-man/service_info.rb', line 278 def all all_service_names.sort.map do |name| ServiceInfo.new(name) end end |
.itself_or_parent?(name) ⇒ Boolean
303 304 305 |
# File 'lib/runit-man/service_info.rb', line 303 def itself_or_parent?(name) name == '.' || name == '..' end |
.log_location_cache ⇒ Object
288 289 290 |
# File 'lib/runit-man/service_info.rb', line 288 def log_location_cache @log_location_cache ||= LogLocationCache.new(RunitMan.logger) end |
.real_data_from_file(file_name) ⇒ Object
292 293 294 295 296 297 298 299 300 301 |
# File 'lib/runit-man/service_info.rb', line 292 def real_data_from_file(file_name) return nil unless File.readable?(file_name) if RUBY_VERSION >= '1.9' data = IO.read(file_name, :external_encoding => 'ASCII-8BIT') else data = IO.read(file_name) end data.chomp! unless data.nil? data.empty? ? nil : data end |
Instance Method Details
#active? ⇒ Boolean
49 50 51 |
# File 'lib/runit-man/service_info.rb', line 49 def active? File.directory?(active_service_folder) || File.symlink?(active_service_folder) end |
#allowed_signals ⇒ Object
228 229 230 231 232 233 |
# File 'lib/runit-man/service_info.rb', line 228 def allowed_signals return [] unless File.directory?(allowed_signals_folder) Dir.entries(allowed_signals_folder).reject do |name| ServiceInfo.itself_or_parent?(name) end end |
#down! ⇒ Object
69 70 71 |
# File 'lib/runit-man/service_info.rb', line 69 def down! send_signal :d end |
#down? ⇒ Boolean
57 58 59 |
# File 'lib/runit-man/service_info.rb', line 57 def down? @status.down? end |
#files_to_view ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/runit-man/service_info.rb', line 203 def files_to_view return [] unless File.directory?(files_to_view_folder) Dir.entries(files_to_view_folder).select do |name| File.symlink?(File.join(files_to_view_folder, name)) end.map do |name| File.( File.readlink(File.join(files_to_view_folder, name)), files_to_view_folder ) end.select do |file_path| File.file?(file_path) end end |
#log_file_location ⇒ Object
103 104 105 106 107 |
# File 'lib/runit-man/service_info.rb', line 103 def log_file_location rel_path = ServiceInfo.log_location_cache[log_pid] return nil if rel_path.nil? File.(rel_path, log_run_folder) end |
#log_file_path(file_name) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/runit-man/service_info.rb', line 122 def log_file_path(file_name) case RunitMan.logger when RunitMan::DEFAULT_LOGGER then svlogd_log_file_path(file_name) when /^logger(?:\:.+)?/ then logger_log_file_path(file_name) else nil end end |
#log_files ⇒ Object
187 188 189 190 191 192 193 |
# File 'lib/runit-man/service_info.rb', line 187 def log_files case RunitMan.logger when RunitMan::DEFAULT_LOGGER then svlogd_log_files when /^logger(?:\:.+)?/ then logger_log_files else [] end end |
#log_pid ⇒ Object
99 100 101 |
# File 'lib/runit-man/service_info.rb', line 99 def log_pid @log_status.pid end |
#logged? ⇒ Boolean
41 42 43 |
# File 'lib/runit-man/service_info.rb', line 41 def logged? File.directory?(log_supervise_folder) end |
#logger_log_file_path(file_name) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/runit-man/service_info.rb', line 114 def logger_log_file_path(file_name) dir_name = File.dirname(File.dirname(log_file_location)) loc = File.(File.join(file_name, "#{name}.log"), dir_name) loc = "#{loc}.gz" unless File.exists?(loc) loc = nil unless File.exists?(loc) loc end |
#logger_log_files ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/runit-man/service_info.rb', line 159 def logger_log_files r = [] dir_name = File.dirname(File.dirname(log_file_location)) Dir.foreach(dir_name) do |name| next if ServiceInfo.itself_or_parent?(name) full_name = File.(name, dir_name) next unless File.directory?(full_name) file_name = File.join(full_name, "#{self.name}.log") label = "#{Utils.host_name}-#{self.name}-#{name}.log" unless File.exists?(file_name) file_name = "#{file_name}.gz" label = "#{label}.gz" end stats = File.stat(file_name) stat_times = [stats.ctime.utc, stats.atime.utc, stats.mtime.utc] min_time, max_time = stat_times.min, stat_times.max r << { :name => name, :label => label, :size => stats.size, :created => min_time, :modified => max_time } end sorted_log_files(r) end |
#pid ⇒ Object
91 92 93 |
# File 'lib/runit-man/service_info.rb', line 91 def pid @status.pid end |
#restart! ⇒ Object
82 83 84 85 |
# File 'lib/runit-man/service_info.rb', line 82 def restart! down! up! end |
#run? ⇒ Boolean
61 62 63 |
# File 'lib/runit-man/service_info.rb', line 61 def run? @status.run? end |
#send_signal(signal) ⇒ Object
196 197 198 199 200 201 |
# File 'lib/runit-man/service_info.rb', line 196 def send_signal(signal) return unless supervise? File.open(File.join(supervise_folder, 'control'), 'w') do |f| f.print signal.to_s end end |
#sorted_log_files(log_files) ⇒ Object
130 131 132 133 |
# File 'lib/runit-man/service_info.rb', line 130 def sorted_log_files(log_files) return log_files if log_files.length < 2 log_files.sort { |a, b| a[:created] <=> b[:created] } end |
#started_at ⇒ Object
87 88 89 |
# File 'lib/runit-man/service_info.rb', line 87 def started_at @status.started_at end |
#stat ⇒ Object
45 46 47 |
# File 'lib/runit-man/service_info.rb', line 45 def stat @status.to_s end |
#svlogd_log_file_path(file_name) ⇒ Object
109 110 111 112 |
# File 'lib/runit-man/service_info.rb', line 109 def svlogd_log_file_path(file_name) dir_name = File.dirname(log_file_location) File.(file_name, dir_name) end |
#svlogd_log_files ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/runit-man/service_info.rb', line 135 def svlogd_log_files r = [] dir_name = File.dirname(log_file_location) Dir.foreach(dir_name) do |name| next if ServiceInfo.itself_or_parent?(name) next if SPECIAL_LOG_FILES.include?(name) full_name = File.(name, dir_name) stats = File.stat(full_name) stat_times = [stats.ctime.utc, stats.atime.utc, stats.mtime.utc] min_time, max_time = stat_times.min, stat_times.max label = "#{Utils.host_name}-#{self.name}-#{I18n.l(min_time)}-#{I18n.l(max_time)}.log" label = label.gsub(/[\:\s\,]/, '-').gsub(/[\\\/]/, '.') r << { :name => name, :label => label, :size => stats.size, :created => min_time, :modified => max_time } end sorted_log_files(r) end |
#switch_down! ⇒ Object
73 74 75 76 |
# File 'lib/runit-man/service_info.rb', line 73 def switch_down! down! File.unlink(active_service_folder) end |
#switch_up! ⇒ Object
78 79 80 |
# File 'lib/runit-man/service_info.rb', line 78 def switch_up! File.symlink(inactive_service_folder, active_service_folder) end |
#switchable? ⇒ Boolean
53 54 55 |
# File 'lib/runit-man/service_info.rb', line 53 def switchable? File.symlink?(active_service_folder) || File.directory?(inactive_service_folder) end |
#to_hash ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/runit-man/service_info.rb', line 18 def to_hash data = {} [ :name, :stat, :active?, :logged?, :switchable?, :log_file_location, :log_pid ].each do |sym| data[sym] = send(sym) end [ :run?, :pid, :finish?, :down?, :want_up?, :want_down?, :got_term?, :started_at, :uptime ].each do |sym| data[sym] = @status.send(sym) end data end |
#to_json(*args) ⇒ Object
37 38 39 |
# File 'lib/runit-man/service_info.rb', line 37 def to_json(*args) Yajl::Encoder.encode(to_hash, *args) end |
#up! ⇒ Object
65 66 67 |
# File 'lib/runit-man/service_info.rb', line 65 def up! send_signal :u end |
#uptime ⇒ Object
95 96 97 |
# File 'lib/runit-man/service_info.rb', line 95 def uptime @status.uptime end |
#urls_to_view ⇒ Object
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/runit-man/service_info.rb', line 217 def urls_to_view return [] unless File.directory?(urls_to_view_folder) Dir.entries(urls_to_view_folder).select do |name| name =~ /\.url$/ && File.file?(File.join(urls_to_view_folder, name)) end.map do |name| data_from_file(File.join(urls_to_view_folder, name)) end.select do |url| !url.nil? end end |