Sha256: c85d234de1986d00d774723489c6e813c518492d3e36614a4048f86104941906

Contents?: true

Size: 1.86 KB

Versions: 10

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Akita
  module HarLogger
    # A thread that consumes HarEntry objects from a queue and writes them to a
    # file.
    #
    # Params:
    # +out_file_name+:: the name of the HAR file to be produced.
    # +entry_queue+:: the queue from which to consume HAR entries.
    class WriterThread
      def initialize(out_file_name, entry_queue)
        # This mutex is used to ensure the entire output is written before the
        # application shuts down.
        shutdown_mutex = Mutex.new

        Thread.new do
          shutdown_mutex.synchronize {
            File.open(out_file_name, 'w') { |f|
              # Produce a preamble.
              f.write <<~EOF.chomp
                {
                  "log": {
                    "version": "1.2",
                    "creator": {
                      "name": "Akita HAR logger for Ruby",
                      "version": "1.0.0"
                    },
                    "entries": [
              EOF

              first_entry = true

              loop do
                entry = entry_queue.pop
                if entry == nil then break end

                # Emit comma separator if needed.
                f.puts (first_entry ? '' : ',')
                first_entry = false

                # Emit the dequeued entry.
                f.write JSON.generate(entry)
              end

              # Produce the epilogue.
              f.write <<~EOF

                    ]
                  }
                }
              EOF
            }
          }
        end

        # Finish outputting the HAR file when the application shuts down.
        at_exit do
          # Signal to the consumer that this is the end of the entry stream and
          # wait for the consumer to terminate.
          entry_queue << nil
          shutdown_mutex.synchronize {}
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
akita-har_logger-0.2.11 lib/akita/har_logger/writer_thread.rb
akita-har_logger-0.2.10 lib/akita/har_logger/writer_thread.rb
akita-har_logger-0.2.9 lib/akita/har_logger/writer_thread.rb
akita-har_logger-0.2.8 lib/akita/har_logger/writer_thread.rb
akita-har_logger-0.2.7 lib/akita/har_logger/writer_thread.rb
akita-har_logger-0.2.6 lib/akita/har_logger/writer_thread.rb
akita-har_logger-0.2.5 lib/akita/har_logger/writer_thread.rb
akita-har_logger-0.2.4 lib/akita/har_logger/writer_thread.rb
akita-har_logger-0.2.3 lib/akita/har_logger/writer_thread.rb
akita-har_logger-0.2.2 lib/akita/har_logger/writer_thread.rb