Class: Msgknife::Stream
- Inherits:
-
Object
- Object
- Msgknife::Stream
- Defined in:
- lib/msgknife.rb
Instance Attribute Summary collapse
-
#optpsr ⇒ Object
Returns the value of attribute optpsr.
Instance Method Summary collapse
-
#initialize ⇒ Stream
constructor
A new instance of Stream.
- #read_io(io) ⇒ Object
- #read_stream(files = nil) ⇒ Object
- #recv(obj, ts, tag) ⇒ Object
- #run(cmd_argv, range = nil) ⇒ Object
- #setup(argv) ⇒ Object
- #teardown ⇒ Object
- #write_stream(obj, ts = nil, tag = nil) ⇒ Object
Constructor Details
#initialize ⇒ Stream
Returns a new instance of Stream.
10 11 12 13 14 15 16 17 18 |
# File 'lib/msgknife.rb', line 10 def initialize @out_encode = false @optpsr = OptionParser.new @optpsr.on('-m', 'output as msgpack encode') { |v| @out_encode = v } @optpsr.on('-F', 'input as fluentd format') { |v| @in_fluentd = v } @optpsr.on('-T VAL', 'key of timestamp in message') { |v| @ts_key = v } @optpsr.on('-N', 'ignore if value is nil') {|v| @ignore_nil = v } @argv = [] end |
Instance Attribute Details
#optpsr ⇒ Object
Returns the value of attribute optpsr.
8 9 10 |
# File 'lib/msgknife.rb', line 8 def optpsr @optpsr end |
Instance Method Details
#read_io(io) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/msgknife.rb', line 34 def read_io(io) u = MessagePack::Unpacker.new(io) begin u.each {|obj| if @in_fluentd recv(obj[2], obj[1], obj[0]) else if @ts_key.nil? or !(obj.key?(@ts_key)) recv(obj, nil, nil) else ts = nil ts_val = obj[@ts_key] case ts_val when String dt = Time.parse(ts_val) rescue nil ts = dt.to_i when Fixnum ts = ts_val when Float ts = ts_val.to_i end recv(obj, ts, nil) end end } rescue EOFError # ignore rescue Interrupt return end end |
#read_stream(files = nil) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/msgknife.rb', line 67 def read_stream(files=nil) if files == nil or files.size == 0 read_io(STDIN) else f_list = Array.new if files.instance_of? String f_list << files else f_list += files end f_list.each do |fpath| if File.directory?(fpath) Find.find(fpath) do |file| next if File.directory?(file) read_io(File.open(file, 'r')) end else read_io(File.open(fpath, 'r')) end end end end |
#recv(obj, ts, tag) ⇒ Object
113 114 115 |
# File 'lib/msgknife.rb', line 113 def recv(obj, ts, tag) raise 'exec(obj) must be implemented' end |
#run(cmd_argv, range = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/msgknife.rb', line 20 def run(cmd_argv, range = nil) args = @optpsr.parse(cmd_argv) unless range.nil? raise "Not enough arguments" if args.size < range.last + 1 @argv = args.slice!(range) end setup(@argv) read_stream(args) teardown end |
#setup(argv) ⇒ Object
109 110 111 |
# File 'lib/msgknife.rb', line 109 def setup(argv) raise 'setup(argv) must be implemented' if argv.size > 0 end |
#teardown ⇒ Object
117 |
# File 'lib/msgknife.rb', line 117 def teardown; end |
#write_stream(obj, ts = nil, tag = nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/msgknife.rb', line 91 def write_stream(obj, ts=nil, tag=nil) if @out_encode == false if ts.nil? and tag.nil? pp obj else pp [tag, ts, obj] end else if ts.nil? and tag.nil? STDOUT.write(obj.to_msgpack) else STDOUT.write([tag, ts, obj].to_msgpack) end end rescue Errno::EPIPE => e ; end |