Class: Logister::Client

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

Defined Under Namespace

Classes: RequestError, UnsupportedBatchEndpoint

Constant Summary collapse

CONTENT_TYPE =
'application/json'
BATCH_CONTENT_TYPE =
'application/x-ndjson'
UNSUPPORTED_BATCH_STATUSES =
%w[404 405 415 501].freeze

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/logister/client.rb', line 29

def initialize(configuration)
  @configuration = configuration
  @worker_mutex  = Mutex.new
  @queue         = SizedQueue.new(@configuration.queue_size)
  @worker        = nil
  @running       = false
  @pending_mutex = Mutex.new
  @pending_condition = ConditionVariable.new
  @pending_count = 0

  # 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
  @batch_uri      = URI.parse(@configuration.batch_endpoint).freeze
  @deployment_uri = URI.parse(@configuration.deployment_endpoint).freeze
  @use_ssl        = @uri.scheme == 'https'
  @batch_use_ssl  = @batch_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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/logister/client.rb', line 67

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

  deadline = monotonic_now + timeout
  @pending_mutex.synchronize do
    while @pending_count.positive?
      remaining = deadline - monotonic_now
      return false unless remaining.positive?

      @pending_condition.wait(@pending_mutex, [remaining, 0.05].min)
    end
  end

  true
end

#publish(payload) ⇒ Object



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

def publish(payload)
  return false unless ready?

  payload = with_stable_uuid(payload)

  return publish_sync(payload) unless @configuration.async

  ensure_worker_started
  enqueue(payload)
end

#publish_deployment(payload) ⇒ Object



61
62
63
64
65
# File 'lib/logister/client.rb', line 61

def publish_deployment(payload)
  return false unless ready?

  publish_deployment_sync(payload)
end

#shutdownObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/logister/client.rb', line 83

def shutdown
  return true unless @configuration.async

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