Class: Logstomp::Command

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

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Command

Returns a new instance of Command.



14
15
16
17
# File 'lib/logstomp.rb', line 14

def initialize argv
  self.argv = argv
  self.start_at = Time.now
end

Instance Method Details

#batch_idObject



53
54
55
56
57
58
59
60
# File 'lib/logstomp.rb', line 53

def batch_id
  @batch_id ||= [
    "h#{hostname}",
    "p#{Process.pid.to_s(36)}",
    "t#{Time.now.to_i.to_s(36)}",
    "r#{rand(1_000_000_000_000).to_s(36)}"
  ].join('.')
end

#broadcast(event) ⇒ Object



44
45
46
47
# File 'lib/logstomp.rb', line 44

def broadcast event
  puts [ uri.to_s, event.to_s ].join("\n") if $DEBUG
  client.publish destination, event
end

#clientObject



40
41
42
# File 'lib/logstomp.rb', line 40

def client
  @client ||= Stomp::Client.new connection_string
end

#connection_stringObject



19
20
21
22
# File 'lib/logstomp.rb', line 19

def connection_string
  return "stomp://127.0.0.1:61613/topic/logs.#{tag}" unless argv[1]
  argv[1]
end

#destinationObject



28
29
30
# File 'lib/logstomp.rb', line 28

def destination
  uri.path
end

#disconnectObject



49
50
51
# File 'lib/logstomp.rb', line 49

def disconnect
  client.close
end

#executeObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/logstomp.rb', line 62

def execute
  m = %Q(<?xml version="1.0" encoding="UTF-8"?>
<event>
  <meta>
<tag>%s</tag>
<hostname>%s</hostname>
<batch_id>%s</batch_id>
  </meta>
  <entry>
<time tai64n="%s"/>
<text><![CDATA[%s]]></text>
  </entry>
</event>)

  STDIN.each_line do |l|
    label, line = l.split(/ /, 2)
    line.strip!
    broadcast m % [ label, tag, hostname, batch_id, line ]
  end
  disconnect
end

#hostnameObject



36
37
38
# File 'lib/logstomp.rb', line 36

def hostname
  @hostname ||= %x[hostname].strip.split(/\./)[0]
end

#tagObject



32
33
34
# File 'lib/logstomp.rb', line 32

def tag
  argv[0] || "default"
end

#uriObject



24
25
26
# File 'lib/logstomp.rb', line 24

def uri
  URI.parse connection_string
end