Class: Msgknife::Stream

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStream

Returns a new instance of Stream.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/msgknife.rb', line 14

def initialize
  @out_encode = false
  @optpsr = OptionParser.new
  @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 = []
  
  @output_fmt = 'console'
  @optpsr.on('-j', 'output as json') { |v| @output_fmt = 'json' }
  @optpsr.on('-m', 'output as msgpack') { |v| @output_fmt = 'msgpack' }
end

Instance Attribute Details

#optpsrObject

Returns the value of attribute optpsr.



12
13
14
# File 'lib/msgknife.rb', line 12

def optpsr
  @optpsr
end

Instance Method Details

#read_io(io) ⇒ Object



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
66
67
68
69
70
71
72
# File 'lib/msgknife.rb', line 41

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/msgknife.rb', line 74

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



116
117
118
# File 'lib/msgknife.rb', line 116

def recv(obj, ts, tag)
  raise 'exec(obj) must be implemented'
end

#run(cmd_argv, range = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/msgknife.rb', line 27

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



112
113
114
# File 'lib/msgknife.rb', line 112

def setup(argv)
  raise 'setup(argv) must be implemented' if argv.size > 0
end

#teardownObject



120
# File 'lib/msgknife.rb', line 120

def teardown; end

#write_stream(obj, ts = nil, tag = nil, io = STDOUT) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/msgknife.rb', line 98

def write_stream(obj, ts=nil, tag=nil, io=STDOUT)
  msg =(ts.nil? and tag.nil?) ? obj : [tag, ts, obj]

  begin
    case @output_fmt
    when 'console'; PP.pp(msg, io);
    when 'json';    JSON.dump(msg, io);
    when 'msgpack'; io.write(msg.to_msgpack);
    end
  rescue Errno::EPIPE => e ;
  end
end