Skip to content

Commit

Permalink
Merge pull request #4 from launchdarkly/jko/respawn-worker
Browse files Browse the repository at this point in the history
Drop the flush interval down to 10 seconds, and try to respawn the worker if it dies
  • Loading branch information
jkodumal committed Dec 16, 2014
2 parents c3f66f3 + 941a1d8 commit d9c9826
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/ldclient-rb/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def self.default_store
end

def self.default_flush_interval
30
10
end

def self.default_logger
Expand Down
49 changes: 30 additions & 19 deletions lib/ldclient-rb/ldclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,38 @@ def initialize(api_key, config = Config.default)
builder.adapter Faraday.default_adapter
end

@worker = create_worker()
end

def create_worker()
Thread.new do
while true do
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
begin
events = []
num_events = @queue.length()
num_events.times do
events << @queue.pop()
end
if res.status != 200
@config.logger.error("[LDClient] Unexpected status code while processing events: #{res.status}")

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
end
if res.status != 200
@config.logger.error("[LDClient] Unexpected status code while processing events: #{res.status}")
end
end
end

sleep(@config.flush_interval)
sleep(@config.flush_interval)
rescue Exception => exn
@config.logger.error("[LDClient] Unexpected exception in create_worker: #{exn.message}")
end
end
end

end

#
Expand Down Expand Up @@ -104,6 +111,10 @@ def add_event(event)
if @queue.length() < @config.capacity
event[:creationDate] = (Time.now.to_f * 1000).to_i
@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
Expand Down Expand Up @@ -242,7 +253,7 @@ def evaluate(feature, user)

end

private :add_event, :get_flag_int, :param_for_user, :match_target?, :match_variation?, :evaluate
private :add_event, :get_flag_int, :param_for_user, :match_target?, :match_variation?, :evaluate, :create_worker


end
Expand Down

0 comments on commit d9c9826

Please sign in to comment.