mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-15 06:55:48 +00:00
feat: improve lock mechanisms
* improve lock mechanisms * use /run/ahriman for sockett * better water
This commit is contained in:
@ -19,7 +19,6 @@
|
||||
#
|
||||
# pylint: disable=too-many-lines
|
||||
import argparse
|
||||
import tempfile
|
||||
|
||||
from pathlib import Path
|
||||
from typing import TypeVar
|
||||
@ -73,8 +72,7 @@ def _parser() -> argparse.ArgumentParser:
|
||||
parser.add_argument("-c", "--configuration", help="configuration path", type=Path,
|
||||
default=Path("/") / "etc" / "ahriman.ini")
|
||||
parser.add_argument("--force", help="force run, remove file lock", action="store_true")
|
||||
parser.add_argument("-l", "--lock", help="lock file", type=Path,
|
||||
default=Path(tempfile.gettempdir()) / "ahriman.lock")
|
||||
parser.add_argument("-l", "--lock", help="lock file", type=Path, default=Path("ahriman.pid"))
|
||||
parser.add_argument("--log-handler", help="explicit log handler specification. If none set, the handler will be "
|
||||
"guessed from environment",
|
||||
type=LogHandler, choices=enum_values(LogHandler))
|
||||
@ -628,8 +626,7 @@ def _set_repo_daemon_parser(root: SubParserAction) -> argparse.ArgumentParser:
|
||||
parser.add_argument("-y", "--refresh", help="download fresh package databases from the mirror before actions, "
|
||||
"-yy to force refresh even if up to date",
|
||||
action="count", default=False)
|
||||
parser.set_defaults(handler=handlers.Daemon, exit_code=False,
|
||||
lock=Path(tempfile.gettempdir()) / "ahriman-daemon.lock", package=[])
|
||||
parser.set_defaults(handler=handlers.Daemon, exit_code=False, lock=Path("ahriman-daemon.pid"), package=[])
|
||||
return parser
|
||||
|
||||
|
||||
@ -880,7 +877,7 @@ def _set_service_clean_parser(root: SubParserAction) -> argparse.ArgumentParser:
|
||||
action=argparse.BooleanOptionalAction, default=False)
|
||||
parser.add_argument("--pacman", help="clear directory with pacman local database cache",
|
||||
action=argparse.BooleanOptionalAction, default=False)
|
||||
parser.set_defaults(handler=handlers.Clean, quiet=True, unsafe=True)
|
||||
parser.set_defaults(handler=handlers.Clean, lock=None, quiet=True, unsafe=True)
|
||||
return parser
|
||||
|
||||
|
||||
@ -1139,8 +1136,8 @@ def _set_web_parser(root: SubParserAction) -> argparse.ArgumentParser:
|
||||
argparse.ArgumentParser: created argument parser
|
||||
"""
|
||||
parser = root.add_parser("web", help="web server", description="start web server", formatter_class=_formatter)
|
||||
parser.set_defaults(handler=handlers.Web, architecture="", lock=Path(tempfile.gettempdir()) / "ahriman-web.lock",
|
||||
report=False, repository="", parser=_parser)
|
||||
parser.set_defaults(handler=handlers.Web, architecture="", lock=Path("ahriman-web.pid"), report=False,
|
||||
repository="", parser=_parser)
|
||||
return parser
|
||||
|
||||
|
||||
|
@ -18,7 +18,10 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
import argparse
|
||||
import fcntl
|
||||
import os
|
||||
|
||||
from io import TextIOWrapper
|
||||
from pathlib import Path
|
||||
from types import TracebackType
|
||||
from typing import Literal, Self
|
||||
@ -36,7 +39,7 @@ from ahriman.models.waiter import Waiter
|
||||
|
||||
class Lock(LazyLogging):
|
||||
"""
|
||||
wrapper for application lock file
|
||||
wrapper for application lock file. Credits for idea to https://github.com/bmhatfield/python-pidfile.git
|
||||
|
||||
Attributes:
|
||||
force(bool): remove lock file on start if any
|
||||
@ -70,8 +73,13 @@ class Lock(LazyLogging):
|
||||
repository_id(RepositoryId): repository unique identifier
|
||||
configuration(Configuration): configuration instance
|
||||
"""
|
||||
self.path: Path | None = \
|
||||
args.lock.with_stem(f"{args.lock.stem}_{repository_id.id}") if args.lock is not None else None
|
||||
self.path: Path | None = None
|
||||
if args.lock is not None:
|
||||
self.path = args.lock.with_stem(f"{args.lock.stem}_{repository_id.id}")
|
||||
if not self.path.is_absolute():
|
||||
# prepend full path to the lock file
|
||||
self.path = Path("/") / "run" / "ahriman" / self.path
|
||||
self._pid_file: TextIOWrapper | None = None
|
||||
|
||||
self.force: bool = args.force
|
||||
self.unsafe: bool = args.unsafe
|
||||
@ -80,6 +88,72 @@ class Lock(LazyLogging):
|
||||
self.paths = configuration.repository_paths
|
||||
self.reporter = Client.load(repository_id, configuration, report=args.report)
|
||||
|
||||
@staticmethod
|
||||
def perform_lock(fd: int) -> bool:
|
||||
"""
|
||||
perform file lock
|
||||
|
||||
Args:
|
||||
fd(int): file descriptor:
|
||||
|
||||
Returns:
|
||||
bool: True in case if file is locked and False otherwise
|
||||
"""
|
||||
try:
|
||||
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def _open(self) -> None:
|
||||
"""
|
||||
create lock file
|
||||
"""
|
||||
if self.path is None:
|
||||
return
|
||||
self._pid_file = self.path.open("a+")
|
||||
|
||||
def _watch(self) -> bool:
|
||||
"""
|
||||
watch until lock disappear
|
||||
|
||||
Returns:
|
||||
bool: True in case if file is locked and False otherwise
|
||||
"""
|
||||
# there are reasons why we are not using inotify here. First of all, if we would use it, it would bring to
|
||||
# race conditions because multiple processes will be notified at the same time. Secondly, it is good library,
|
||||
# but platform-specific, and we only need to check if file exists
|
||||
if self._pid_file is None:
|
||||
return False
|
||||
|
||||
waiter = Waiter(self.wait_timeout)
|
||||
return bool(waiter.wait(lambda fd: not self.perform_lock(fd), self._pid_file.fileno()))
|
||||
|
||||
def _write(self, *, is_locked: bool = False) -> None:
|
||||
"""
|
||||
write pid to the lock file
|
||||
|
||||
Args:
|
||||
is_locked(bool, optional): indicates if file was already locked or not (Default value = False)
|
||||
|
||||
Raises:
|
||||
DuplicateRunError: if it cannot lock PID file
|
||||
"""
|
||||
if self._pid_file is None:
|
||||
return
|
||||
if not is_locked:
|
||||
if not self.perform_lock(self._pid_file.fileno()):
|
||||
raise DuplicateRunError
|
||||
|
||||
self._pid_file.seek(0) # reset position and remove file content if any
|
||||
self._pid_file.truncate()
|
||||
|
||||
self._pid_file.write(str(os.getpid())) # write current pid
|
||||
self._pid_file.flush() # flush data to disk
|
||||
|
||||
self._pid_file.seek(0) # reset position again
|
||||
|
||||
def check_user(self) -> None:
|
||||
"""
|
||||
check if current user is actually owner of ahriman root
|
||||
@ -100,46 +174,33 @@ class Lock(LazyLogging):
|
||||
"""
|
||||
remove lock file
|
||||
"""
|
||||
if self.path is None:
|
||||
return
|
||||
self.path.unlink(missing_ok=True)
|
||||
if self._pid_file is not None: # close file descriptor
|
||||
try:
|
||||
self._pid_file.close()
|
||||
except IOError:
|
||||
pass # suppress any IO errors occur
|
||||
if self.path is not None: # remove lock file
|
||||
self.path.unlink(missing_ok=True)
|
||||
|
||||
def create(self) -> None:
|
||||
def lock(self) -> None:
|
||||
"""
|
||||
create lock file
|
||||
|
||||
Raises:
|
||||
DuplicateRunError: if lock exists and no force flag supplied
|
||||
create pid file
|
||||
"""
|
||||
if self.path is None:
|
||||
return
|
||||
try:
|
||||
self.path.touch(exist_ok=self.force)
|
||||
except FileExistsError:
|
||||
raise DuplicateRunError from None
|
||||
|
||||
def watch(self) -> None:
|
||||
"""
|
||||
watch until lock disappear
|
||||
"""
|
||||
# there are reasons why we are not using inotify here. First of all, if we would use it, it would bring to
|
||||
# race conditions because multiple processes will be notified in the same time. Secondly, it is good library,
|
||||
# but platform-specific, and we only need to check if file exists
|
||||
if self.path is None:
|
||||
return
|
||||
|
||||
waiter = Waiter(self.wait_timeout)
|
||||
waiter.wait(self.path.is_file)
|
||||
if self.force: # remove lock if force flag is set
|
||||
self.clear()
|
||||
self._open()
|
||||
is_locked = self._watch()
|
||||
self._write(is_locked=is_locked)
|
||||
|
||||
def __enter__(self) -> Self:
|
||||
"""
|
||||
default workflow is the following:
|
||||
|
||||
#. Check user UID
|
||||
#. Check if there is lock file
|
||||
#. Check web status watcher status
|
||||
#. Open lock file
|
||||
#. Wait for lock file to be free
|
||||
#. Create lock file and directory tree
|
||||
#. Write current PID to the lock file
|
||||
#. Report to status page if enabled
|
||||
|
||||
Returns:
|
||||
@ -147,8 +208,7 @@ class Lock(LazyLogging):
|
||||
"""
|
||||
self.check_user()
|
||||
self.check_version()
|
||||
self.watch()
|
||||
self.create()
|
||||
self.lock()
|
||||
self.reporter.status_update(BuildStatusEnum.Building)
|
||||
return self
|
||||
|
||||
|
@ -21,27 +21,87 @@ import time
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass, field
|
||||
from typing import ParamSpec
|
||||
from typing import Literal, ParamSpec
|
||||
|
||||
|
||||
Params = ParamSpec("Params")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WaiterResult:
|
||||
"""
|
||||
representation of a waiter result. This class should not be used directly, use derivatives instead
|
||||
|
||||
Attributes:
|
||||
took(float): consumed time in seconds
|
||||
"""
|
||||
|
||||
took: float
|
||||
|
||||
def __bool__(self) -> bool:
|
||||
"""
|
||||
indicates whether the waiter completed with success or not
|
||||
|
||||
Raises:
|
||||
NotImplementedError: not implemented method
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def __float__(self) -> float:
|
||||
"""
|
||||
extract time spent to retrieve the result in seconds
|
||||
|
||||
Returns:
|
||||
float: consumed time in seconds
|
||||
"""
|
||||
return self.took
|
||||
|
||||
|
||||
class WaiterTaskFinished(WaiterResult):
|
||||
"""
|
||||
a waiter result used to notify that the task has been completed successfully
|
||||
"""
|
||||
|
||||
def __bool__(self) -> Literal[True]:
|
||||
"""
|
||||
indicates whether the waiter completed with success or not
|
||||
|
||||
Returns:
|
||||
Literal[True]: always False
|
||||
"""
|
||||
return True
|
||||
|
||||
|
||||
class WaiterTimedOut(WaiterResult):
|
||||
"""
|
||||
a waiter result used to notify that the waiter run out of time
|
||||
"""
|
||||
|
||||
def __bool__(self) -> Literal[False]:
|
||||
"""
|
||||
indicates whether the waiter completed with success or not
|
||||
|
||||
Returns:
|
||||
Literal[False]: always False
|
||||
"""
|
||||
return False
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Waiter:
|
||||
"""
|
||||
simple waiter implementation
|
||||
|
||||
Attributes:
|
||||
interval(int): interval in seconds between checks
|
||||
interval(float): interval in seconds between checks
|
||||
start_time(float): monotonic time of the waiter start. More likely must not be assigned explicitly
|
||||
wait_timeout(int): timeout in seconds to wait for. Negative value will result in immediate exit. Zero value
|
||||
wait_timeout(float): timeout in seconds to wait for. Negative value will result in immediate exit. Zero value
|
||||
means infinite timeout
|
||||
"""
|
||||
|
||||
wait_timeout: int
|
||||
wait_timeout: float
|
||||
start_time: float = field(default_factory=time.monotonic, kw_only=True)
|
||||
interval: int = field(default=10, kw_only=True)
|
||||
interval: float = field(default=10, kw_only=True)
|
||||
|
||||
def is_timed_out(self) -> bool:
|
||||
"""
|
||||
@ -51,10 +111,10 @@ class Waiter:
|
||||
bool: True in case current monotonic time is more than :attr:`start_time` and :attr:`wait_timeout`
|
||||
doesn't equal to 0
|
||||
"""
|
||||
since_start: float = time.monotonic() - self.start_time
|
||||
since_start = time.monotonic() - self.start_time
|
||||
return self.wait_timeout != 0 and since_start > self.wait_timeout
|
||||
|
||||
def wait(self, in_progress: Callable[Params, bool], *args: Params.args, **kwargs: Params.kwargs) -> float:
|
||||
def wait(self, in_progress: Callable[Params, bool], *args: Params.args, **kwargs: Params.kwargs) -> WaiterResult:
|
||||
"""
|
||||
wait until requirements are not met
|
||||
|
||||
@ -64,9 +124,12 @@ class Waiter:
|
||||
**kwargs(Params.kwargs): keyword arguments for check call
|
||||
|
||||
Returns:
|
||||
float: consumed time in seconds
|
||||
WaiterResult: consumed time in seconds
|
||||
"""
|
||||
while not self.is_timed_out() and in_progress(*args, **kwargs):
|
||||
while not (timed_out := self.is_timed_out()) and in_progress(*args, **kwargs):
|
||||
time.sleep(self.interval)
|
||||
took = time.monotonic() - self.start_time
|
||||
|
||||
return time.monotonic() - self.start_time
|
||||
if timed_out:
|
||||
return WaiterTimedOut(took)
|
||||
return WaiterTaskFinished(took)
|
||||
|
Reference in New Issue
Block a user