Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added minimal logging #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion retrying.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import random
import six
import sys
Expand Down Expand Up @@ -83,6 +83,7 @@ def __init__(
wait_jitter_max=None,
before_attempts=None,
after_attempts=None,
logger=None,
):

self._stop_max_attempt_number = (
Expand Down Expand Up @@ -110,6 +111,14 @@ def __init__(
self._wait_jitter_max = 0 if wait_jitter_max is None else wait_jitter_max
self._before_attempts = before_attempts
self._after_attempts = after_attempts

if logger in (True, None):
self._logger = logging.getLogger(__name__)
if logger is None:
self._logger.addHandler(logging.NullHandler())
self._logger.propagate = False
elif logger:
self._logger = logger

# TODO add chaining of stop behaviors
# stop behavior
Expand Down Expand Up @@ -256,6 +265,7 @@ def call(self, fn, *args, **kwargs):
if not self.should_reject(attempt):
return attempt.get(self._wrap_exception)

self._logger.warn(attempt)
if self._after_attempts:
self._after_attempts(attempt_number)

Expand All @@ -271,6 +281,7 @@ def call(self, fn, *args, **kwargs):
if self._wait_jitter_max:
jitter = random.random() * self._wait_jitter_max
sleep = sleep + max(0, jitter)
self._logger.info(f"Retrying in {sleep / 1000.0:.2f} seconds.")
time.sleep(sleep / 1000.0)

attempt_number += 1
Expand Down