Class: LaunchDarkly::EventPayloadSendTask

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

Instance Method Summary collapse

Instance Method Details

#run(sdk_key, config, client, payload, formatter) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/ldclient-rb/events.rb', line 287

def run(sdk_key, config, client, payload, formatter)
  events_out = formatter.make_output_events(payload.events, payload.summary)
  res = nil
  body = events_out.to_json
  (0..1).each do |attempt|
    if attempt > 0
      config.logger.warn { "[LDClient] Will retry posting events after 1 second" }
      sleep(1)
    end
    begin
      config.logger.debug { "[LDClient] sending #{events_out.length} events: #{body}" }
      res = client.post (config.events_uri + "/bulk") do |req|
        req.headers["Authorization"] = sdk_key
        req.headers["User-Agent"] = "RubyClient/" + LaunchDarkly::VERSION
        req.headers["Content-Type"] = "application/json"
        req.headers["X-LaunchDarkly-Event-Schema"] = CURRENT_SCHEMA_VERSION.to_s
        req.body = body
        req.options.timeout = config.read_timeout
        req.options.open_timeout = config.connect_timeout
      end
    rescue StandardError => exn
      config.logger.warn { "[LDClient] Error flushing events: #{exn.inspect}." }
      next
    end
    if res.status < 200 || res.status >= 300
      config.logger.error { "[LDClient] Unexpected status code while processing events: #{res.status}" }
      if res.status >= 500
        next
      end
    end
    break
  end
  # used up our retries, return the last response if any
  res
end