Class: DevCycle::EventQueue

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/devcycle-ruby-server-sdk/localbucketing/event_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(sdkKey, options, local_bucketing) ⇒ EventQueue

Returns a new instance of EventQueue.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/devcycle-ruby-server-sdk/localbucketing/event_queue.rb', line 12

def initialize(sdkKey, options, local_bucketing)
  @sdkKey = sdkKey
  @client_uuid = SecureRandom.uuid
  @events_api_uri = options.events_api_uri
  @logger = options.logger
  @event_flush_interval_ms = options.event_flush_interval_ms
  @flush_event_queue_size = options.flush_event_queue_size
  @max_event_queue_size = options.max_event_queue_size
  @flush_timer_task = Concurrent::TimerTask.new(
    execution_interval: @event_flush_interval_ms.fdiv(1000)
  ) {
    flush_events
  }
  @flush_timer_task.execute
  @flush_mutex = Mutex.new
  @local_bucketing = local_bucketing
  @local_bucketing.init_event_queue(@client_uuid, options)

end

Instance Method Details

#closeObject



32
33
34
35
# File 'lib/devcycle-ruby-server-sdk/localbucketing/event_queue.rb', line 32

def close
  @flush_timer_task.shutdown
  flush_events
end

#flush_eventsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/devcycle-ruby-server-sdk/localbucketing/event_queue.rb', line 38

def flush_events
  @flush_mutex.synchronize do
    payloads = @local_bucketing.flush_event_queue
    if payloads.length == 0
      return
    end
    eventCount = payloads.reduce(0) { |sum, payload| sum + payload.eventCount }
    @logger.debug("DevCycle: Flushing #{eventCount} event(s) for #{payloads.length} user(s)")

    payloads.each do |payload|
      begin
        response = Typhoeus.post(
          @events_api_uri + '/v1/events/batch',
          headers: { 'Authorization': @sdkKey, 'Content-Type': 'application/json' },
          body: { 'batch': payload.records }.to_json
        )
        if response.code != 201
          @logger.error("Error publishing events, status: #{response.code}, body: #{response.return_message}")
          @local_bucketing.on_payload_failure(payload.payloadId, response.code >= 500)
        else
          @logger.debug("DevCycle: Flushed #{eventCount} event(s), for #{payload.records.length} user(s)")
          @local_bucketing.on_payload_success(payload.payloadId)
        end
      rescue => e
        @logger.error("DevCycle: Error Flushing Events response message: #{e.message}")
        @local_bucketing.on_payload_failure(payload.payloadId, false)
      end
    end
  end
  nil
end

#max_event_queue_size_reached?Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
# File 'lib/devcycle-ruby-server-sdk/localbucketing/event_queue.rb', line 94

def max_event_queue_size_reached?
  queue_size = @local_bucketing.check_event_queue_size()
  if queue_size >= @flush_event_queue_size
    flush_events
    if queue_size >= @max_event_queue_size
      return true
    end
  end
  false
end

#queue_aggregate_event(event, bucketed_config) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/devcycle-ruby-server-sdk/localbucketing/event_queue.rb', line 83

def queue_aggregate_event(event, bucketed_config)
  if max_event_queue_size_reached?
    @logger.warn("Max event queue size reached, dropping event: #{event}")
    return
  end

  @local_bucketing.queue_aggregate_event(event, bucketed_config)
  nil
end

#queue_event(user, event) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/devcycle-ruby-server-sdk/localbucketing/event_queue.rb', line 72

def queue_event(user, event)
  if max_event_queue_size_reached?
    @logger.warn("Max event queue size reached, dropping event: #{event}")
    return
  end

  @local_bucketing.queue_event(user, event)
  nil
end