Class: Logister::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/logister/client.rb

Constant Summary collapse

CONTENT_TYPE =
'application/json'

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/logister/client.rb', line 11

def initialize(configuration)
  @configuration = configuration
  @worker_mutex  = Mutex.new
  @queue         = SizedQueue.new(@configuration.queue_size)
  @worker        = nil
  @running       = false

  # Cache values that are static for the lifetime of this client so we
  # don't allocate on every send_request call.
  @uri         = URI.parse(@configuration.endpoint).freeze
  @use_ssl     = @uri.scheme == 'https'
  @auth_header = "Bearer #{@configuration.api_key}".freeze
end

Instance Method Details

#flush(timeout: 2) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/logister/client.rb', line 34

def flush(timeout: 2)
  return true unless @configuration.async

  deadline = monotonic_now + timeout
  until @queue.empty?
    return false if monotonic_now > deadline

    sleep(0.01)
  end

  true
end

#publish(payload) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/logister/client.rb', line 25

def publish(payload)
  return false unless ready?

  return publish_sync(payload) unless @configuration.async

  ensure_worker_started
  enqueue(payload)
end

#shutdownObject



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

def shutdown
  return true unless @configuration.async

  @running = false
  begin
    @queue.push(nil)
  rescue StandardError
    nil
  end
  @worker&.join(1)
  @worker = nil
  true
end