Class: Akita::HarLogger::WriterThread

Inherits:
Object
  • Object
show all
Defined in:
lib/akita/har_logger/writer_thread.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(out_file_name, entry_queue) ⇒ WriterThread

Returns a new instance of WriterThread.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/akita/har_logger/writer_thread.rb', line 12

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
    Rails.logger.debug "AKITA: About to acquire shutdown mutex "\
                       "#{shutdown_mutex} for file #{out_file_name} "\
                       "in writer thread #{Thread.current}. "\
                       "Self-owned? #{shutdown_mutex.owned?}"
    shutdown_mutex.synchronize {
      begin
        Rails.logger.debug "AKITA: Acquired shutdown mutex "\
                           "#{shutdown_mutex} for file "\
                           "#{out_file_name} in writer thread "\
                           "#{Thread.current}."
        File.open(out_file_name, 'w') { |f|
          # Produce a preamble.
          f.write "            {\n              \"log\": {\n                \"version\": \"1.2\",\n                \"creator\": {\n                  \"name\": \"Akita HAR logger for Ruby\",\n                  \"version\": \"1.0.0\"\n                },\n                \"entries\": [\n          EOF\n\n          first_entry = true\n\n          loop do\n            entry = entry_queue.pop\n            if entry == nil then break end\n\n            # Emit comma separator if needed.\n            f.puts (first_entry ? '' : ',')\n            first_entry = false\n\n            # Emit the dequeued entry.\n            f.write JSON.generate(entry)\n          end\n\n          # Produce the epilogue.\n          f.write <<~EOF\n\n                ]\n              }\n            }\n          EOF\n        }\n      ensure\n        Rails.logger.debug \"AKITA: About to release shutdown mutex \"\\\n                           \"\#{shutdown_mutex} for file \"\\\n                           \"\#{out_file_name} in writer thread \"\\\n                           \"\#{Thread.current}.\"\n      end\n    }\n  end\n\n  # Finish outputting the HAR file when the application shuts down.\n  at_exit do\n    # Signal to the consumer that this is the end of the entry stream and\n    # wait for the consumer to terminate.\n    entry_queue << nil\n    Rails.logger.debug \"AKITA: About to acquire shutdown mutex \"\\\n                       \"\#{shutdown_mutex} for file \#{out_file_name} \"\\\n                       \"while shutting down in \#{Thread.current}. \"\\\n                       \"Self-owned? \#{shutdown_mutex.owned?}\"\n    shutdown_mutex.synchronize {\n      Rails.logger.debug \"AKITA: Acquired shutdown mutex \"\\\n                         \"\#{shutdown_mutex} for file \#{out_file_name} \"\\\n                         \"while shutting down in \#{Thread.current}.\"\n      Rails.logger.debug \"AKITA: About to release shutdown mutex \"\\\n                         \"\#{shutdown_mutex} for file \#{out_file_name} \"\\\n                         \"while shutting down in \#{Thread.current}.\"\n    }\n  end\nend\n".chomp