From ae5d2225a00d7fd8c3816b93d5fe3a06f77096f6 Mon Sep 17 00:00:00 2001 From: John Kodumal Date: Fri, 6 Feb 2015 10:35:01 -0800 Subject: [PATCH 1/2] Add a flush mechanism --- lib/ldclient-rb/ldclient.rb | 45 ++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index bf7143cc..65f5fb43 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -39,30 +39,35 @@ def initialize(api_key, config = Config.default) @worker = create_worker() end + def flush() + 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 + 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 From bf9ac1ec87ad3d0b7f8c382026722dcaf1584bae Mon Sep 17 00:00:00 2001 From: John Kodumal Date: Fri, 6 Feb 2015 15:55:17 -0800 Subject: [PATCH 2/2] Do a non-blocking pop, but catch any exceptions if the queue is empty --- lib/ldclient-rb/ldclient.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index 65f5fb43..1704841a 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -42,10 +42,14 @@ def initialize(api_key, config = Config.default) def flush() events = [] num_events = @queue.length() - num_events.times do - events << @queue.pop() + 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|