Class: Itly::Plugin::Iteratively::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/itly/plugin/iteratively/client.rb

Overview

HTTP client for the plugin requests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, api_key:, logger:, flush_queue_size:, batch_size:, flush_interval_ms:, max_retries:, retry_delay_min:, retry_delay_max:, omit_values:, branch:, version:) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/itly/plugin/iteratively/client.rb', line 17

def initialize(
  url:, api_key:, logger:, flush_queue_size:, batch_size:, flush_interval_ms:, max_retries:,
  retry_delay_min:, retry_delay_max:, omit_values:, branch:, version:
)
  @buffer = ::Concurrent::Array.new
  @runner = @scheduler = nil

  @api_key = api_key
  @url = url
  @logger = logger
  @flush_queue_size = flush_queue_size
  @batch_size = batch_size
  @flush_interval_ms = flush_interval_ms
  @max_retries = max_retries
  @retry_delay_min = retry_delay_min
  @retry_delay_max = retry_delay_max
  @omit_values = omit_values
  @branch = branch
  @version = version

  # Start the scheduler
  start_scheduler
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



14
15
16
# File 'lib/itly/plugin/iteratively/client.rb', line 14

def api_key
  @api_key
end

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



14
15
16
# File 'lib/itly/plugin/iteratively/client.rb', line 14

def batch_size
  @batch_size
end

#branchObject (readonly)

Returns the value of attribute branch.



14
15
16
# File 'lib/itly/plugin/iteratively/client.rb', line 14

def branch
  @branch
end

#flush_interval_msObject (readonly)

Returns the value of attribute flush_interval_ms.



14
15
16
# File 'lib/itly/plugin/iteratively/client.rb', line 14

def flush_interval_ms
  @flush_interval_ms
end

#flush_queue_sizeObject (readonly)

Returns the value of attribute flush_queue_size.



14
15
16
# File 'lib/itly/plugin/iteratively/client.rb', line 14

def flush_queue_size
  @flush_queue_size
end

#loggerObject (readonly)

Returns the value of attribute logger.



14
15
16
# File 'lib/itly/plugin/iteratively/client.rb', line 14

def logger
  @logger
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



14
15
16
# File 'lib/itly/plugin/iteratively/client.rb', line 14

def max_retries
  @max_retries
end

#omit_valuesObject (readonly)

Returns the value of attribute omit_values.



14
15
16
# File 'lib/itly/plugin/iteratively/client.rb', line 14

def omit_values
  @omit_values
end

#retry_delay_maxObject (readonly)

Returns the value of attribute retry_delay_max.



14
15
16
# File 'lib/itly/plugin/iteratively/client.rb', line 14

def retry_delay_max
  @retry_delay_max
end

#retry_delay_minObject (readonly)

Returns the value of attribute retry_delay_min.



14
15
16
# File 'lib/itly/plugin/iteratively/client.rb', line 14

def retry_delay_min
  @retry_delay_min
end

#urlObject (readonly)

Returns the value of attribute url.



14
15
16
# File 'lib/itly/plugin/iteratively/client.rb', line 14

def url
  @url
end

#versionObject (readonly)

Returns the value of attribute version.



14
15
16
# File 'lib/itly/plugin/iteratively/client.rb', line 14

def version
  @version
end

Instance Method Details

#flushObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/itly/plugin/iteratively/client.rb', line 49

def flush
  # Case: the runner is on, cannot call flush again
  return unless runner_complete?

  # Exit if there is nothing to do
  return if @buffer.empty?

  # Extract the current content of the buffer for processing
  processing = @buffer.each_slice(@batch_size).to_a
  @buffer.clear

  # Run in the background
  @runner = Concurrent::Future.new do
    processing.each do |batch|
      # Initialization before the loop starts
      tries = 0

      loop do
        # Count the number of tries
        tries += 1

        # Case: successfully sent
        break if post_models batch

        # Case: could not sent and reached maximum number of allowed tries
        if tries >= @max_retries
          # Log
          logger&.error 'Iteratively::Client: flush() reached maximum number of tries. '\
            "#{batch.count} events won't be sent to the server"

          # Discard the list of event in the batch queue
          break

        # Case: could not sent and wait before retrying
        else
          sleep delay_before_next_try(tries)
        end
      end
    end
  end

  @runner.execute
end

#shutdown(force: false) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/itly/plugin/iteratively/client.rb', line 93

def shutdown(force: false)
  @scheduler&.cancel

  if force
    @runner&.cancel
    return
  end

  @max_retries = 0
  flush
  @runner&.wait_or_cancel @retry_delay_min
end

#track(type:, event:, properties:, validation:) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/itly/plugin/iteratively/client.rb', line 41

def track(type:, event:, properties:, validation:)
  @buffer << ::Itly::Plugin::Iteratively::TrackModel.new(
    omit_values: omit_values, type: type, event: event, properties: properties, validation: validation
  )

  flush if buffer_full?
end