Skip to content

Commit

Permalink
Fix test event reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
xjules committed Nov 20, 2024
1 parent afa1f9a commit e7ee737
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 167 deletions.
1 change: 1 addition & 0 deletions src/_ert/forward_model_runner/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __enter__(self) -> Self:
def __exit__(self, exc_type: Any, exc_value: Any, exc_traceback: Any) -> None:
self.socket.close()
self.context.term()
self.loop.close()

async def __aenter__(self) -> Self:
return self
Expand Down
30 changes: 7 additions & 23 deletions src/_ert/forward_model_runner/reporting/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import logging
import queue
import threading
import time
from datetime import datetime, timedelta
from pathlib import Path
from typing import Final, Union

Expand All @@ -19,6 +17,7 @@
)
from _ert.forward_model_runner.client import (
Client,
ClientConnectionError,
)
from _ert.forward_model_runner.reporting.base import Reporter
from _ert.forward_model_runner.reporting.message import (
Expand Down Expand Up @@ -77,10 +76,7 @@ def __init__(self, evaluator_url, token=None, cert_path=None):
self._real_id = None
self._event_queue: queue.Queue[events.Event | EventSentinel] = queue.Queue()
self._event_publisher_thread = ErtThread(target=self._event_publisher)
self._timeout_timestamp = None
self._timestamp_lock = threading.Lock()
# seconds to timeout the reporter the thread after Finish() was received
self._reporter_timeout = 60
self._done = False

def _event_publisher(self):
logger.debug("Publishing event.")
Expand All @@ -91,21 +87,12 @@ def _event_publisher(self):
) as client:
events = []
last_sent_time = time.time()
while True:
with self._timestamp_lock:
if (
self._timeout_timestamp is not None
and datetime.now() > self._timeout_timestamp
):
self._timeout_timestamp = None
break

while not self._done:
try:
event = self._event_queue.get()
logger.debug(f"Got event for zmq: {event}")
if event is self._sentinel:
self._done = True
if events:
logger.debug(f"Got event class for zmq: {events}")
client.send(events)
events.clear()
break
Expand All @@ -114,12 +101,13 @@ def _event_publisher(self):
current_time = time.time()
if current_time - last_sent_time >= 2:
if events:
logger.debug(f"Got event class for zmq: {events}")
client.send(events)
events.clear()
last_sent_time = current_time
except Exception as e:
except ClientConnectionError as e:
logger.error(f"Failed to send event: {e}")
except Exception as e:
logger.error(f"Error while sending event: {e}")
raise

def report(self, msg):
Expand Down Expand Up @@ -184,10 +172,6 @@ def _job_handler(self, msg: Union[Start, Running, Exited]):

def _finished_handler(self, _):
self._event_queue.put(Event._sentinel)
with self._timestamp_lock:
self._timeout_timestamp = datetime.now() + timedelta(
seconds=self._reporter_timeout
)
if self._event_publisher_thread.is_alive():
self._event_publisher_thread.join()

Expand Down
Loading

0 comments on commit e7ee737

Please sign in to comment.