Skip to content

Commit

Permalink
Pass in arguments instead of unpacking inside function
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindeide committed Oct 31, 2024
1 parent 5c7768f commit d3c2dc3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/everest/bin/everest_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def run_everest(options):
logger = logging.getLogger("everest_main")
server_state = everserver_status(options.config)

if server_is_running(options.config):
if server_is_running(*options.config.server_context):
config_file = options.config.config_file
print(
"An optimization is currently running.\n"
Expand Down
2 changes: 1 addition & 1 deletion src/everest/bin/kill_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _handle_keyboard_interrupt(signal, frame, after=False):


def kill_everest(options):
if not server_is_running(options.config):
if not server_is_running(*options.config.server_context):
print("Server is not running.")
return

Expand Down
2 changes: 1 addition & 1 deletion src/everest/bin/monitor_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def monitor_everest(options):
config: EverestConfig = options.config
server_state = everserver_status(options.config)

if server_is_running(config):
if server_is_running(*config.server_context):
run_detached_monitor(config, show_all_jobs=options.show_all_jobs)
server_state = everserver_status(config)
if server_state["status"] == ServerStatus.failed:
Expand Down
30 changes: 20 additions & 10 deletions src/everest/config/everest_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
from argparse import ArgumentParser
from io import StringIO
from pathlib import Path
from typing import TYPE_CHECKING, List, Literal, Optional, Protocol, no_type_check
from typing import (
TYPE_CHECKING,
List,
Literal,
Optional,
Protocol,
Tuple,
no_type_check,
)

from pydantic import (
AfterValidator,
Expand Down Expand Up @@ -684,18 +692,20 @@ def hostfile_path(self):
def server_info(self):
"""Load server information from the hostfile"""
host_file_path = self.hostfile_path
try:
with open(host_file_path, "r", encoding="utf-8") as f:
json_string = f.read()

with open(host_file_path, "r", encoding="utf-8") as f:
json_string = f.read()

data = json.loads(json_string)
if set(data.keys()) != {"host", "port", "cert", "auth"}:
raise RuntimeError("Malformed hostfile")

return data
data = json.loads(json_string)
if set(data.keys()) != {"host", "port", "cert", "auth"}:
raise RuntimeError("Malformed hostfile")
return data
except FileNotFoundError:
# No host file
return {"host": None, "port": None, "cert": None, "auth": None}

@property
def server_context(self):
def server_context(self) -> Tuple[str, str, Tuple[str, str]]:
"""Returns a tuple with
- url of the server
- path to the .cert file
Expand Down
17 changes: 8 additions & 9 deletions src/everest/detached/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def start_server(config: EverestConfig, ert_config: ErtConfig, storage):
"""
Start an Everest server running the optimization defined in the config
"""
if server_is_running(config): # better safe than sorry
if server_is_running(*config.server_context): # better safe than sorry
return

log_dir = config.log_dir
Expand Down Expand Up @@ -164,7 +164,7 @@ def wait_for_server(
Raise an exception when the timeout is reached.
"""
if not server_is_running(config):
if not server_is_running(*config.server_context):
sleep_time_increment = float(timeout) / (2**_HTTP_REQUEST_RETRY - 1)
for retry_count in range(_HTTP_REQUEST_RETRY):
# Failure may occur before contact with the server is established:
Expand Down Expand Up @@ -208,11 +208,11 @@ def wait_for_server(

sleep_time = sleep_time_increment * (2**retry_count)
time.sleep(sleep_time)
if server_is_running(config):
if server_is_running(*config.server_context):
return

# If number of retries reached and server is not running - throw exception
if not server_is_running(config):
if not server_is_running(*config.server_context):
raise RuntimeError("Failed to start server within configured timeout.")


Expand Down Expand Up @@ -313,22 +313,21 @@ def wait_for_server_to_stop(config: EverestConfig, timeout):
Raise an exception when the timeout is reached.
"""
if server_is_running(config):
if server_is_running(*config.server_context):
sleep_time_increment = float(timeout) / (2**_HTTP_REQUEST_RETRY - 1)
for retry_count in range(_HTTP_REQUEST_RETRY):
sleep_time = sleep_time_increment * (2**retry_count)
time.sleep(sleep_time)
if not server_is_running(config):
if not server_is_running(*config.server_context):
return

# If number of retries reached and server still running - throw exception
if server_is_running(config):
if server_is_running(*config.server_context):
raise Exception("Failed to stop server within configured timeout.")


def server_is_running(config: EverestConfig):
def server_is_running(url: str, cert: bool, auth: Tuple[str, str]):
try:
url, cert, auth = config.server_context
response = requests.get(
url,
verify=cert,
Expand Down
4 changes: 2 additions & 2 deletions tests/everest/test_detached.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_https_requests(copy_math_func_test_data_to_tmp):
result.raise_for_status()

# Test stopping server
assert server_is_running(everest_config)
assert server_is_running(*everest_config.server_context)

if stop_server(everest_config):
wait_for_server_to_stop(everest_config, 60)
Expand All @@ -115,7 +115,7 @@ def test_https_requests(copy_math_func_test_data_to_tmp):
ServerStatus.stopped,
ServerStatus.completed,
]
assert not server_is_running(everest_config)
assert not server_is_running(*everest_config.server_context)
else:
context_stop_and_wait()
server_status = everserver_status(everest_config)
Expand Down

0 comments on commit d3c2dc3

Please sign in to comment.