Class: Freyr::Service
- Inherits:
-
Object
- Object
- Freyr::Service
- Extended by:
- Forwardable
- Defined in:
- lib/freyr/service.rb
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#service_info ⇒ Object
readonly
Returns the value of attribute service_info.
Class Method Summary collapse
- .[](name) ⇒ Object
- .add_file(f) ⇒ Object
- .add_service_method(*methods) ⇒ Object
- .alive?(name) ⇒ Boolean
- .groups ⇒ Object
- .names ⇒ Object
- .s ⇒ Object
- .selectors ⇒ Object
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #describe ⇒ Object
- #env ⇒ Object
- #error(*args, &blk) ⇒ Object
-
#initialize(s) ⇒ Service
constructor
A new instance of Service.
- #inspect ⇒ Object
- #is_group?(name) ⇒ Boolean
- #log ⇒ Object
- #matches?(n) ⇒ Boolean
- #ping! ⇒ Object
- #read_log ⇒ Object
- #restart! ⇒ Object
- #start! ⇒ Object
- #start_command ⇒ Object
- #stop! ⇒ Object
- #tail!(size = 600, follow = true) ⇒ Object
- #write_log? ⇒ Boolean
Constructor Details
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
10 11 12 |
# File 'lib/freyr/service.rb', line 10 def command @command end |
#service_info ⇒ Object (readonly)
Returns the value of attribute service_info.
10 11 12 |
# File 'lib/freyr/service.rb', line 10 def service_info @service_info end |
Class Method Details
.[](name) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/freyr/service.rb', line 140 def [](name) group = ServiceGroup.new if ser = s.find {|sr| sr.matches?(name)} group << ser else s.each do |sr| if sr.is_group?(name) group << sr end end end group.empty? ? nil : group end |
.add_file(f) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/freyr/service.rb', line 118 def add_file f s Freyr.logger.debug('adding file') {f} services = ServiceInfo.from_file(f).collect do |ser| raise 'Cannot have two things of the same name' if selectors.include?(ser.name) names |= [ser.name] @all_groups |= ser.groups Freyr.logger.debug('adding service') {ser.name.inspect} new(ser) end @all_services += services end |
.add_service_method(*methods) ⇒ Object
5 6 7 |
# File 'lib/freyr/service.rb', line 5 def add_service_method *methods def_delegators :@service_info, *methods end |
.alive?(name) ⇒ Boolean
134 135 136 137 138 |
# File 'lib/freyr/service.rb', line 134 def alive?(name) !!self[name].find do |ser| ser.alive? end end |
.groups ⇒ Object
110 111 112 |
# File 'lib/freyr/service.rb', line 110 def groups @all_groups ||= [] end |
.names ⇒ Object
106 107 108 |
# File 'lib/freyr/service.rb', line 106 def names @all_names ||= [] end |
.s ⇒ Object
102 103 104 |
# File 'lib/freyr/service.rb', line 102 def s @all_services ||= [] end |
.selectors ⇒ Object
114 115 116 |
# File 'lib/freyr/service.rb', line 114 def selectors names+groups end |
Instance Method Details
#alive? ⇒ Boolean
53 54 55 |
# File 'lib/freyr/service.rb', line 53 def alive? command.alive? end |
#describe ⇒ Object
85 86 87 |
# File 'lib/freyr/service.rb', line 85 def describe %Q{#{name}(#{groups.join(',')}) - #{start_command}} end |
#env ⇒ Object
21 22 23 |
# File 'lib/freyr/service.rb', line 21 def env service_info.env || {} end |
#error(*args, &blk) ⇒ Object
80 81 82 83 |
# File 'lib/freyr/service.rb', line 80 def error *args, &blk Freyr.logger.error(*args,&blk) Freyr.logger.debug("service info for service #{self}") {@service_info.inspect} end |
#inspect ⇒ Object
96 97 98 |
# File 'lib/freyr/service.rb', line 96 def inspect %Q{#<Freyr::Service #{name} #{start_command.inspect}>} end |
#is_group?(name) ⇒ Boolean
49 50 51 |
# File 'lib/freyr/service.rb', line 49 def is_group?(name) groups.find {|g| g.to_s == name.to_s} end |
#log ⇒ Object
25 26 27 |
# File 'lib/freyr/service.rb', line 25 def log @service_info.read_log || @service_info.log || File.join(command.file_dir,"#{name}.log") end |
#matches?(n) ⇒ Boolean
89 90 91 92 93 94 |
# File 'lib/freyr/service.rb', line 89 def matches?(n) n = n.to_s return true if name.to_s == n also.find {|a| a.to_s == n} end |
#ping! ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/freyr/service.rb', line 57 def ping! if ping pinger = Pinger.new(self) pinger.ping pinger end end |
#read_log ⇒ Object
65 66 67 |
# File 'lib/freyr/service.rb', line 65 def read_log @service_info.log || @service_info.read_log end |
#restart! ⇒ Object
45 46 47 |
# File 'lib/freyr/service.rb', line 45 def restart! command.restart! end |
#start! ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/freyr/service.rb', line 33 def start! if start_command command.run! unless alive? else error("no start_command") end end |
#start_command ⇒ Object
17 18 19 |
# File 'lib/freyr/service.rb', line 17 def start_command @service_info.start end |
#stop! ⇒ Object
41 42 43 |
# File 'lib/freyr/service.rb', line 41 def stop! command.kill! if start_command end |
#tail!(size = 600, follow = true) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/freyr/service.rb', line 69 def tail!(size = 600, follow = true) f = follow ? 'f' : '' if read_log cmd = "tail -#{size}#{f} #{File.join(dir||'/',read_log)}" Freyr.logger.debug("tailing cmd") {cmd.inspect} exec(cmd) else error("no logfile found") end end |
#write_log? ⇒ Boolean
29 30 31 |
# File 'lib/freyr/service.rb', line 29 def write_log? @service_info.log && !@service_info.read_log end |