Class: Freyr::Service

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/freyr/service.rb

Defined Under Namespace

Classes: MissingDependency, NoProcMatch

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ Service

Returns a new instance of Service.



14
15
16
17
18
19
20
# File 'lib/freyr/service.rb', line 14

def initialize(s)
  @info = s
  @command = Command.new(self)
  Service.by_selector[name] = ServiceGroup.new(self)
  Service.s[name] = self
  Service.by_name[name] = self
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



10
11
12
# File 'lib/freyr/service.rb', line 10

def command
  @command
end

#defined_in_pathsObject (readonly)

Returns the value of attribute defined_in_paths.



10
11
12
# File 'lib/freyr/service.rb', line 10

def defined_in_paths
  @defined_in_paths
end

#infoObject (readonly)

Returns the value of attribute info.



10
11
12
# File 'lib/freyr/service.rb', line 10

def info
  @info
end

Class Method Details

.[](name) ⇒ Object



153
154
155
# File 'lib/freyr/service.rb', line 153

def [](name)
  by_selector.has_key?(name) ? by_selector[name] : nil
end

.add_file(f) ⇒ Object



147
148
149
150
151
# File 'lib/freyr/service.rb', line 147

def add_file f
  ServiceInfo.from_file(f)

  @all_services
end

.add_service_method(*methods) ⇒ Object



5
6
7
# File 'lib/freyr/service.rb', line 5

def add_service_method *methods
  
end

.by_dirObject



133
134
135
# File 'lib/freyr/service.rb', line 133

def by_dir
  @by_dir ||= {}
end

.by_nameObject

Get by name and alias



138
139
140
# File 'lib/freyr/service.rb', line 138

def by_name
  @by_name ||= {}
end

.by_selectorObject

by group/alias/name and always a servicegroup



143
144
145
# File 'lib/freyr/service.rb', line 143

def by_selector
  @by_selector ||= Hash.new {|h,k| h[k] = ServiceGroup.new}
end

.sObject

Get by name only



125
126
127
# File 'lib/freyr/service.rb', line 125

def s
  @all_services ||= {}
end

.selectorsObject



129
130
131
# File 'lib/freyr/service.rb', line 129

def selectors
  by_selector.keys
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/freyr/service.rb', line 46

def alive?
  pid_file.alive?
end

#call_graphObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/freyr/service.rb', line 50

def call_graph
  graph = Hash.new {|h,k| h[k]=[]}
  graph[self]
  @info.dependencies.each do |dep|
    if d = Service.s[dep]
      graph[self] << d
      graph.merge!(d.call_graph)
    end
  end
  graph
end

#dependencies(yell = false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/freyr/service.rb', line 62

def dependencies(yell = false)
  missing = []
  deps = @info.dependencies.inject([]) do |all,dep|
    if d = Service.s[dep]
      all | (d.dependencies(yell) + [d])
    else
      missing << dep
      all
    end
  end

  if yell && !missing.empty?
    raise MissingDependency, "missing #{missing.join(', ')} dependencies"
  end

  deps.compact
end

#describeObject



106
107
108
# File 'lib/freyr/service.rb', line 106

def describe
  %Q{#{@info.name}(#{@info.groups.join(',')}) - #{@info.start}}
end

#error(*args, &blk) ⇒ Object



101
102
103
104
# File 'lib/freyr/service.rb', line 101

def error *args, &blk
  Freyr.logger.error(*args,&blk)
  Freyr.logger.debug("service info for service with error #{self}") {@info.inspect}
end

#inspectObject



117
118
119
# File 'lib/freyr/service.rb', line 117

def inspect
  %Q{#<Freyr::Service #{@info.name} #{@info.start.inspect}>}
end

#is_group?(name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/freyr/service.rb', line 42

def is_group?(name)
  @info.groups.find {|g| g.to_s == name.to_s}
end

#matches?(n) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
# File 'lib/freyr/service.rb', line 110

def matches?(n)
  n = n.to_s
  return true if @info.name.to_s == n
  
  @info.also.find {|a| a.to_s == n}
end

#pid_fileObject



23
24
25
26
27
28
# File 'lib/freyr/service.rb', line 23

def pid_file
  @pid_file ||= begin
    raise NoProcMatch, "please provide proc_match for service #{@info.name}" unless @info.proc_match
    PidFile.new(@info.pid_file,@info.proc_match)
  end
end

#ping!Object



80
81
82
83
84
85
86
# File 'lib/freyr/service.rb', line 80

def ping!
  if @info.ping
    pinger = Pinger.new(self)
    pinger.ping
    pinger
  end
end

#restart!Object



38
39
40
# File 'lib/freyr/service.rb', line 38

def restart!
  command.restart!
end

#start!Object



30
31
32
# File 'lib/freyr/service.rb', line 30

def start!
  command.run! unless alive?
end

#stop!Object



34
35
36
# File 'lib/freyr/service.rb', line 34

def stop!
  command.kill!
end

#tail!(size = 600, follow = true) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/freyr/service.rb', line 88

def tail!(size = 600, follow = true)
  f = follow ? 'f' : ''
  log_location = @info.log
  if File.exist?(log_location)
    cmd = "tail -#{size}#{f} #{log_location}"
    Freyr.logger.debug("tailing cmd") {cmd.inspect}
    exec(cmd)
  else
    error("no logfile found at #{log_location}")
    abort("no logfile found at #{log_location}")
  end
end