Dont modify the mtime, save it to a file
This commit is contained in:
parent
b735a0841c
commit
da29be0596
@ -1,5 +1,4 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
|
||||||
import pickle
|
import pickle
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
@ -143,6 +142,19 @@ class DocumentClassifier:
|
|||||||
):
|
):
|
||||||
raise IncompatibleClassifierVersionError("sklearn version update")
|
raise IncompatibleClassifierVersionError("sklearn version update")
|
||||||
|
|
||||||
|
def set_last_checked(self) -> None:
|
||||||
|
# save a timestamp of the last time we checked for retraining to a file
|
||||||
|
with Path(settings.MODEL_FILE.with_suffix(".last_checked")).open("w") as f:
|
||||||
|
f.write(str(time.time()))
|
||||||
|
|
||||||
|
def get_last_checked(self) -> float | None:
|
||||||
|
# load the timestamp of the last time we checked for retraining
|
||||||
|
try:
|
||||||
|
with Path(settings.MODEL_FILE.with_suffix(".last_checked")).open("r") as f:
|
||||||
|
return float(f.read())
|
||||||
|
except FileNotFoundError: # pragma: no cover
|
||||||
|
return None
|
||||||
|
|
||||||
def save(self) -> None:
|
def save(self) -> None:
|
||||||
target_file: Path = settings.MODEL_FILE
|
target_file: Path = settings.MODEL_FILE
|
||||||
target_file_temp: Path = target_file.with_suffix(".pickle.part")
|
target_file_temp: Path = target_file.with_suffix(".pickle.part")
|
||||||
@ -163,6 +175,7 @@ class DocumentClassifier:
|
|||||||
pickle.dump(self.storage_path_classifier, f)
|
pickle.dump(self.storage_path_classifier, f)
|
||||||
|
|
||||||
target_file_temp.rename(target_file)
|
target_file_temp.rename(target_file)
|
||||||
|
self.set_last_checked()
|
||||||
|
|
||||||
def train(self) -> bool:
|
def train(self) -> bool:
|
||||||
# Get non-inbox documents
|
# Get non-inbox documents
|
||||||
@ -231,9 +244,7 @@ class DocumentClassifier:
|
|||||||
and self.last_doc_change_time >= latest_doc_change
|
and self.last_doc_change_time >= latest_doc_change
|
||||||
) and self.last_auto_type_hash == hasher.digest():
|
) and self.last_auto_type_hash == hasher.digest():
|
||||||
logger.info("No updates since last training")
|
logger.info("No updates since last training")
|
||||||
# Update the modification time of the file to mark it as fresh
|
self.set_last_checked()
|
||||||
new_mtime = time.time()
|
|
||||||
os.utime(settings.MODEL_FILE, (new_mtime, new_mtime))
|
|
||||||
# Set the classifier information into the cache
|
# Set the classifier information into the cache
|
||||||
# Caching for 50 minutes, so slightly less than the normal retrain time
|
# Caching for 50 minutes, so slightly less than the normal retrain time
|
||||||
cache.set(
|
cache.set(
|
||||||
|
@ -2175,9 +2175,10 @@ class SystemStatusView(PassUserMixin):
|
|||||||
classifier_status = "OK"
|
classifier_status = "OK"
|
||||||
classifier_last_trained = (
|
classifier_last_trained = (
|
||||||
make_aware(
|
make_aware(
|
||||||
datetime.fromtimestamp(settings.MODEL_FILE.stat().st_mtime),
|
datetime.fromtimestamp(classifier.get_last_checked()),
|
||||||
)
|
)
|
||||||
if settings.MODEL_FILE.exists()
|
if settings.MODEL_FILE.exists()
|
||||||
|
and classifier.get_last_checked() is not None
|
||||||
else None
|
else None
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user