Skip to content

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
miili committed Oct 26, 2023
1 parent 423ef9d commit e623710
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions lassie/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,34 @@ class Search(BaseModel):
default_factory=lambda: deque(maxlen=25)
)

def init_rundir(self, force=False) -> None:
def init_rundir(self, force: bool = False) -> None:
rundir = (
self.project_dir / self._config_stem or f"run-{time_to_path(self.created)}"
)
self._rundir = rundir

if rundir.exists() and not force:
raise FileExistsError(f"Rundir {rundir} already exists")

if rundir.exists() and force:
if not rundir.exists():
rundir.mkdir()
elif rundir.exists() and force:
create_time = time_to_path(
datetime.fromtimestamp(rundir.stat().st_ctime) # noqa
)
rundir_backup = rundir.with_name(f"{rundir.name}.bak-{create_time}")
rundir.rename(rundir_backup)
logger.info("created backup of existing rundir to %s", rundir_backup)
else:
raise FileExistsError(f"Rundir {rundir} already exists")

if not rundir.exists():
rundir.mkdir()

file_logger = logging.FileHandler(self._rundir / "lassie.log")
logging.root.addHandler(file_logger)
self.write_config()
self._init_logging()

logger.info("created new rundir %s", rundir)
self._detections = EventDetections(rundir=rundir)

def _init_logging(self) -> None:
file_logger = logging.FileHandler(self._rundir / "lassie.log")
logging.root.addHandler(file_logger)

def write_config(self, path: Path | None = None) -> None:
rundir = self._rundir
path = path or rundir / "search.json"
Expand Down Expand Up @@ -215,7 +216,10 @@ async def prepare(self) -> None:

async def start(self, force_rundir: bool = False) -> None:
await self.prepare()
self.init_rundir(force_rundir)

if not self.has_rundir():
self.init_rundir(force=force_rundir)

logger.info("starting search...")
batch_processing_start = datetime_now()
processing_start = datetime_now()
Expand Down Expand Up @@ -322,6 +326,8 @@ def load_rundir(cls, rundir: Path) -> Self:
search._progress = SearchProgress.model_validate_json(
progress_file.read_text()
)

search._init_logging()
return search

@classmethod
Expand All @@ -340,6 +346,9 @@ def from_config(
model._config_stem = filename.stem
return model

def has_rundir(self) -> bool:
return hasattr(self, "_rundir") and self._rundir.exists()

def __del__(self) -> None:
# FIXME: Replace with signal overserver?
if hasattr(self, "_detections"):
Expand Down

0 comments on commit e623710

Please sign in to comment.