Unverified Commit 5ab925b6 authored by mvdbeek's avatar mvdbeek
Browse files

Ignore hashes when checking if tool needs to be reloaded

We only load the hashes in the config watcher thread, so the initial
hashes are not available. I assume there's no sudden modtime changes on
production instances that would make this invalid.
parent a83af33f
Loading
Loading
Loading
Loading
+36 −5
Original line number Diff line number Diff line
@@ -6,6 +6,10 @@ import sqlite3
import tempfile
import zlib
from threading import Lock
from typing import (
    Dict,
    Optional,
)

from sqlitedict import SqliteDict

@@ -134,7 +138,7 @@ class ToolCache:

    def __init__(self):
        self._lock = Lock()
        self._hash_by_tool_paths = {}
        self._hash_by_tool_paths: Dict[str, ToolHash] = {}
        self._tools_by_path = {}
        self._tool_paths_by_id = {}
        self._macro_paths_by_id = {}
@@ -195,9 +199,12 @@ class ToolCache:
        try:
            new_mtime = os.path.getmtime(config_filename)
            tool_hash = self._hash_by_tool_paths.get(config_filename)
            if tool_hash.modtime < new_mtime:
                if md5_hash_file(config_filename) != tool_hash.hash:
            if tool_hash and tool_hash.modtime_less_than(new_mtime):
                if not tool_hash.hash_equals(md5_hash_file(config_filename)):
                    return True
                else:
                    # No change of content, so not necessary to calculate the md5 checksum every time
                    tool_hash.modtime = new_mtime
            tool = self._tools_by_path[config_filename]
            for macro_path in tool._macro_paths:
                new_mtime = os.path.getmtime(macro_path)
@@ -256,13 +263,37 @@ class ToolCache:


class ToolHash:
    def __init__(self, path, modtime=None, lazy_hash=False):
    def __init__(self, path: str, modtime: Optional[float] = None, lazy_hash: bool = False):
        self.path = path
        self.modtime = modtime or os.path.getmtime(path)
        self._modtime = modtime or os.path.getmtime(path)
        self._tool_hash = None
        if not lazy_hash:
            self.hash  # noqa: B018

    def modtime_less_than(self, other_modtime: float):
        if self._modtime is None:
            # For the purposes of the tool cache,
            # if we haven't seen the modtime we consider it not equal
            return True
        return self._modtime < other_modtime

    def hash_equals(self, other_hash: Optional[str]):
        if self._tool_hash is None or other_hash is None:
            # For the purposes of the tool cache,
            # if we haven't seen the hash yet we consider it not equal
            return False
        return self.hash == other_hash

    @property
    def modtime(self) -> float:
        if self._modtime is None:
            self._modtime = os.path.getmtime(self.path)
        return self._modtime

    @modtime.setter
    def modtime(self, new_value: float):
        self._modtime = new_value

    @property
    def hash(self):
        if self._tool_hash is None: