Class: GraphQL::Hive::UsageReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql-hive/usage_reporter.rb

Overview

Report usage to Hive API without impacting application performances

Constant Summary collapse

@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, client) ⇒ UsageReporter

Returns a new instance of UsageReporter.



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
# File 'lib/graphql-hive/usage_reporter.rb', line 22

def initialize(options, client)
  @@instance = self

  @options = options
  @client = client

  @options_mutex = Mutex.new
  @queue = Queue.new
  @thread = Thread.new do
    buffer = []
    while (operation = @queue.pop(false))
      @options[:logger].debug("add operation to buffer: #{operation}")
      buffer << operation
      @options_mutex.synchronize do
        if buffer.size >= @options[:buffer_size]
          @options[:logger].debug('buffer is full, sending!')
          process_operations(buffer)
          buffer = []
        end
      end
    end
    unless buffer.size.zero?
      @options[:logger].debug('shuting down with buffer, sending!')
      process_operations(buffer)
    end
  end
end

Class Method Details

.instanceObject



18
19
20
# File 'lib/graphql-hive/usage_reporter.rb', line 18

def self.instance
  @@instance
end

Instance Method Details

#add_operation(operation) ⇒ Object



50
51
52
# File 'lib/graphql-hive/usage_reporter.rb', line 50

def add_operation(operation)
  @queue.push(operation)
end

#on_exitObject



54
55
56
57
# File 'lib/graphql-hive/usage_reporter.rb', line 54

def on_exit
  @queue.close
  @thread.join
end