Class: ServiceInfo::Base

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

Overview

Represents information about service on any host.

Direct Known Subclasses

Logger, Svlogd

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a_name) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
# File 'lib/runit-man/service_info/base.rb', line 9

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.



7
8
9
# File 'lib/runit-man/service_info/base.rb', line 7

def name
  @name
end

Class Method Details

.[](name) ⇒ Object



263
264
265
# File 'lib/runit-man/service_info/base.rb', line 263

def [](name)
  all_service_names.include?(name) ? ServiceInfo.klass.new(name) : nil
end

.allObject



257
258
259
260
261
# File 'lib/runit-man/service_info/base.rb', line 257

def all
  all_service_names.sort.map do |name|
    ServiceInfo.klass.new(name)
  end
end

.itself_or_parent?(name) ⇒ Boolean

Returns:

  • (Boolean)


280
281
282
# File 'lib/runit-man/service_info/base.rb', line 280

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

.real_data_from_file(file_name) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/runit-man/service_info/base.rb', line 267

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)


48
49
50
# File 'lib/runit-man/service_info/base.rb', line 48

def active?
  File.directory?(active_service_folder) || File.symlink?(active_service_folder)
end

#all_log_file_locationsObject

All log file locations



108
109
110
# File 'lib/runit-man/service_info/base.rb', line 108

def all_log_file_locations
  []
end

#allowed_signalsObject



164
165
166
167
168
169
170
# File 'lib/runit-man/service_info/base.rb', line 164

def allowed_signals
  return []  unless File.directory?(allowed_signals_folder)

  Dir.entries(allowed_signals_folder).reject do |name|
    ServiceInfo::Base.itself_or_parent?(name)
  end
end

#down!Object



68
69
70
# File 'lib/runit-man/service_info/base.rb', line 68

def down!
  send_signal :d
end

#down?Boolean

Returns:

  • (Boolean)


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

def down?
  @status.down?
end

#files_to_viewObject



144
145
146
# File 'lib/runit-man/service_info/base.rb', line 144

def files_to_view
  symlinks_to_file_paths(files_to_view_folder)
end

#files_to_watchObject



148
149
150
# File 'lib/runit-man/service_info/base.rb', line 148

def files_to_watch
  symlinks_to_file_paths(files_to_watch_folder)
end

#log_file_locationsObject

Current log file locations



103
104
105
# File 'lib/runit-man/service_info/base.rb', line 103

def log_file_locations
  []
end

#log_pidObject



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

def log_pid
  @log_status.pid
end

#logged?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/runit-man/service_info/base.rb', line 40

def logged?
  File.directory?(log_supervise_folder)
end

#pidObject



90
91
92
# File 'lib/runit-man/service_info/base.rb', line 90

def pid
  @status.pid
end

#restart!Object



81
82
83
84
# File 'lib/runit-man/service_info/base.rb', line 81

def restart!
  down!
  up!
end

#run?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/runit-man/service_info/base.rb', line 60

def run?
  @status.run?
end

#send_signal(signal) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/runit-man/service_info/base.rb', line 121

def send_signal(signal)
  return  unless supervise?

  File.open(File.join(supervise_folder, 'control'), 'w') do |f|
    f.print signal.to_s
  end
end

#started_atObject



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

def started_at
  @status.started_at
end

#statObject



44
45
46
# File 'lib/runit-man/service_info/base.rb', line 44

def stat
  @status.to_s
end

#switch_down!Object



72
73
74
75
# File 'lib/runit-man/service_info/base.rb', line 72

def switch_down!
  down!
  File.unlink(active_service_folder)
end

#switch_up!Object



77
78
79
# File 'lib/runit-man/service_info/base.rb', line 77

def switch_up!
  File.symlink(inactive_service_folder, active_service_folder)
end

#switchable?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/runit-man/service_info/base.rb', line 52

def switchable?
  File.symlink?(active_service_folder) || File.directory?(inactive_service_folder)
end


129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/runit-man/service_info/base.rb', line 129

def symlinks_to_file_paths(dir_path)
  return []  unless File.directory?(dir_path)

  Dir.entries(dir_path).select do |name|
    File.symlink?(File.join(dir_path, name))
  end.map do |name|
    File.expand_path(
      File.readlink(File.join(dir_path, name)),
      dir_path
    )
  end.select do |file_path|
    File.file?(file_path)
  end
end

#to_hashObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/runit-man/service_info/base.rb', line 17

def to_hash
  data = {}
  [
    :name, :stat, :active?, :logged?, :switchable?,
    :log_file_locations, :log_pid, :watched_modified_files
  ].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



36
37
38
# File 'lib/runit-man/service_info/base.rb', line 36

def to_json(*args)
  Yajl::Encoder.encode(to_hash, *args)
end

#up!Object



64
65
66
# File 'lib/runit-man/service_info/base.rb', line 64

def up!
  send_signal :u
end

#uptimeObject



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

def uptime
  @status.uptime
end

#urls_to_viewObject



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/runit-man/service_info/base.rb', line 152

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

#watched_modified_filesObject



112
113
114
115
116
117
118
119
# File 'lib/runit-man/service_info/base.rb', line 112

def watched_modified_files
  files_to_watch.map do |filepath|
    mtime = File.stat(filepath).mtime.utc
    { :path => filepath, :mtime => mtime }
  end.select do |rec|
    rec[:mtime] > @status.started_at
  end
end