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.



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

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.



29
30
31
# File 'lib/steno/sink/io.rb', line 29

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
# File 'lib/steno/sink/io.rb', line 17

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

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

  io.sync = autoflush

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

Instance Method Details

#add_record(record) ⇒ Object



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

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

  @io_lock.synchronize do
    retries = 0
    begin
      @io.write(bytes)
    rescue IOError => e
      raise e unless retries < @max_retries

      retries += 1
      retry
    end
  end

  nil
end

#flushObject



63
64
65
66
67
# File 'lib/steno/sink/io.rb', line 63

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

  nil
end