Class: RSpecTelemetry::Writer

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

Instance Method Summary collapse

Constructor Details

#initialize(output_path, flush_each: false) ⇒ Writer

Returns a new instance of Writer.



8
9
10
11
12
13
# File 'lib/rspec_telemetry/writer.rb', line 8

def initialize(output_path, flush_each: false)
  @output_path = output_path
  @flush_each = flush_each
  @mutex = Mutex.new
  @io = nil
end

Instance Method Details

#closeObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/rspec_telemetry/writer.rb', line 42

def close
  @mutex.synchronize do
    @io&.flush
    @io&.close
    @io = nil
  end

rescue => e
  warn_failure("close", e)
end

#flushObject



36
37
38
39
40
# File 'lib/rspec_telemetry/writer.rb', line 36

def flush
  @mutex.synchronize { @io&.flush }
rescue => e
  warn_failure("flush", e)
end

#openObject



15
16
17
18
19
20
21
22
# File 'lib/rspec_telemetry/writer.rb', line 15

def open
  FileUtils.mkdir_p(File.dirname(@output_path))
  # Each run gets a fresh stream; appending would create misleading time gaps.
  @io = File.open(@output_path, "w")
rescue => e
  warn_failure("open", e)
  @io = nil
end

#write(event) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rspec_telemetry/writer.rb', line 24

def write(event)
  return unless @io

  @mutex.synchronize do
    @io.puts(JSON.generate(event))
    @io.flush if @flush_each
  end

rescue => e
  warn_failure("write", e)
end