lib/runit-man/service_status.rb in runit-man-2.4.1 vs lib/runit-man/service_status.rb in runit-man-2.4.2
- old
+ new
@@ -1,23 +1,27 @@
# Represents service status in daemontools supervise format.
-# @ note see runit's sv.c source code for details.
+# @note see runit's sv.c source code for details.
class ServiceStatus
# Size of status data in bytes
STATUS_SIZE = 20
# Service is down.
S_DOWN = 0
# Service is running.
S_RUN = 1
# Service is finishing.
S_FINISH = 2
- # Initializes serice status by binary data.
+ # Initializes service status by binary data.
# @param [String] data Binary data of service status in daemontools supervise format.
def initialize(data)
- data = (!data.nil? && data.length == STATUS_SIZE) ? data : nil
-
- @raw = data.nil? ? nil : data.unpack('NNxxxxVxa1CC')
+ @raw = nil
+ unless data.nil?
+ data_size = data.respond_to?(:bytesize) ? data.bytesize : data.size
+ if data_size == STATUS_SIZE
+ @raw = data.unpack('NNxxxxVxa1CC')
+ end
+ end
end
# Is service inactive?
def inactive?
@raw.nil?
@@ -55,21 +59,26 @@
# @return [Float] Service uptime in seconds if running; otherwise nil.
def uptime
@uptime ||= down? ? nil : Time.now - started_at
end
+ # Is service want up?
def want_up?
@raw && !pid && @raw[3] == 'u'
end
+ # Is service want down?
def want_down?
pid && @raw[3] == 'd'
end
+ # Is service got TERM signal?
def got_term?
pid && @raw[4] != 0
end
+ # Gets service status in string format.
+ # @return [String] Service status in string format.
def to_s
return 'inactive' if inactive?
# try to mimics stat behaviour to minimize readings
result = status_string