Skip to content

Commit

Permalink
Added retry method for dealing with failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Cawllec committed Jan 5, 2018
1 parent d0b1b08 commit dd0cdd4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/bugsnag/delivery/synchronous.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def deliver(url, body, configuration, options={})
configuration.debug("Request to #{url} completed, status: #{response.code}")
success = options[:success] || '200'
if !(response.code == success)
configuration.warn("Notifications to #{url} was reported unsuccessful with code #{response.code}")
configuration.warn("Notification to #{url} was reported unsuccessful with code #{response.code}")
if options[:retry]
options[:retry].call
end
end
rescue StandardError => e
# KLUDGE: Since we don't re-raise http exceptions, this breaks rspec
Expand Down
34 changes: 29 additions & 5 deletions lib/bugsnag/session_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module Bugsnag
class SessionTracker

THREAD_SESSION = "bugsnag_session"
TIME_THRESHOLD = 2
TIME_THRESHOLD = 60
FALLBACK_TIME = 300
MAXIMUM_SESSION_COUNT = 50
MAXIMUM_SESSION_COUNT = 200
SESSION_PAYLOAD_VERSION = "1.0"

attr_reader :session_counts
Expand Down Expand Up @@ -105,6 +105,24 @@ def reset_delivery_thread
end
end

def merge_sessions(sessions)
sessions.each do |session|
@session_counts[session[:startedAt]] ||= 0
@session_counts[session[:startedAt]] += session[:sessionsStarted]
end
trim_sessions if @session_counts.size > MAXIMUM_SESSION_COUNT
end

def trim_sessions
counts = @session_counts.sort_by { |min, count| min }
while counts.size > MAXIMUM_SESSION_COUNT
current = counts.first
counts.slice!(0)
counts.first()[1] += current[1]
end
@session_counts = counts.to_h
end

def deliver(sessionCounts)
if sessionCounts.length == 0
@config.debug("No sessions to deliver")
Expand Down Expand Up @@ -149,9 +167,15 @@ def deliver(sessionCounts)
"Bugsnag-Payload-Version" => SESSION_PAYLOAD_VERSION
}

options = {:headers => headers, :success => '202'}
@last_sent = Time.now
Bugsnag::Delivery[@config.delivery_method].deliver(@config.session_endpoint, payload, @config, options)
retry_proc = proc { merge_sessions(sessionCounts) }

options = {:headers => headers, :success => '202', :retry => retry_proc}
queued = Bugsnag::Delivery[@config.delivery_method].deliver(@config.session_endpoint, payload, @config, options)
if queued.nil?
merge_sessions(sessionCounts)
else
@last_sent = Time.now
end
end
end
end

0 comments on commit dd0cdd4

Please sign in to comment.