Class: Piper::Manager

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#hostObject

Returns the value of attribute host.



16
17
18
# File 'lib/piper/manager.rb', line 16

def host
  @host
end

#portObject

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.

Parameters:

  • what (String)

    reason or contents of the record

  • tid (String) (defaults to: nil)

    tracking identifier if any

  • the (String)

    record identifier of the created 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.

Parameters:

  • rid (String)

    record identifier of the record to delete

Returns:

  • (Net::HTTP::Response)

    of the send request.



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.

Parameters:

  • what (String)

    reason or contents of the record

  • tid (String) (defaults to: nil)

    tracking identifier if any

  • the (String)

    record identifier of the created 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.

Parameters:

  • what (String)

    reason or contents of the record

  • tid (String) (defaults to: nil)

    tracking identifier if any

  • the (String)

    record identifier of the created 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.

Parameters:

  • rid (String)

    record identifier of the record to get

Returns:

  • (Net::HTTP::Response)

    of the send request.



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.

Parameters:

  • what (String)

    reason or contents of the record

  • tid (String) (defaults to: nil)

    tracking identifier if any

  • the (String)

    record identifier of the created 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.

Parameters:

  • level (Integer|String|Symbol)

    severity level (e.g., 1, “error”, or :error)

  • what (String)

    reason or contents of the record

  • tid (String) (defaults to: nil)

    tracking identifier if any

  • where (String) (defaults to: nil)

    where the log even was created, default is <file>:<line>

  • the (String)

    record identifier of the created log record.



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

Parameters:

  • url (String)

    URL to connect to NATS server e.g., nats://localhost:4222

  • subject (String)

    subkect prefix



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.

Parameters:

  • rid (String)

    record identifier for the published record

  • object (Object)

    Object to convert to JSON before storing



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.

Parameters:

  • rid (String)

    record identifier for the published record

  • json (String)

    json to publish to Piper



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.

Parameters:

  • rid (String)

    record identifier

  • object (Object)

    Object to convert to JSON before storing

Returns:

  • (Net::HTTP::Response)

    of the send request.



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.

Parameters:

  • rid (String)

    record identifier

  • json (String)

    JSON to store in Piper

Returns:

  • (Net::HTTP::Response)

    of the send request.



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.

Parameters:

  • what (String)

    reason or contents of the record

  • tid (String) (defaults to: nil)

    tracking identifier if any

  • the (String)

    record identifier of the created log record.



132
133
134
# File 'lib/piper/manager.rb', line 132

def warn(what, tid=nil)
  log(:warn, what, tid)
end