Skip to content

Commit

Permalink
apt: add decorator to deal with lock retries
Browse files Browse the repository at this point in the history
Signed-off-by: Renan Rodrigo <[email protected]>
  • Loading branch information
renanrodrigo committed Mar 26, 2024
1 parent 1e86d9e commit 6fa3d22
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions uaclient/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re
import subprocess
import tempfile
import time
from functools import lru_cache, wraps
from typing import Dict, Iterable, List, NamedTuple, Optional, Set, Union

Expand Down Expand Up @@ -85,6 +86,35 @@
LOG = logging.getLogger(util.replace_top_level_logger_name(__name__))


def check_apt_lock():
"""Decorator to deal with apt locks when running apt commands."""

def wrapper(f):
@wraps(f)
def decorator(*args, **kwargs):
waiting = False
while True:
try:
return f(*args, **kwargs)
except exceptions.APTProcessConflictError as e:
if not waiting:
choice = util.prompt_choices(
"apt bail. [w]ait, [c]ancel, [t]ry again",
["w", "c", "t"],
)
if choice == "c":
raise e
if choice == "w":
waiting = True
else:
time.sleep(1)
continue

return decorator

return wrapper


@enum.unique
class AptProxyScope(enum.Enum):
GLOBAL = object()
Expand Down

0 comments on commit 6fa3d22

Please sign in to comment.