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
24
25
# 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
  @deployment_uri = URI.parse(@configuration.deployment_endpoint).freeze
  @use_ssl        = @uri.scheme == 'https'
  @deployment_use_ssl = @deployment_uri.scheme == 'https'
  @auth_header    = "Bearer #{@configuration.api_key}".freeze
end

Instance Method Details

#flush(timeout: 2) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/logister/client.rb', line 42

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



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

def publish(payload)
  return false unless ready?

  return publish_sync(payload) unless @configuration.async

  ensure_worker_started
  enqueue(payload)
end

#publish_deployment(payload) ⇒ Object



36
37
38
39
40
# File 'lib/logister/client.rb', line 36

def publish_deployment(payload)
  return false unless ready?

  publish_deployment_sync(payload)
end

#shutdownObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/logister/client.rb', line 55

def shutdown
  return true unless @configuration.async

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