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:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chillout/client.rb', line 22

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
  @event_data_builder = EventDataBuilder.new(@config).freeze
  @server_side = ServerSide.new(@event_data_builder, @http_client).freeze
  @dispatcher = Dispatcher.new(@server_side).freeze
  @queue = Queue.new
  @worker_mutex = Mutex.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#queueObject (readonly)

Returns the value of attribute queue.



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

def queue
  @queue
end

Instance Method Details

#enqueue(creations) ⇒ Object



37
38
39
40
41
# File 'lib/chillout/client.rb', line 37

def enqueue(creations)
  start_worker
  @logger.info "Creations were enqueued."
  @queue << creations
end

#start_workerObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chillout/client.rb', line 47

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

#worker_running?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/chillout/client.rb', line 43

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