Skip to content

Commit

Permalink
Add config lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
jrdnbradford committed Apr 6, 2024
1 parent 2b29bd9 commit bdbb3ad
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 42 deletions.
109 changes: 67 additions & 42 deletions tljh/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,88 +178,113 @@ def show_config(config_path):
"""
Pretty print config from given config_path
"""
try:
with open(config_path) as f:
config = yaml.load(f)
except FileNotFoundError:
config = {}

config = get_current_config(config_path)
yaml.dump(config, sys.stdout)


def set_config_value(config_path, key_path, value, validate=True):
"""
Set key at key_path in config_path to value
"""
# FIXME: Have a file lock here
from filelock import FileLock, Timeout

lock_file = f"{config_path}.lock"
lock = FileLock(lock_file)
try:
with open(config_path) as f:
config = yaml.load(f)
except FileNotFoundError:
config = {}
config = set_item_in_config(config, key_path, value)
with lock.acquire(timeout=1):
config = get_current_config(config_path)
config = set_item_in_config(config, key_path, value)
validate_config(config, validate)

validate_config(config, validate)
with open(config_path, "w") as f:
yaml.dump(config, f)

with open(config_path, "w") as f:
yaml.dump(config, f)
except Timeout:
print(f"Another instance of tljh-config holds the lock {lock_file}")
exit(1)


def unset_config_value(config_path, key_path, validate=True):
"""
Unset key at key_path in config_path
"""
# FIXME: Have a file lock here
from filelock import FileLock, Timeout

lock_file = f"{config_path}.lock"
lock = FileLock(lock_file)
try:
with open(config_path) as f:
config = yaml.load(f)
except FileNotFoundError:
config = {}
with lock.acquire(timeout=1):
config = get_current_config(config_path)
config = unset_item_from_config(config, key_path)
validate_config(config, validate)

config = unset_item_from_config(config, key_path)
validate_config(config, validate)
with open(config_path, "w") as f:
yaml.dump(config, f)

with open(config_path, "w") as f:
yaml.dump(config, f)
except Timeout:
print(f"Another instance of tljh-config holds the lock {lock_file}")
exit(1)


def add_config_value(config_path, key_path, value, validate=True):
"""
Add value to list at key_path
"""
# FIXME: Have a file lock here
from filelock import FileLock, Timeout

lock_file = f"{config_path}.lock"
lock = FileLock(lock_file)
try:
with open(config_path) as f:
config = yaml.load(f)
except FileNotFoundError:
config = {}
with lock.acquire(timeout=1):
config = get_current_config(config_path)
config = add_item_to_config(config, key_path, value)
validate_config(config, validate)

config = add_item_to_config(config, key_path, value)
validate_config(config, validate)
with open(config_path, "w") as f:
yaml.dump(config, f)

with open(config_path, "w") as f:
yaml.dump(config, f)
except Timeout:
print(f"Another instance of tljh-config holds the lock {lock_file}")
exit(1)


def remove_config_value(config_path, key_path, value, validate=True):
"""
Remove value from list at key_path
"""
# FIXME: Have a file lock here
from filelock import FileLock, Timeout

lock_file = f"{config_path}.lock"
lock = FileLock(lock_file)
try:
with open(config_path) as f:
config = yaml.load(f)
except FileNotFoundError:
config = {}
with lock.acquire(timeout=1):
config = get_current_config(config_path)
config = remove_item_from_config(config, key_path, value)
validate_config(config, validate)

config = remove_item_from_config(config, key_path, value)
validate_config(config, validate)
with open(config_path, "w") as f:
yaml.dump(config, f)

with open(config_path, "w") as f:
yaml.dump(config, f)
except Timeout:
print(f"Another instance of tljh-config holds the lock {lock_file}")
exit(1)


def get_current_config(config_path):
"""
Retrieve the current config at config_path
"""
try:
with open(config_path) as f:
return yaml.load(f)
except FileNotFoundError:
return {}


def check_hub_ready():
"""
Checks that hub is running.
"""
from .configurer import load_config

base_url = load_config()["base_url"]
Expand Down
2 changes: 2 additions & 0 deletions tljh/requirements-hub-env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ jupyterhub-idle-culler>=1.2.1,<2
# ref: https://github.com/jupyterhub/the-littlest-jupyterhub/issues/289
#
pycurl>=7.45.2,<8

filelock>=3.13.3,<4

0 comments on commit bdbb3ad

Please sign in to comment.