From 92754a35b40734d5bcd18e5ac9693e3131a2dfa3 Mon Sep 17 00:00:00 2001 From: Joshua Liebowitz Date: Fri, 20 Apr 2018 09:44:31 -0700 Subject: [PATCH] add some extra exception handling --- app/shared/github_handler.rb | 40 +++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/app/shared/github_handler.rb b/app/shared/github_handler.rb index 5aae28fb..11dc23d6 100644 --- a/app/shared/github_handler.rb +++ b/app/shared/github_handler.rb @@ -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) @@ -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