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
Public Class Methods
# 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
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
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
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
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
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
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
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
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
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
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
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
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
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