Skip to content

Commit 334d6fa

Browse files
committed
Add retry module to docs.
1 parent 9a75198 commit 334d6fa

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

docs/source/api/retry.rst

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Retry Module
2+
============
3+
4+
.. automodule:: judge0.retry
5+
:members:
6+
:member-order: bysource

docs/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Every contribution, big or small, is valuable!
5959
api/submission
6060
api/clients
6161
api/types
62+
api/retry
6263

6364
.. toctree::
6465
:caption: Getting Involved

pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,18 @@ test = [
4848
docs = ["sphinx==7.4.7"]
4949

5050
[tool.flake8]
51-
docstring-convention = "numpy"
5251
extend-ignore = [
5352
'D100',
5453
'D101',
5554
'D102',
5655
'D103',
5756
'D104',
5857
'D105',
58+
'D107',
5959
'D205',
60+
"D209",
6061
'D400',
6162
'F821',
6263
]
64+
docstring-convention = "numpy"
6365
max-line-length = 88

src/judge0/retry.py

+47-3
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,106 @@
33

44

55
class RetryStrategy(ABC):
6+
"""Abstract base class that defines the interface for any retry strategy.
7+
8+
See :obj:`MaxRetries`, :obj:`MaxWaitTime`, and :obj:`RegularPeriodRetry` for
9+
example implementations.
10+
"""
11+
612
@abstractmethod
713
def is_done(self) -> bool:
14+
"""Check if the retry strategy has exhausted its retries."""
815
pass
916

1017
@abstractmethod
1118
def wait(self) -> None:
19+
"""Delay implementation before the next retry attempt."""
1220
pass
1321

22+
@abstractmethod
1423
def step(self) -> None:
24+
"""Update internal attributes of the retry strategy."""
1525
pass
1626

1727

1828
class MaxRetries(RetryStrategy):
1929
"""Check for submissions status every 100 ms and retry a maximum of
20-
`max_retries` times."""
30+
`max_retries` times.
31+
32+
Parameters
33+
----------
34+
max_retries : int
35+
Max number of retries.
36+
"""
2137

2238
def __init__(self, max_retries: int = 20):
39+
if max_retries < 1:
40+
raise ValueError("max_retries must be at least 1.")
2341
self.n_retries = 0
2442
self.max_retries = max_retries
2543

2644
def step(self):
45+
"""Increment the number of retries by one."""
2746
self.n_retries += 1
2847

2948
def wait(self):
49+
"""Wait for 0.1 seconds between retries."""
3050
time.sleep(0.1)
3151

3252
def is_done(self) -> bool:
53+
"""Check if the number of retries is bigger or equal to specified
54+
maximum number of retries."""
3355
return self.n_retries >= self.max_retries
3456

3557

3658
class MaxWaitTime(RetryStrategy):
3759
"""Check for submissions status every 100 ms and wait for all submissions
38-
a maximum of `max_wait_time` (seconds)."""
60+
a maximum of `max_wait_time` (seconds).
61+
62+
Parameters
63+
----------
64+
max_wait_time_sec : float
65+
Maximum waiting time (in seconds).
66+
"""
3967

4068
def __init__(self, max_wait_time_sec: float = 5 * 60):
4169
self.max_wait_time_sec = max_wait_time_sec
4270
self.total_wait_time = 0
4371

4472
def step(self):
73+
"""Add 0.1 seconds to total waiting time."""
4574
self.total_wait_time += 0.1
4675

4776
def wait(self):
77+
"""Wait (sleep) for 0.1 seconds."""
4878
time.sleep(0.1)
4979

5080
def is_done(self):
81+
"""Check if the total waiting time is bigger or equal to the specified
82+
maximum waiting time."""
5183
return self.total_wait_time >= self.max_wait_time_sec
5284

5385

5486
class RegularPeriodRetry(RetryStrategy):
55-
"""Check for submissions status periodically for indefinite amount of time."""
87+
"""Check for submissions status periodically for indefinite amount of time.
88+
89+
Parameters
90+
----------
91+
wait_time_sec : float
92+
Wait time between retries (in seconds).
93+
"""
5694

5795
def __init__(self, wait_time_sec: float = 0.1):
5896
self.wait_time_sec = wait_time_sec
5997

6098
def wait(self):
99+
"""Wait for `wait_time_sec` seconds."""
61100
time.sleep(self.wait_time_sec)
62101

63102
def is_done(self) -> bool:
103+
"""Return False, as this retry strategy is indefinite."""
64104
return False
105+
106+
def step(self) -> None:
107+
"""Satisfy the interface with a dummy implementation."""
108+
pass

0 commit comments

Comments
 (0)