Class: Steno::Sink::IO

Inherits:
Base show all
Defined in:
lib/steno/sink/io.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#codec

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, opts = {}) ⇒ IO

Returns a new instance of IO.

Parameters:

  • io (IO)

    The IO object that will be written to

  • opts (Hash) (defaults to: {})

    Key :codec is used to specify a codec inheriting from Steno::Codec::Base. Key :max_retries takes an integer value which specifies the number of times the write operation can be retried when IOError is raised while writing a record.



39
40
41
42
43
44
45
# File 'lib/steno/sink/io.rb', line 39

def initialize(io, opts = {})
  super(opts[:codec])

  @max_retries = opts[:max_retries] || -1
  @io_lock = Mutex.new
  @io = io
end

Instance Attribute Details

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



31
32
33
# File 'lib/steno/sink/io.rb', line 31

def max_retries
  @max_retries
end

Class Method Details

.for_file(path, opts = {}) ⇒ Steno::Sink::IO

Returns a new sink configured to append to the file at path.

Parameters:

  • path (String)
  • If (Hash)

    the key :autoflush is set to true, encoded records will not be buffered by Ruby. The key :max_retries is forwarded to Steno::Sink::IO object during creation.

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/steno/sink/io.rb', line 17

def for_file(path, opts = {})
  autoflush = true
  if opts.include?(:autoflush)
    autoflush = opts[:autoflush]
  end

  io = File.open(path, "a+")

  io.sync = autoflush

  new(io, :max_retries => opts[:max_retries])
end

Instance Method Details

#add_record(record) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/steno/sink/io.rb', line 47

def add_record(record)
  bytes = @codec.encode_record(record)

  @io_lock.synchronize do
    retries = 0
    begin
      @io.write(bytes)
    rescue IOError => e
      if retries < @max_retries
        retries += 1
        retry
      else
        raise e
      end
    end
  end

  nil
end

#flushObject



67
68
69
70
71
# File 'lib/steno/sink/io.rb', line 67

def flush
  @io_lock.synchronize { @io.flush }

  nil
end