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 12, 2024
1 parent 5b96e8a commit c0cfb68
Show file tree
Hide file tree
Showing 16 changed files with 306 additions and 232 deletions.
2 changes: 1 addition & 1 deletion src/everest/api/everest_data_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def output_folder(self):

@property
def everest_csv(self):
state = everserver_status(self._config)
state = everserver_status(self._config.everserver_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 @@ -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,7 +84,7 @@ def _build_args_parser():

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

if server_is_running(*options.config.server_context):
config_file = options.config.config_file
Expand All @@ -108,16 +110,31 @@ 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=options.config.session_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=options.config.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(options.config.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 +143,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=options.config.everserver_status_path,
optimization_output_dir=options.config.optimization_output_dir,
)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions src/everest/bin/kill_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def kill_everest(options):
print("Server is not running.")
return

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

Expand All @@ -83,7 +83,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(options.config.server_context, timeout=60)
print("Server stopped.")
except:
logging.debug(traceback.format_exc())
Expand Down
16 changes: 12 additions & 4 deletions src/everest/bin/monitor_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ def _build_args_parser():

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

if server_is_running(*config.server_context):
run_detached_monitor(config, show_all_jobs=options.show_all_jobs)
server_state = everserver_status(config)
run_detached_monitor(
server_context=config.server_context,
optimization_output_dir=config.optimization_output_dir,
show_all_jobs=options.show_all_jobs,
)
server_state = everserver_status(config.everserver_status_path)
if server_state["status"] == ServerStatus.failed:
raise SystemExit(server_state["message"])
if server_state["message"] is not None:
Expand All @@ -78,7 +82,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=config.everserver_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
2 changes: 1 addition & 1 deletion src/everest/bin/visualization_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def visualization_entry(args=None):
options = parser.parse_args(args)
config = options.config_file

server_state = everserver_status(config)
server_state = everserver_status(config.everserver_status_path)
if server_state["status"] != ServerStatus.never_run:
pm = EverestPluginManager()
pm.hook.visualize_data(api=EverestDataAPI(config))
Expand Down
Loading

0 comments on commit c0cfb68

Please sign in to comment.