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 18, 2024
1 parent dbb6321 commit c4a2062
Show file tree
Hide file tree
Showing 18 changed files with 270 additions and 205 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
33 changes: 27 additions & 6 deletions src/everest/bin/everest_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import asyncio
import json
import logging
import os
import signal
import threading
from functools import partial
Expand All @@ -17,6 +18,7 @@
start_server,
wait_for_server,
)
from everest.strings import EVEREST
from everest.util import (
makedirs_if_needed,
version_info,
Expand Down Expand Up @@ -86,8 +88,10 @@ def _build_args_parser():

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

everserver_status_path = ServerConfig.get_everserver_status_path(
options.config.output_dir
)
server_state = everserver_status(everserver_status_path)
if server_is_running(*ServerConfig.get_server_context(options.config.output_dir)):
config_file = options.config.config_file
print(
Expand All @@ -105,13 +109,26 @@ async def run_everest(options):
logger.info("Everest forward model contains job {}".format(job_name))

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)
)
await start_server(options.config, options.debug)
print("Waiting for server ...")
wait_for_server(options.config, timeout=600)
wait_for_server(options.config.output_dir, timeout=600)
print("Everest server found!")
run_detached_monitor(options.config, show_all_jobs=options.show_all_jobs)
run_detached_monitor(
server_context=ServerConfig.get_server_context(options.config.output_dir),
optimization_output_dir=options.config.optimization_output_dir,
show_all_jobs=options.show_all_jobs,
)

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 @@ -120,7 +137,11 @@ async 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 c4a2062

Please sign in to comment.