Skip to content

Added minimal logging #6

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
6 changes: 5 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,7 @@ 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
self._logger = logging.getLogger(__name__) if logger is None else logger
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] This is okay, but I hate having a library change behavior wrt logging.

Maybe something like:

if logger in (True, None):
    self._logger = logging.getLogger(__name__)
    if logger is None:
       # -- Turn this into a Null Logger
        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 +258,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 @@ -268,6 +271,7 @@ def call(self, fn, *args, **kwargs):
raise RetryError(attempt)
else:
sleep = self.wait(attempt_number, delay_since_first_attempt_ms)
self._logger.info(f"Retrying in {sleep / 1000.0:.2f} seconds.")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] This line should be below the if self._wait_jitter_max: clause so that it reflects the actual wait time.

Copy link
Author

@frnhr frnhr Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: NM, I read it to quickly 😁. Moving below 👍

if self._wait_jitter_max:
jitter = random.random() * self._wait_jitter_max
sleep = sleep + max(0, jitter)
Expand Down