Class: Watchcat

Inherits:
Object
  • Object
show all
Defined in:
lib/watchcat.rb

Overview

Overview

Ruby/Watchcat is a library for the development of watchcatd-aware applications. It requires watchcatd to be installed and running, and communicates with it via UNIX sockets.

Constant Summary collapse

DEFAULT_TIMEOUT =
60
DEFAULT_DEVICE =
'/var/run/watchcat.socket'
DEFAULT_SIGNAL =
Signal.list['KILL']

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Watchcat

Create a new Watchcat object. The parameter hash may have the following symbols:

timeout

If watchcatd doesn’t receive a heartbeat after this period (in seconds), it will signal the process. (default: 60)

signal

Defines which signal will be sent to the process after the timeout expires. Can be a string like ‘HUP’ or ‘SIGHUP’ or an integer like 9. (default: 9)

info

Should be a string which is added to the log generated by watchcatd when it signals a process. (default: nil)

device

The watchcat device. (default: /var/run/watchcat.socket). Use for debugging purposes.

If a block is given, the Watchcat object will be yielded and automatically closed on block termination.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/watchcat.rb', line 54

def initialize(args = {}) # :yield:
  timeout = args[:timeout] || DEFAULT_TIMEOUT
  device  = args[:device] || DEFAULT_DEVICE
  info    = args[:info] ? args[:info].to_s : ''

  unless timeout.is_a? Fixnum
    raise ArgumentError, 'timeout must be an integer'
  end

  signal = signal_number(args[:signal])
  @sock  = create_socket(device)
  msg    = build_message(timeout, signal, info)

  safe_write(@sock, msg)
  unless safe_read(@sock, 256) == "ok\n"
    @sock.close
    # Probably not the best error, but it matches libwcat.
    raise Errno::EPERM
  end

  if block_given?
    begin
      yield(self)
    ensure
      @sock.close
    end
  end
  return self
end

Instance Method Details

#closeObject

Close communication with watchcatd.



91
92
93
94
95
96
97
98
# File 'lib/watchcat.rb', line 91

def close
  begin
    @sock.close
  rescue Errno::EINTR
    retry
  end
  return nil
end

#heartbeatObject

Send a heartbeat to watchcatd, telling it we’re still alive.



85
86
87
88
# File 'lib/watchcat.rb', line 85

def heartbeat
  safe_write(@sock, '.')
  return nil
end