-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Spinach more reliable to connection errors
- Loading branch information
Showing
4 changed files
with
79 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,27 @@ | ||
from logging import Logger | ||
from threading import Event | ||
import time | ||
from typing import Callable | ||
|
||
from redis import ConnectionError, TimeoutError | ||
|
||
|
||
def human_duration(duration_seconds: float) -> str: | ||
"""Convert a duration in seconds into a human friendly string.""" | ||
if duration_seconds < 0.001: | ||
return '0 ms' | ||
if duration_seconds < 1: | ||
return '{} ms'.format(int(duration_seconds * 1000)) | ||
return '{} s'.format(int(duration_seconds)) | ||
|
||
|
||
def run_forever(func: Callable, must_stop: Event, logger: Logger): | ||
while not must_stop.is_set(): | ||
try: | ||
func() | ||
except (ConnectionError, TimeoutError) as e: | ||
logger.warning('Connection issue: %s', e) | ||
time.sleep(10) | ||
except Exception: | ||
logger.exception('Unexpected error') | ||
time.sleep(10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters