Skip to content

Commit

Permalink
Issue #604/#644 replace lru_cache trick with cleaner cache
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Oct 14, 2024
1 parent 84ee8ea commit ddc9ee5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 6 additions & 6 deletions openeo/extra/job_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import collections
import contextlib
import datetime
import functools
import json
import logging
import re
Expand All @@ -27,7 +26,7 @@
parse_remote_process_definition,
)
from openeo.rest import OpenEoApiError
from openeo.util import deep_get, repr_truncate, rfc3339
from openeo.util import LazyLoadCache, deep_get, repr_truncate, rfc3339

_log = logging.getLogger(__name__)

Expand Down Expand Up @@ -994,11 +993,15 @@ def __init__(
self._namespace = namespace
self._parameter_defaults = parameter_defaults or {}
self._parameter_column_map = parameter_column_map
self._cache = LazyLoadCache()

def _get_process_definition(self, connection: Connection) -> Process:
if isinstance(self._namespace, str) and re.match("https?://", self._namespace):
# Remote process definition handling
return self._get_remote_process_definition()
return self._cache.get(
key=("remote_process_definition", self._namespace, self._process_id),
load=lambda: parse_remote_process_definition(namespace=self._namespace, process_id=self._process_id),
)
elif self._namespace is None:
# Handling of a user-specific UDP
udp_raw = connection.user_defined_process(self._process_id).describe()
Expand All @@ -1008,9 +1011,6 @@ def _get_process_definition(self, connection: Connection) -> Process:
f"Unsupported process definition source udp_id={self._process_id!r} namespace={self._namespace!r}"
)

@functools.lru_cache()
def _get_remote_process_definition(self) -> Process:
return parse_remote_process_definition(namespace=self._namespace, process_id=self._process_id)

def start_job(self, row: pd.Series, connection: Connection, **_) -> BatchJob:
"""
Expand Down
2 changes: 2 additions & 0 deletions tests/extra/test_job_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,8 @@ def test_with_job_manager_remote_basic(
}
)
assert set(job_db.read().status) == {"finished"}

# Verify caching of HTTP request of remote process definition
assert remote_process_definitions["increment"].call_count == 1

assert dummy_backend.batch_jobs == {
Expand Down

0 comments on commit ddc9ee5

Please sign in to comment.