24 lines
637 B
Python
24 lines
637 B
Python
import sys
|
|
import typing
|
|
|
|
from loop_claude.effects import Effects
|
|
from loop_claude.failure import Failure
|
|
from loop_claude.stop import Stop
|
|
|
|
ATTEMPTS = 3
|
|
RETRY_DELAY_SECONDS = 60
|
|
T = typing.TypeVar("T")
|
|
|
|
|
|
def retried(who: str, attempt: typing.Callable[[], T | Failure], effects: Effects) -> T | Stop:
|
|
failures = 0
|
|
while True:
|
|
failures += 1
|
|
result = attempt()
|
|
if not isinstance(result, Failure):
|
|
return result
|
|
if failures == ATTEMPTS:
|
|
return Stop(1, f"the {who} failed three times running: {result.reason}")
|
|
print(f"retrying the {who} in a minute: {result.reason}", file=sys.stderr)
|
|
effects.sleep(RETRY_DELAY_SECONDS)
|