Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
add some extra exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Liebowitz committed Apr 20, 2018
1 parent 2e2b56c commit 92754a3
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions app/shared/github_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,37 @@ def self.included(klass)

def github_action(client, &block)
if client.kind_of?(Octokit::Client)
if client.rate_limit!.remaining.zero?
sleep_time = client.rate_limit!.resets_in
logger.debug("Rate Limit exceeded, sleeping for #{sleep_time} seconds")
sleep(sleep_time)
# `rate_limit_retry_count` retains the variables through iterations so we assign to 0 the first time.
rate_limit_retry_count ||= 0
begin
if client.rate_limit!.remaining.zero?
rate_limit_reset_time_length = client.rate_limit!.resets_in
logger.debug("Rate Limit exceeded, sleeping for #{rate_limit_reset_time_length} seconds")
sleep(rate_limit_reset_time_length)
end
rescue Octokit::Unauthorized => ex # Maybe the token does not give access to rate limits.
logger.error("Your GitHub Personal Auth Token is not unauthorized to check the rate_limit")
logger.error(ex)
# We want to die now, since this is a server config issue
# Ultimately, this shouldn't kill the server, but rather, send a notification
# TODO: accomplish the above ^
raise ex
rescue Octokit::ServerError, Octokit::TooManyRequests, Faraday::ConnectionFailed => ex
if (rate_limit_retry_count += 1) < 5
rate_limit_sleep_length = 2**rate_limit_retry_count
logger.debug("Unable to get rate limit, sleeping for #{rate_limit_sleep_length} seconds and retrying")
logger.debug(ex)
sleep(rate_limit_sleep_length)
retry
end
logger.debug("Unable to get rate limit after retrying multiple time, failing")
# Ultimately, this shouldn't kill the server, but rather, send a notification
# TODO: accomplish the above ^
raise ex
end
end

# `retry` retains the variables through iterations so we assign to 0 the first time.
# `retry_count` retains the variables through iterations so we assign to 0 the first time.
retry_count ||= 0
begin
return block.call(client)
Expand All @@ -58,13 +81,20 @@ def github_action(client, &block)
# exponential backoff
sleep_length = 2**retry_count
logger.debug("A GitHub action failed, sleeping for #{sleep_length} seconds and retrying")
logger.debug(ex)
sleep(sleep_length)
retry
end
logger.debug("Unable to perform GitHub action after retrying multiple time, failing")
# Ultimately, this shouldn't kill the server, but rather, send a notification
# TODO: accomplish the above ^
raise ex
rescue Octokit::Unauthorized => ex # Maybe the token does not give access to rate limits.
logger.error("Your GitHub Personal Auth Token is unauthorized to perform the github action")
logger.error(ex)
# Ultimately, this shouldn't kill the server, but rather, send a notification
# TODO: accomplish the above ^
raise ex
end
end
end
Expand Down

0 comments on commit 92754a3

Please sign in to comment.