Class: Piper::Manager
- Inherits:
-
Object
- Object
- Piper::Manager
- Defined in:
- lib/piper/manager.rb
Overview
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.
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
-
#debug(what, tid = nil) ⇒ Object
Put or publish a debug log record.
-
#delete(rid) ⇒ Net::HTTP::Response
Deletes a record from the Piper cache.
-
#error(what, tid = nil) ⇒ Object
Put or publish a error log record.
-
#fatal(what, tid = nil) ⇒ Object
Put or publish a fatal log record.
-
#get(rid) ⇒ Net::HTTP::Response
Gets a record from the Piper cache.
-
#info(what, tid = nil) ⇒ Object
Put or publish a info log record.
-
#initialize(host, port) ⇒ Manager
constructor
A new instance of Manager.
-
#log(level, what, tid = nil, where = nil) ⇒ Object
Send a log entry to Piper either by a publish over NATS or by a HTTP PUT.
-
#nats_init(url, subject) ⇒ Object
Sets up NATS.
-
#publish(rid, obj) ⇒ Object
Publishs a JSON message using a NATS client.
-
#publish_json(rid, json) ⇒ Object
Publishs a JSON message using a NATS client.
-
#push(rid, obj) ⇒ Net::HTTP::Response
Converts an object to JSON and then pushed that JSON to Piper and associates it with the provided record identifier.
-
#push_json(rid, json) ⇒ Net::HTTP::Response
Pushes JSON to Piper and associates it with the provided record identifier.
-
#warn(what, tid = nil) ⇒ Object
Put or publish a warning log record.
Constructor Details
#initialize(host, port) ⇒ Manager
Returns a new instance of Manager.
19 20 21 22 23 24 25 |
# 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 |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
16 17 18 |
# File 'lib/piper/manager.rb', line 16 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
17 18 19 |
# File 'lib/piper/manager.rb', line 17 def port @port end |
Instance Method Details
#debug(what, tid = nil) ⇒ Object
Put or publish a debug log record.
148 149 150 |
# File 'lib/piper/manager.rb', line 148 def debug(what, tid=nil) log(:debug, what, tid) end |
#delete(rid) ⇒ Net::HTTP::Response
Deletes a record from the Piper cache.
49 50 51 52 |
# 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) ⇒ Object
Put or publish a error log record.
124 125 126 |
# File 'lib/piper/manager.rb', line 124 def error(what, tid=nil) log(:error, what, tid) end |
#fatal(what, tid = nil) ⇒ Object
Put or publish a fatal log record.
116 117 118 |
# File 'lib/piper/manager.rb', line 116 def fatal(what, tid=nil) log(:fatal, what, tid) end |
#get(rid) ⇒ Net::HTTP::Response
Gets a record from the Piper cache. The JSON will be in the body of the response if successful.
58 59 60 61 |
# 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) ⇒ Object
Put or publish a info log record.
140 141 142 |
# File 'lib/piper/manager.rb', line 140 def info(what, tid=nil) log(:info, what, tid) end |
#log(level, what, tid = nil, where = nil) ⇒ Object
Send a log entry to Piper either by a publish over NATS or by a HTTP PUT.
99 100 101 102 103 104 105 106 107 108 109 110 |
# 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) ⇒ Object
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
69 70 71 72 73 74 75 76 77 |
# 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) ⇒ Object
Publishs a JSON message using a NATS client.
82 83 84 |
# 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) ⇒ Object
Publishs a JSON message using a NATS client.
89 90 91 |
# 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) ⇒ Net::HTTP::Response
Converts an object to JSON and then pushed that JSON to Piper and associates it with the provided record identifier.
42 43 44 |
# File 'lib/piper/manager.rb', line 42 def push(rid, obj) push_json(rid, Oj::dump(obj, mode: :compat)) end |
#push_json(rid, json) ⇒ Net::HTTP::Response
Pushes JSON to Piper and associates it with the provided record identifier.
32 33 34 35 |
# 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) ⇒ Object
Put or publish a warning log record.
132 133 134 |
# File 'lib/piper/manager.rb', line 132 def warn(what, tid=nil) log(:warn, what, tid) end |