Class: ServiceStatus

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

Overview

Note:

see runit's sv.c source code for details.

Represents service status in daemontools supervise format.

Constant Summary collapse

STATUS_SIZE =

Size of status data in bytes

20
S_DOWN =

Service is down.

0
S_RUN =

Service is running.

1
S_FINISH =

Service is finishing.

2

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ServiceStatus

Initializes service status by binary data.

Parameters:

  • data (String)

    Binary data of service status in daemontools supervise format.



15
16
17
18
19
20
21
22
23
# File 'lib/runit-man/service_status.rb', line 15

def initialize(data)
  @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

Instance Method Details

#down?Boolean

Is service down?

Returns:

  • (Boolean)


31
32
33
# File 'lib/runit-man/service_status.rb', line 31

def down?
  status_byte == S_DOWN
end

#finish?Boolean

Is service finishing?

Returns:

  • (Boolean)


41
42
43
# File 'lib/runit-man/service_status.rb', line 41

def finish?
  status_byte == S_FINISH
end

#got_term?Boolean

Is service got TERM signal?

Returns:

  • (Boolean)


75
76
77
# File 'lib/runit-man/service_status.rb', line 75

def got_term?
  pid && @raw[4] != 0
end

#inactive?Boolean

Is service inactive?

Returns:

  • (Boolean)


26
27
28
# File 'lib/runit-man/service_status.rb', line 26

def inactive?
  @raw.nil?
end

#pidFixnum

Gets service process id.

Returns:

  • (Fixnum)

    Process id.



47
48
49
# File 'lib/runit-man/service_status.rb', line 47

def pid
  @pid ||= down? ? nil : @raw[2]
end

#run?Boolean

Is service running?

Returns:

  • (Boolean)


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

def run?
  status_byte == S_RUN
end

#started_atTime

Gets service start time.

Returns:

  • (Time)

    Service start time.



53
54
55
56
# File 'lib/runit-man/service_status.rb', line 53

def started_at
  # from TAI to Unix
  @started_at ||= @raw ? Time.at((@raw[0] << 32) + @raw[1] - 4611686018427387914) : nil
end

#to_sString

Gets service status in string format.

Returns:

  • (String)

    Service status in string format.



81
82
83
84
85
86
87
88
89
90
# File 'lib/runit-man/service_status.rb', line 81

def to_s
  return 'inactive'  if inactive?

  # try to mimics stat behaviour to minimize readings
  result = status_string
  result += ', got TERM'  if got_term?
  result += ', want down'  if want_down?
  result += ', want up'  if want_up?
  result
end

#uptimeFloat

Gets service uptime in seconds.

Returns:

  • (Float)

    Service uptime in seconds if running; otherwise nil.



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

def uptime
  @uptime ||= down? ? nil : Time.now - started_at
end

#want_down?Boolean

Is service want down?

Returns:

  • (Boolean)


70
71
72
# File 'lib/runit-man/service_status.rb', line 70

def want_down?
  pid && @raw[3] == 'd'
end

#want_up?Boolean

Is service want up?

Returns:

  • (Boolean)


65
66
67
# File 'lib/runit-man/service_status.rb', line 65

def want_up?
  @raw && !pid && @raw[3] == 'u'
end