Skip to content

Commit

Permalink
Refactor usage of EverestConfig in everest server functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSava committed Nov 13, 2024
1 parent 03cfa25 commit 6944c45
Show file tree
Hide file tree
Showing 17 changed files with 379 additions and 279 deletions.
5 changes: 3 additions & 2 deletions src/everest/api/everest_data_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from seba_sqlite.snapshot import SebaSnapshot

from ert.storage import open_storage
from everest.config import EverestConfig
from everest.config import EverestConfig, ServerConfig
from everest.detached import ServerStatus, everserver_status


Expand Down Expand Up @@ -204,7 +204,8 @@ def output_folder(self):

@property
def everest_csv(self):
state = everserver_status(self._config)
status_path = ServerConfig.get_everserver_status_path(self._config.output_dir)
state = everserver_status(status_path)
if state["status"] == ServerStatus.completed:
return self._config.export_path
else:
Expand Down
42 changes: 34 additions & 8 deletions src/everest/bin/everest_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import json
import logging
import os
import signal
import threading
from functools import partial
Expand All @@ -20,6 +21,7 @@
wait_for_server,
)
from everest.plugins.site_config_env import PluginSiteConfigEnv
from everest.strings import EVEREST
from everest.util import makedirs_if_needed, version_info

from .utils import (
Expand Down Expand Up @@ -82,9 +84,12 @@ def _build_args_parser():

def run_everest(options):
logger = logging.getLogger("everest_main")
server_state = everserver_status(options.config)

if server_is_running(*ServerConfig.get_server_context(options.config.output_dir)):
everserver_status_path = ServerConfig.get_everserver_status_path(
options.config.output_dir
)
server_state = everserver_status(everserver_status_path)
server_context = ServerConfig.get_server_context(options.config.output_dir)
if server_is_running(*server_context):
config_file = options.config.config_file
print(
"An optimization is currently running.\n"
Expand All @@ -108,16 +113,33 @@ def run_everest(options):
)

makedirs_if_needed(options.config.output_dir, roll_if_exists=True)
try:
output_dir = options.config.output_dir
config_file = options.config.config_file
save_config_path = os.path.join(output_dir, config_file)
options.config.dump(save_config_path)
except (OSError, LookupError) as e:
logging.getLogger(EVEREST).error(
"Failed to save optimization config: {}".format(e)
)

with open_storage(ert_config.ens_path, "w") as storage, PluginSiteConfigEnv():
context = start_server(options.config, ert_config, storage)
context = start_server(ert_config, storage)
print("Waiting for server ...")
wait_for_server(options.config, timeout=600, context=context)
wait_for_server(
session_dir=ServerConfig.get_session_dir(options.config.output_dir),
timeout=600,
context=context,
)
print("Everest server found!")
run_detached_monitor(options.config, show_all_jobs=options.show_all_jobs)
run_detached_monitor(
server_context=server_context,
optimization_output_dir=options.config.optimization_output_dir,
show_all_jobs=options.show_all_jobs,
)
wait_for_context()

server_state = everserver_status(options.config)
server_state = everserver_status(everserver_status_path)
server_state_info = server_state["message"]
if server_state["status"] == ServerStatus.failed:
logger.error("Everest run failed with: {}".format(server_state_info))
Expand All @@ -126,7 +148,11 @@ def run_everest(options):
logger.info("Everest run finished with: {}".format(server_state_info))
print(server_state_info)
else:
report_on_previous_run(options.config)
report_on_previous_run(
config_file=options.config.config_file,
everserver_status_path=everserver_status_path,
optimization_output_dir=options.config.optimization_output_dir,
)


if __name__ == "__main__":
Expand Down
9 changes: 4 additions & 5 deletions src/everest/bin/kill_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ def _handle_keyboard_interrupt(signal, frame, after=False):


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

stopping = stop_server(options.config)
stopping = stop_server(server_context)
if threading.current_thread() is threading.main_thread():
signal.signal(signal.SIGINT, partial(_handle_keyboard_interrupt, after=True))

Expand All @@ -85,7 +84,7 @@ def kill_everest(options):
return
try:
print("Waiting for server to stop ...")
wait_for_server_to_stop(options.config, timeout=60)
wait_for_server_to_stop(server_context, timeout=60)
print("Server stopped.")
except:
logging.debug(traceback.format_exc())
Expand Down
21 changes: 15 additions & 6 deletions src/everest/bin/monitor_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,16 @@ def _build_args_parser():

def monitor_everest(options):
config: EverestConfig = options.config
server_state = everserver_status(options.config)

if server_is_running(*ServerConfig.get_server_context(config.output_dir)):
run_detached_monitor(config, show_all_jobs=options.show_all_jobs)
server_state = everserver_status(config)
status_path = ServerConfig.get_everserver_status_path(config.output_dir)
server_state = everserver_status(status_path)
server_context = ServerConfig.get_server_context(config.output_dir)
if server_is_running(*server_context):
run_detached_monitor(
server_context=server_context,
optimization_output_dir=config.optimization_output_dir,
show_all_jobs=options.show_all_jobs,
)
server_state = everserver_status(status_path)
if server_state["status"] == ServerStatus.failed:
raise SystemExit(server_state["message"])
if server_state["message"] is not None:
Expand All @@ -78,7 +83,11 @@ def monitor_everest(options):
f" `everest run {config_file}`"
)
else:
report_on_previous_run(config)
report_on_previous_run(
config_file=config.config_file,
everserver_status_path=status_path,
optimization_output_dir=config.optimization_output_dir,
)


if __name__ == "__main__":
Expand Down
33 changes: 19 additions & 14 deletions src/everest/bin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import traceback
from dataclasses import dataclass, field
from itertools import groupby
from typing import ClassVar, Dict, List
from typing import ClassVar, Dict, List, Tuple

import colorama
from colorama import Fore
Expand Down Expand Up @@ -140,8 +140,7 @@ class _DetachedMonitor:
INDENT = 2
FLOAT_FMT = ".5g"

def __init__(self, config, show_all_jobs):
self._config = config
def __init__(self, show_all_jobs):
self._show_all_jobs: bool = show_all_jobs
self._clear_lines = 0
self._batches_done = set()
Expand Down Expand Up @@ -300,19 +299,26 @@ def _clear(self):
print(colorama.Cursor.UP(), end=colorama.ansi.clear_line())


def run_detached_monitor(config: EverestConfig, show_all_jobs: bool = False):
monitor = _DetachedMonitor(config, show_all_jobs)
start_monitor(config, callback=monitor.update)
opt_status = get_opt_status(config.optimization_output_dir)
def run_detached_monitor(
server_context: Tuple[str, str, Tuple[str, str]],
optimization_output_dir: str,
show_all_jobs: bool = False,
):
monitor = _DetachedMonitor(show_all_jobs)
start_monitor(server_context, callback=monitor.update)
opt_status = get_opt_status(optimization_output_dir)
if opt_status.get("cli_monitor_data"):
msg, _ = monitor.get_opt_progress(opt_status)
if msg.strip():
print(f"{msg}\n")


def report_on_previous_run(config: EverestConfig):
server_state = everserver_status(config)
config_file = config.config_file
def report_on_previous_run(
config_file: str,
everserver_status_path: str,
optimization_output_dir: str,
):
server_state = everserver_status(everserver_status_path)
if server_state["status"] == ServerStatus.failed:
error_msg = server_state["message"]
print(
Expand All @@ -321,14 +327,13 @@ def report_on_previous_run(config: EverestConfig):
f"` everest run --new-run {config_file}`\n"
)
else:
output_dir = config.output_dir
opt_status = get_opt_status(config.optimization_output_dir)
opt_status = get_opt_status(optimization_output_dir)
if opt_status.get("cli_monitor_data"):
monitor = _DetachedMonitor(config, show_all_jobs=False)
monitor = _DetachedMonitor(show_all_jobs=False)
msg, _ = monitor.get_opt_progress(opt_status)
print(msg + "\n")
print(
f"Optimization completed, results in {output_dir}\n"
f"Optimization completed.\n"
"\nTo re-run the optimization use command:\n"
f" `everest run --new-run {config_file}`\n"
"To export the results use command:\n"
Expand Down
6 changes: 4 additions & 2 deletions src/everest/bin/visualization_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import partial

from everest.api import EverestDataAPI
from everest.config import EverestConfig
from everest.config import EverestConfig, ServerConfig
from everest.detached import ServerStatus, everserver_status
from everest.plugins.everest_plugin_manager import EverestPluginManager

Expand All @@ -27,7 +27,9 @@ def visualization_entry(args=None):
options = parser.parse_args(args)
config = options.config_file

server_state = everserver_status(config)
server_state = everserver_status(
ServerConfig.get_everserver_status_path(config.output_dir)
)
if server_state["status"] != ServerStatus.never_run:
pm = EverestPluginManager()
pm.hook.visualize_data(api=EverestDataAPI(config))
Expand Down
2 changes: 1 addition & 1 deletion src/everest/config/server_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get_server_url(output_dir: str) -> str:
return f"https://{server_info['host']}:{server_info['port']}"

@staticmethod
def get_server_context(output_dir: str) -> Tuple[str, bool, Tuple[str, str]]:
def get_server_context(output_dir: str) -> Tuple[str, str, Tuple[str, str]]:
"""Returns a tuple with
- url of the server
- path to the .cert file
Expand Down
Loading

0 comments on commit 6944c45

Please sign in to comment.