diff --git a/src/common/db/Database.py b/src/common/db/Database.py index 09ba162b5..fa09e1991 100644 --- a/src/common/db/Database.py +++ b/src/common/db/Database.py @@ -1697,7 +1697,7 @@ class Database: } def get_job_cache_file( - self, job_name: str, file_name: str, *, service_id: str = "", with_info: bool = False, with_data: bool = True + self, job_name: str, file_name: str, *, service_id: str = "", plugin_id: str = "", with_info: bool = False, with_data: bool = True ) -> Optional[Union[Dict[str, Any], bytes]]: """Get job cache file.""" entities = [] @@ -1709,6 +1709,8 @@ class Database: filters = {"job_name": job_name, "file_name": file_name} if service_id: filters["service_id"] = service_id + if plugin_id: + filters["plugin_id"] = plugin_id with self.__db_session() as session: data = session.query(Jobs_cache).with_entities(*entities).filter_by(**filters).first() @@ -1727,7 +1729,7 @@ class Database: ret_data["data"] = data.data return ret_data - def get_jobs_cache_files(self, *, job_name: str = "", with_data: bool = False) -> List[Dict[str, Any]]: + def get_jobs_cache_files(self, *, job_name: str = "", plugin_id: str = "", with_data: bool = False) -> List[Dict[str, Any]]: """Get jobs cache files.""" with self.__db_session() as session: entities = [Jobs_cache.job_name, Jobs_cache.service_id, Jobs_cache.file_name] @@ -1736,8 +1738,14 @@ class Database: query = session.query(Jobs_cache).with_entities(*entities) + filters = {} if job_name: - query = query.filter_by(job_name=job_name) + filters["job_name"] = job_name + if plugin_id: + filters["plugin_id"] = plugin_id + + if filters: + query = query.filter_by(**filters) return [ { diff --git a/src/common/utils/jobs.py b/src/common/utils/jobs.py index 1f8dda622..8b2979f86 100644 --- a/src/common/utils/jobs.py +++ b/src/common/utils/jobs.py @@ -47,11 +47,11 @@ class Job: if not deprecated: self.restore_cache() - def restore_cache(self, *, job_name: str = "") -> bool: + def restore_cache(self, *, job_name: str = "", plugin_id: str = "") -> bool: """Restore job cache files from database.""" ret = True with LOCK: - job_cache_files = self.db.get_jobs_cache_files(job_name=job_name or self.job_name, with_data=True) # type: ignore + job_cache_files = self.db.get_jobs_cache_files(job_name=job_name or self.job_name, plugin_id=plugin_id or self.job_path.name, with_data=True) # type: ignore for job_cache_file in job_cache_files: try: @@ -71,7 +71,7 @@ class Job: return ret def get_cache( - self, name: str, *, job_name: str = "", service_id: str = "", with_info: bool = False, with_data: bool = True + self, name: str, *, job_name: str = "", service_id: str = "", plugin_id: str = "", with_info: bool = False, with_data: bool = True ) -> Optional[Union[Dict[str, Any], bytes]]: """Get cache file from database or from local cache file.""" cache_path = self.job_path.joinpath(service_id, name) @@ -84,15 +84,17 @@ class Job: with LOCK: if not ret_data: - return self.db.get_job_cache_file(job_name or self.job_name, name, service_id=service_id, with_info=with_info, with_data=with_data) # type: ignore - ret_data.update(self.db.get_job_cache_file(job_name or self.job_name, name, service_id=service_id, with_info=True, with_data=False)) # type: ignore + return self.db.get_job_cache_file(job_name or self.job_name, name, service_id=service_id, plugin_id=plugin_id or self.job_path.name, with_info=with_info, with_data=with_data) # type: ignore + ret_data.update(self.db.get_job_cache_file(job_name or self.job_name, name, service_id=service_id, plugin_id=plugin_id or self.job_path.name, with_info=True, with_data=False)) # type: ignore return ret_data - def is_cached_file(self, name: str, expire: Literal["hour", "day", "week", "month"], *, job_name: str = "", service_id: str = "") -> bool: + def is_cached_file( + self, name: str, expire: Literal["hour", "day", "week", "month"], *, job_name: str = "", service_id: str = "", plugin_id: str = "" + ) -> bool: """Check if cache file is cached and if it's still fresh.""" is_cached = False try: - cache_info = self.get_cache(name, job_name=job_name, service_id=service_id, with_info=True, with_data=False) + cache_info = self.get_cache(name, job_name=job_name, service_id=service_id, plugin_id=plugin_id, with_info=True, with_data=False) if isinstance(cache_info, dict): current_time = datetime.now().timestamp() if current_time < cache_info["last_update"]: @@ -178,13 +180,13 @@ class Job: return False, f"exception :\n{format_exc()}" return ret, err - def cache_hash(self, name: str, *, job_name: str = "", service_id: str = "") -> Optional[str]: + def cache_hash(self, name: str, *, job_name: str = "", service_id: str = "", plugin_id: str = "") -> Optional[str]: """Get cache file hash from database or from local cache file.""" cache_path = self.job_path.joinpath(service_id, name) if cache_path.is_file(): return file_hash(cache_path) - cache_info = self.get_cache(name, with_info=True, with_data=False, job_name=job_name, service_id=service_id) + cache_info = self.get_cache(name, with_info=True, with_data=False, job_name=job_name, service_id=service_id, plugin_id=plugin_id) if isinstance(cache_info, dict): return cache_info.get("checksum")