Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 0.4.0 merge conflicts #229

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions broker/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,44 @@ def simple_retry(cmd, cmd_args=None, cmd_kwargs=None, max_timeout=60, _cur_timeo
simple_retry(cmd, cmd_args, cmd_kwargs, max_timeout, new_wait)


class FileLock:
"""Basic file locking class that acquires and releases locks.

Recommended usage is the context manager which will handle everything for you.

with FileLock("basic_file.txt"):
Path("basic_file.txt").write_text("some text")

If a lock is already in place, FileLock will wait up to <timeout> seconds
"""

def __init__(self, file_name, timeout=10):
self.lock = Path(f"{file_name}.lock")
self.timeout = timeout

def wait_file(self):
"""Wait for an existing lock to be released, if applicable."""
timeout_after = time.time() + self.timeout
while self.lock.exists():
if time.time() <= timeout_after:
time.sleep(1)
else:
raise exceptions.BrokerError(
f"Timeout while waiting for lock release: {self.lock.absolute()}"
)
self.lock.touch()

def return_file(self):
"""Release our lock."""
self.lock.unlink()

def __enter__(self): # noqa: D105
self.wait_file()

def __exit__(self, *tb_info): # noqa: D105
self.return_file()


class Result:
"""Dummy result class for presenting results in dot access."""

Expand Down
5 changes: 3 additions & 2 deletions catalog-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ metadata:
- aap
- docker
- podman
namespace: quality-community
annotations:
github.com/project-slug: SatelliteQE/broker
spec:
type: library
owner: satelliteqe
lifecycle: beta
owner: group:redhat/satelliteqe
lifecycle: preproduction
17 changes: 17 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ def test_emitter(tmp_file):
assert written == {"test": "value", "another": 5, "thing": 13}


def test_lock_file_created(tmp_file):
lock_file = helpers.FileLock(tmp_file)
with lock_file:
assert isinstance(lock_file.lock, Path)
assert lock_file.lock.exists()
assert not lock_file.lock.exists()


def test_lock_timeout(tmp_file):
tmp_lock = Path(f"{tmp_file}.lock")
tmp_lock.touch()
with pytest.raises(exceptions.BrokerError) as exc:
with helpers.FileLock(tmp_file, timeout=1):
pass
assert str(exc.value).startswith("Timeout while waiting for lock release: ")


def test_find_origin_simple():
origin = helpers.find_origin()
assert len(origin) == 2
Expand Down