Class: LaunchDarkly::EventProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/events.rb

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key, config) ⇒ EventProcessor

Returns a new instance of EventProcessor.



7
8
9
10
11
12
13
14
15
# File 'lib/ldclient-rb/events.rb', line 7

def initialize(sdk_key, config)
  @queue = Queue.new
  @sdk_key = sdk_key
  @config = config
  @serializer = EventSerializer.new(config)
  @client = Faraday.new
  @stopped = Concurrent::AtomicBoolean.new(false)
  @worker = create_worker if @config.send_events
end

Instance Method Details

#add_event(event) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ldclient-rb/events.rb', line 76

def add_event(event)
  return if @offline || !@config.send_events || @stopped.value

  if @queue.length < @config.capacity
    event[:creationDate] = (Time.now.to_f * 1000).to_i
    @config.logger.debug { "[LDClient] Enqueueing event: #{event.to_json}" }
    @queue.push(event)

    if !@worker.alive?
      @worker = create_worker
    end
  else
    @config.logger.warn { "[LDClient] Exceeded event queue capacity. Increase capacity to avoid dropping events." }
  end
end

#alive?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/ldclient-rb/events.rb', line 17

def alive?
  !@stopped.value
end

#flushObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ldclient-rb/events.rb', line 61

def flush
  return if @offline || !@config.send_events
  events = []
  begin
    loop do
      events << @queue.pop(true)
    end
  rescue ThreadError
  end

  if !events.empty? && !@stopped.value
    post_flushed_events(events)
  end
end

#stopObject



21
22
23
24
25
26
27
28
# File 'lib/ldclient-rb/events.rb', line 21

def stop
  if @stopped.make_true
    # There seems to be no such thing as "close" in Faraday: https://github.com/lostisland/faraday/issues/241
    if !@worker.nil? && @worker.alive?
      @worker.raise "shutting down client"
    end
  end
end