Skip to content

Commit 77a31a5

Browse files
authored
feat(timeout): convert util/timeout to functional (#639)
1 parent dda2965 commit 77a31a5

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed

cli/app/commands/install/command.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from app.utils.config import Config
44
from app.utils.logger import create_logger, log_error, log_success, log_warning
5-
from app.utils.timeout import TimeoutWrapper
5+
from app.utils.timeout import timeout_wrapper
66

77
from .deps import install_all_deps
88
from .run import Install
@@ -211,7 +211,7 @@ def ssh(
211211
)
212212
ssh_operation = SSH(logger=logger)
213213

214-
with TimeoutWrapper(timeout):
214+
with timeout_wrapper(timeout):
215215
result = ssh_operation.generate(config)
216216

217217
log_success(result.output, verbose=verbose)
@@ -234,7 +234,7 @@ def deps(
234234
logger = create_logger(verbose=verbose)
235235
try:
236236

237-
with TimeoutWrapper(timeout):
237+
with timeout_wrapper(timeout):
238238
result = install_all_deps(verbose=verbose, output=output, dry_run=dry_run)
239239

240240
if output == "json":

cli/app/commands/install/development.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from app.utils.file_manager import get_directory_path, set_permissions
3434
from app.utils.host_information import get_os_name, get_package_manager
3535
from app.utils.protocols import LoggerProtocol
36-
from app.utils.timeout import TimeoutWrapper
36+
from app.utils.timeout import timeout_wrapper
3737

3838
from .base import BaseInstall
3939
from .deps import get_deps_from_config, get_installed_deps, install_dep
@@ -361,7 +361,7 @@ def _setup_clone_and_config(self):
361361
return
362362

363363
try:
364-
with TimeoutWrapper(self.timeout):
364+
with timeout_wrapper(self.timeout):
365365
success, error = clone_repository(
366366
repo=self._get_config("repo_url"),
367367
path=self._get_config("full_source_path"),
@@ -475,7 +475,7 @@ def _setup_ssh(self):
475475

476476
ssh_operation = SSH(logger=self.logger)
477477
try:
478-
with TimeoutWrapper(self.timeout):
478+
with timeout_wrapper(self.timeout):
479479
result = ssh_operation.generate(config)
480480
except TimeoutError:
481481
raise Exception(f"{ssh_setup_failed}: {operation_timed_out}")
@@ -548,7 +548,7 @@ def _load_proxy(self):
548548
return
549549

550550
try:
551-
with TimeoutWrapper(self.timeout):
551+
with timeout_wrapper(self.timeout):
552552
success, error = load_config(caddy_json_config, proxy_port, self.logger)
553553
except TimeoutError:
554554
raise Exception(f"Proxy load failed: {operation_timed_out}")
@@ -591,7 +591,7 @@ def _start_all_services(self):
591591

592592
try:
593593
try:
594-
with TimeoutWrapper(self.timeout):
594+
with timeout_wrapper(self.timeout):
595595
success, error = start_services(
596596
name=self._get_config("service_name"),
597597
detach=self._get_config("service_detach"),

cli/app/commands/install/run.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from app.utils.file_manager import get_directory_path, set_permissions
4343
from app.utils.host_information import get_public_ip
4444
from app.utils.protocols import LoggerProtocol
45-
from app.utils.timeout import TimeoutWrapper
45+
from app.utils.timeout import timeout_wrapper
4646

4747
from .deps import install_all_deps
4848
from .messages import (
@@ -306,7 +306,7 @@ def _run_preflight_checks(self):
306306

307307
def _install_dependencies(self):
308308
try:
309-
with TimeoutWrapper(self.timeout):
309+
with timeout_wrapper(self.timeout):
310310
result = install_all_deps(verbose=self.verbose, output="json", dry_run=self.dry_run)
311311
except TimeoutError:
312312
raise Exception(dependency_installation_timeout)
@@ -317,7 +317,7 @@ def _setup_clone_and_config(self):
317317
return
318318

319319
try:
320-
with TimeoutWrapper(self.timeout):
320+
with timeout_wrapper(self.timeout):
321321
success, error = clone_repository(
322322
repo=self._get_config(DEFAULT_REPO),
323323
path=self._get_config("full_source_path"),
@@ -417,7 +417,7 @@ def _setup_ssh(self):
417417
)
418418
ssh_operation = SSH(logger=self.logger)
419419
try:
420-
with TimeoutWrapper(self.timeout):
420+
with timeout_wrapper(self.timeout):
421421
result = ssh_operation.generate(config)
422422
except TimeoutError:
423423
raise Exception(f"{ssh_setup_failed}: {operation_timed_out}")
@@ -455,7 +455,7 @@ def _start_services(self):
455455

456456
try:
457457
try:
458-
with TimeoutWrapper(self.timeout):
458+
with timeout_wrapper(self.timeout):
459459
success, error = start_services(
460460
name="all",
461461
detach=True,
@@ -490,7 +490,7 @@ def _load_proxy(self):
490490
return
491491

492492
try:
493-
with TimeoutWrapper(self.timeout):
493+
with timeout_wrapper(self.timeout):
494494
success, error = load_config(caddy_json_config, proxy_port, self.logger)
495495
except TimeoutError:
496496
raise Exception(f"{proxy_load_failed}: {operation_timed_out}")

cli/app/commands/uninstall/uninstall.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from app.utils.config import DEFAULT_COMPOSE_FILE, NIXOPUS_CONFIG_DIR, SSH_FILE_PATH, Config
99
from app.utils.logger import create_logger
1010
from app.utils.protocols import LoggerProtocol
11-
from app.utils.timeout import TimeoutWrapper
11+
from app.utils.timeout import timeout_wrapper
1212

1313
from .messages import (
1414
authorized_keys_not_found,
@@ -108,7 +108,7 @@ def stop_services_step(
108108
return True, None
109109

110110
try:
111-
with TimeoutWrapper(timeout):
111+
with timeout_wrapper(timeout):
112112
success, error = stop_services(
113113
name="all",
114114
env_file=None,

cli/app/utils/timeout.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
import signal
2+
from contextlib import contextmanager
3+
from typing import Generator
24

35
from app.commands.install.messages import timeout_error
46

57

6-
class TimeoutWrapper:
7-
"""Context manager for timeout operations"""
8+
@contextmanager
9+
def timeout_wrapper(timeout: int) -> Generator[None, None, None]:
10+
"""Context manager for timeout operations using functional approach"""
11+
if timeout > 0:
12+
def timeout_handler(signum, frame):
13+
raise TimeoutError(timeout_error.format(timeout=timeout))
814

9-
def __init__(self, timeout: int):
10-
self.timeout = timeout
11-
self.original_handler = None
12-
13-
def __enter__(self):
14-
if self.timeout > 0:
15-
16-
def timeout_handler(signum, frame):
17-
raise TimeoutError(timeout_error.format(timeout=self.timeout))
18-
19-
self.original_handler = signal.signal(signal.SIGALRM, timeout_handler)
20-
signal.alarm(self.timeout)
21-
return self
22-
23-
def __exit__(self, exc_type, exc_val, exc_tb):
24-
if self.timeout > 0:
15+
original_handler = signal.signal(signal.SIGALRM, timeout_handler)
16+
signal.alarm(timeout)
17+
18+
try:
19+
yield
20+
finally:
2521
signal.alarm(0)
26-
signal.signal(signal.SIGALRM, self.original_handler)
22+
signal.signal(signal.SIGALRM, original_handler)
23+
else:
24+
yield

0 commit comments

Comments
 (0)