Implements Additional Logs and class Player Tests

This commit is contained in:
2023-05-03 03:14:26 -03:00
parent 56994dba27
commit b440a0f05d
8 changed files with 586 additions and 80 deletions

View File

@@ -3,6 +3,7 @@ from __future__ import annotations
from diceplayer.shared.config.dice_dto import DiceDTO
from diceplayer.shared.config.step_dto import StepDTO
from diceplayer.shared.interface import Interface
from diceplayer import logger
from multiprocessing import Process, connection
from setproctitle import setproctitle
@@ -15,7 +16,6 @@ import time
import sys
import os
DICE_END_FLAG: Final[str] = "End of simulation"
DICE_FLAG_LINE: Final[int] = -2
UMAANG3_TO_GCM3: Final[float] = 1.6605
@@ -41,8 +41,9 @@ class DiceInterface(Interface):
procs = []
sentinels = []
for proc in range(1, self.step.nprocs + 1):
logger.info(f"---------------------- DICE - CYCLE {cycle} --------------------------\n")
for proc in range(1, self.step.nprocs + 1):
p = Process(target=self._simulation_process, args=(cycle, proc))
p.start()
@@ -61,6 +62,8 @@ class DiceInterface(Interface):
p.terminate()
sys.exit(status)
logger.info("\n")
def reset(self):
del self.step
@@ -132,6 +135,11 @@ class DiceInterface(Interface):
f"step{cycle:02d}",
f"p{proc:02d}"
)
logger.info(
f"Simulation process {str(proc_dir)} initiated with pid {os.getpid()}"
)
os.chdir(proc_dir)
if not (self.config.randominit == 'first' and cycle > 1):
@@ -385,3 +393,5 @@ class DiceInterface(Interface):
flag = outfile.readlines()[DICE_FLAG_LINE].strip()
if flag != DICE_END_FLAG:
raise RuntimeError(f"Dice process step{cycle:02d}-p{proc:02d} did not exit properly")
logger.info(f"Dice {file_name} - step{cycle:02d}-p{proc:02d} exited properly")

View File

@@ -14,7 +14,7 @@ class GaussianInterface(Interface):
def configure(self):
pass
def start(self):
def start(self, cycle: int):
pass
def reset(self):

View File

@@ -24,15 +24,17 @@ class Logger:
if self._logger is None:
self._logger = logging.getLogger(logger_name)
def set_logger(self, outfile='run.log', level=logging.INFO):
self.outfile = Path(outfile)
if self.outfile.exists():
self.outfile.rename(str(self.outfile) + ".backup")
def set_logger(self, outfile='run.log', level=logging.INFO, stream=None):
outfile_path = None
if outfile is not None and stream is None:
outfile_path = Path(outfile)
if outfile_path.exists():
outfile_path.rename(str(outfile_path) + ".backup")
if level is not None:
self._logger.setLevel(level)
self._create_handlers()
self._create_handlers(outfile_path, stream)
self._was_set = True
@@ -52,10 +54,12 @@ class Logger:
def error(self, message):
self._logger.error(message)
def _create_handlers(self):
def _create_handlers(self, outfile_path: Path, stream):
handlers = []
if self.outfile is not None:
handlers.append(logging.FileHandler(self.outfile, mode='a+'))
if outfile_path is not None:
handlers.append(logging.FileHandler(outfile_path, mode='a+'))
elif stream is not None:
handlers.append(logging.StreamHandler(stream))
else:
handlers.append(logging.StreamHandler())