Skip to content

Commit 6fd4a7a

Browse files
committed
Docstrings, timeout, loop_sleep
1 parent da7ced8 commit 6fd4a7a

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-20
lines changed

src/cluster_tasks/controller_sync.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,20 @@ def main():
2222
backend_name = scenarios_config.get("API.backend", "https")
2323
register_backends(backend_name)
2424
ext_api = ProxmoxAPI(backend_name=backend_name, backend_type="sync")
25-
with ext_api as api:
26-
node_tasks = NodeTasksSync(api=api) # Pass the api instance to NodeTasksAsync
27-
for v in scenarios_config.get("Scenarios").values():
28-
scenario_file = v.get("file")
29-
config = v.get("config")
30-
# Create scenario instance using the factory
31-
scenario = ScenarioFactory.create_scenario(scenario_file, config)
32-
# Run the scenario
33-
scenario.run(node_tasks)
25+
try:
26+
with ext_api as api:
27+
node_tasks = NodeTasksSync(
28+
api=api, timeout=120
29+
) # Pass the api instance to NodeTasksAsync
30+
for v in scenarios_config.get("Scenarios").values():
31+
scenario_file = v.get("file")
32+
config = v.get("config")
33+
# Create scenario instance using the factory
34+
scenario = ScenarioFactory.create_scenario(scenario_file, config)
35+
# Run the scenario
36+
scenario.run(node_tasks)
37+
except Exception as e:
38+
logger.error(f"MAIN: {e}")
3439

3540

3641
if __name__ == "__main__":
Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,67 @@
11
from datetime import timedelta
2-
32
from ext_api.proxmox_api import ProxmoxAPI
43

54

65
class BaseTasks:
7-
TIMEOUT = 60
8-
LOOP_SLEEP = 2
6+
"""
7+
BaseTasks class that provides common functionality for tasks interacting
8+
with the Proxmox API. It manages timeouts, loop sleeps, and provides utility
9+
methods for formatting durations.
10+
11+
Attributes:
12+
timeout (int): The default timeout in seconds for tasks.
13+
loop_sleep (int): The default sleep duration between task loops in seconds.
14+
_api (ProxmoxAPI): The Proxmox API instance used for interacting with Proxmox.
15+
"""
16+
17+
timeout = 60
18+
loop_sleep = 2
19+
20+
def __init__(
21+
self, api: ProxmoxAPI, timeout: int = timeout, loop_sleep: int = loop_sleep
22+
):
23+
"""
24+
Initializes the BaseTasks class with the given Proxmox API instance and optional
25+
timeout and loop sleep values.
926
10-
def __init__(self, api: ProxmoxAPI):
27+
Args:
28+
api (ProxmoxAPI): The Proxmox API instance for making API calls.
29+
timeout (int, optional): The timeout value for tasks (default is 60).
30+
loop_sleep (int, optional): The sleep time between loops in seconds (default is 2).
31+
"""
1132
self._api: ProxmoxAPI = api
33+
self.timeout = timeout
34+
self.loop_sleep = loop_sleep
1235

1336
@property
1437
def api(self):
38+
"""
39+
Gets the Proxmox API instance.
40+
41+
Returns:
42+
ProxmoxAPI: The Proxmox API instance.
43+
"""
1544
return self._api
1645

1746
@api.setter
1847
def api(self, value):
48+
"""
49+
Sets a new Proxmox API instance.
50+
51+
Args:
52+
value (ProxmoxAPI): The new Proxmox API instance to set.
53+
"""
1954
self._api = value
2055

2156
@staticmethod
2257
def format_duration(seconds: float | int) -> str:
23-
"""Format a duration (in seconds) as HH:MM:SS."""
58+
"""
59+
Formats a duration (in seconds) as a string in the format HH:MM:SS.
60+
61+
Args:
62+
seconds (float or int): The duration in seconds to format.
63+
64+
Returns:
65+
str: The formatted duration as HH:MM:SS.
66+
"""
2467
return str(timedelta(seconds=seconds)).split(".")[0]

src/cluster_tasks/tasks/node_tasks_base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ def wait_task_done_sync(self, node: str, upid: str) -> bool:
5353
return True
5454
duration = time.time() - start_time
5555
formatted_duration = self.format_duration(duration)
56-
formatted_timeout = self.format_duration(self.TIMEOUT)
56+
formatted_timeout = self.format_duration(self.timeout)
5757
logger.info(
5858
f"Waiting for task to finish... [ {formatted_duration} / {formatted_timeout} ]"
5959
)
60-
time.sleep(self.LOOP_SLEEP)
61-
if time.time() - start_time > self.TIMEOUT:
60+
time.sleep(self.loop_sleep)
61+
if time.time() - start_time > self.timeout:
6262
logger.warning(
6363
f"Timeout reached while waiting for task to finish. {upid=}"
6464
)
@@ -75,12 +75,12 @@ async def wait_task_done_async(self, node: str, upid: str) -> bool:
7575
return True
7676
duration = time.time() - start_time
7777
formatted_duration = self.format_duration(duration)
78-
formatted_timeout = self.format_duration(self.TIMEOUT)
78+
formatted_timeout = self.format_duration(self.timeout)
7979
logger.info(
8080
f"Waiting for task to finish... [ {formatted_duration} / {formatted_timeout} ]"
8181
)
82-
await asyncio.sleep(self.LOOP_SLEEP)
83-
if time.time() - start_time > self.TIMEOUT:
82+
await asyncio.sleep(self.loop_sleep)
83+
if time.time() - start_time > self.timeout:
8484
logger.warning(
8585
f"Timeout reached while waiting for task to finish. {upid=}"
8686
)

0 commit comments

Comments
 (0)