Class: ServiceInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/runit-man/service_info.rb

Constant Summary collapse

SPECIAL_LOG_FILES =
%w(lock config state newstate).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#nameObject (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

.allObject



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

Returns:

  • (Boolean)


303
304
305
# File 'lib/runit-man/service_info.rb', line 303

def itself_or_parent?(name)
  name == '.' || name == '..'
end

.log_location_cacheObject



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

Returns:

  • (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_signalsObject



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

Returns:

  • (Boolean)


57
58
59
# File 'lib/runit-man/service_info.rb', line 57

def down?
  @status.down?
end

#files_to_viewObject



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.expand_path(
      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_locationObject



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.expand_path(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_filesObject



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_pidObject



99
100
101
# File 'lib/runit-man/service_info.rb', line 99

def log_pid
  @log_status.pid
end

#logged?Boolean

Returns:

  • (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.expand_path(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_filesObject



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.expand_path(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

#pidObject



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

Returns:

  • (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_atObject



87
88
89
# File 'lib/runit-man/service_info.rb', line 87

def started_at
  @status.started_at
end

#statObject



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.expand_path(file_name, dir_name)
end

#svlogd_log_filesObject



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.expand_path(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

Returns:

  • (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_hashObject



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

#uptimeObject



95
96
97
# File 'lib/runit-man/service_info.rb', line 95

def uptime
  @status.uptime
end

#urls_to_viewObject



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