diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index bf7143cc..1704841a 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -39,30 +39,39 @@ def initialize(api_key, config = Config.default) @worker = create_worker() end + def flush() + events = [] + num_events = @queue.length() + begin + num_events.times do + events << @queue.pop(true) + end + rescue + end + + + if !events.empty?() + res = + @client.post (@config.base_uri + "/api/events/bulk") do |req| + req.headers['Authorization'] = 'api_key ' + @api_key + req.headers['User-Agent'] = 'RubyClient/' + LaunchDarkly::VERSION + req.headers['Content-Type'] = 'application/json' + req.body = events.to_json + req.options.timeout = @config.read_timeout + req.options.open_timeout = @config.connect_timeout + end + if res.status != 200 + @config.logger.error("[LDClient] Unexpected status code while processing events: #{res.status}") + end + end + end + + def create_worker() Thread.new do while true do begin - events = [] - num_events = @queue.length() - num_events.times do - events << @queue.pop() - end - - if !events.empty?() - res = - @client.post (@config.base_uri + "/api/events/bulk") do |req| - req.headers['Authorization'] = 'api_key ' + @api_key - req.headers['User-Agent'] = 'RubyClient/' + LaunchDarkly::VERSION - req.headers['Content-Type'] = 'application/json' - req.body = events.to_json - req.options.timeout = @config.read_timeout - req.options.open_timeout = @config.connect_timeout - end - if res.status != 200 - @config.logger.error("[LDClient] Unexpected status code while processing events: #{res.status}") - end - end + flush() sleep(@config.flush_interval) rescue Exception => exn