class Piper::Manager

Piper::Manager is a simple class that is used to manage cached content in a Piper Push Cache server. It uses HTTP request in a REST pattern.

Attributes

host[RW]
port[RW]

Public Class Methods

new(host, port) click to toggle source
# File lib/piper/manager.rb, line 19
def initialize(host, port)
  @host = host
  @port = port
  @prev_log_rid = nil
  @nats_url = nil
  @subject = nil
end

Public Instance Methods

debug(what, tid=nil) click to toggle source

Put or publish a debug log record. @param [String] what reason or contents of the record @param [String] tid tracking identifier if any @param [String] the record identifier of the created log record.

# File lib/piper/manager.rb, line 148
def debug(what, tid=nil)
  log(:debug, what, tid)
end
delete(rid) click to toggle source

Deletes a record from the Piper cache. @param [String] rid record identifier of the record to delete @return [Net::HTTP::Response] of the send request.

# File lib/piper/manager.rb, line 49
def delete(rid)
  h = Net::HTTP.new(@host, @port)
  h.send_request('DELETE', "/#{rid}")
end
error(what, tid=nil) click to toggle source

Put or publish a error log record. @param [String] what reason or contents of the record @param [String] tid tracking identifier if any @param [String] the record identifier of the created log record.

# File lib/piper/manager.rb, line 124
def error(what, tid=nil)
  log(:error, what, tid)
end
fatal(what, tid=nil) click to toggle source

Put or publish a fatal log record. @param [String] what reason or contents of the record @param [String] tid tracking identifier if any @param [String] the record identifier of the created log record.

# File lib/piper/manager.rb, line 116
def fatal(what, tid=nil)
  log(:fatal, what, tid)
end
get(rid) click to toggle source

Gets a record from the Piper cache. The JSON will be in the body of the response if successful. @param [String] rid record identifier of the record to get @return [Net::HTTP::Response] of the send request.

# File lib/piper/manager.rb, line 58
def get(rid)
  h = Net::HTTP.new(@host, @port)
  h.send_request('GET', "/#{rid}")
end
info(what, tid=nil) click to toggle source

Put or publish a info log record. @param [String] what reason or contents of the record @param [String] tid tracking identifier if any @param [String] the record identifier of the created log record.

# File lib/piper/manager.rb, line 140
def info(what, tid=nil)
  log(:info, what, tid)
end
log(level, what, tid=nil, where=nil) click to toggle source

Send a log entry to Piper either by a publish over NATS or by a HTTP PUT. @param [Integer|String|Symbol] level severity level (e.g., 1, “error”, or :error) @param [String] what reason or contents of the record @param [String] tid tracking identifier if any @param [String] where where the log even was created, default is <file>:<line> @param [String] the record identifier of the created log record.

# File lib/piper/manager.rb, line 99
def log(level, what, tid=nil, where=nil)
  rec = LogRec.new(level, what, tid, where)
  rid_num = rec.when.strftime('%Y%m%d%H%M%S%N').to_i
  rid_num = @prev_log_rid + 1 if !@prev_log_rid.nil? && @prev_log_rid >= rid_num
  rid = "#{LogRec.who}-#{rid_num}"
  if @subject.nil?
    push_json(rid, rec.to_s)
  else
    NATS.start(:uri => uri) { NATS.publish("#{@subject}.#{rid}", rec.to_s) { NATS.stop } }
  end
  rid
end
nats_init(url, subject) click to toggle source

Sets up NATS. A connection is not made until a log entry is made or a publish is made. The subject should provided will be the prefix for published records and for log messages. Log messages are published on <subject>.log @param [String] url URL to connect to NATS server e.g., nats://localhost:4222 @param [String] subject subkect prefix

# File lib/piper/manager.rb, line 69
def nats_init(url, subject)
  if (defined? NATS)
    @nats_url = url
    @nats_url = 'nats://localhost:4222' if url.nil?
    @subject = subject
  else
    raise Exception.new("NATS gem not installed.")
  end
end
publish(rid, obj) click to toggle source

Publishs a JSON message using a NATS client. @param [String] rid record identifier for the published record @param [Object] object Object to convert to JSON before storing

# File lib/piper/manager.rb, line 82
def publish(rid, obj)
  NATS.start(:uri => uri) { NATS.publish("#{@subject}.#{rid}", Oj::dump(obj, mode: :compat)) { NATS.stop } }
end
publish_json(rid, json) click to toggle source

Publishs a JSON message using a NATS client. @param [String] rid record identifier for the published record @param [String] json json to publish to Piper

# File lib/piper/manager.rb, line 89
def publish_json(rid, json)
  NATS.start(:uri => uri) { NATS.publish("#{@subject}.#{rid}", json) { NATS.stop } }
end
push(rid, obj) click to toggle source

Converts an object to JSON and then pushed that JSON to Piper and associates it with the provided record identifier. @param [String] rid record identifier @param [Object] object Object to convert to JSON before storing @return [Net::HTTP::Response] of the send request.

# File lib/piper/manager.rb, line 42
def push(rid, obj)
  push_json(rid, Oj::dump(obj, mode: :compat))
end
push_json(rid, json) click to toggle source

Pushes JSON to Piper and associates it with the provided record identifier. @param [String] rid record identifier @param [String] json JSON to store in Piper @return [Net::HTTP::Response] of the send request.

# File lib/piper/manager.rb, line 32
def push_json(rid, json)
  h = Net::HTTP.new(@host, @port)
  h.send_request('PUT', "/#{rid}", json, {'Content-Type' => 'text/plain; charset=utf-8'})
end
warn(what, tid=nil) click to toggle source

Put or publish a warning log record. @param [String] what reason or contents of the record @param [String] tid tracking identifier if any @param [String] the record identifier of the created log record.

# File lib/piper/manager.rb, line 132
def warn(what, tid=nil)
  log(:warn, what, tid)
end