Class: Chillout::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/chillout/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_or_api_key, options = {}) {|@config| ... } ⇒ Client

Returns a new instance of Client.

Yields:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chillout/client.rb', line 21

def initialize(config_or_api_key, options = {})
  build_config(config_or_api_key, options)

  yield @config if block_given?

  @logger = PrefixedLogger.new("Chillout", @config.logger)

  @http_client = HttpClient.new(@config, logger).freeze
  @server_side = ServerSide.new(@config, @http_client).freeze
  @dispatcher = Dispatcher.new(@server_side).freeze
  case @config.strategy
  when :thread
    @queue = Queue.new
    @worker_mutex = Mutex.new
    @worker_thread = nil
    @max_queue = config.max_queue
  when :active_job
    require 'chillout/job'
    Chillout::Job.dispatcher = @dispatcher
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



17
18
19
# File 'lib/chillout/client.rb', line 17

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/chillout/client.rb', line 18

def logger
  @logger
end

#queueObject (readonly)

Returns the value of attribute queue.



19
20
21
# File 'lib/chillout/client.rb', line 19

def queue
  @queue
end

Instance Method Details

#enqueue(metrics) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chillout/client.rb', line 51

def enqueue(metrics)
  case @config.strategy
  when :thread
    start_worker
    if @queue.size < @max_queue
      @queue << metrics
      @logger.debug "Metrics were enqueued."
    else
      @logger.error "Metrics buffer overflow. Skipping enqueue."
    end
  when :active_job
    Chillout::Job.perform_later(YAML.dump(metrics))
    @logger.debug "Metrics were enqueued."
  end
end

#startObject



43
44
45
46
47
48
49
# File 'lib/chillout/client.rb', line 43

def start
  case @config.strategy
  when :thread
    start_worker
  when :active_job
  end
end

#start_workerObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/chillout/client.rb', line 71

def start_worker
  return if worker_running?
  @worker_mutex.synchronize do
    return if worker_running?
    @worker_thread = Thread.new do
      worker = Worker.new(@max_queue, @dispatcher, @queue, @logger)
      worker.run
    end
  end
end

#worker_running?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/chillout/client.rb', line 67

def worker_running?
  @worker_thread && @worker_thread.alive?
end