Skip to content

Commit

Permalink
fix: find_dependent by name or path
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed Oct 16, 2024
1 parent cee4059 commit b961a85
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/_bentoml_impl/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def import_service(
if not depend_path:
svc = root_service
else:
svc = root_service.find_dependent(depend_path)
svc = root_service.find_dependent_by_path(depend_path)
if bento is not None:
svc.on_load_bento(bento)
return svc
Expand Down
4 changes: 2 additions & 2 deletions src/_bentoml_impl/server/serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ def serve_http(
allocator = ResourceAllocator()
if dependency_map is None:
dependency_map = {}
if service_name:
svc = svc.find_dependent(service_name)
if service_name and service_name != svc.name:
svc = svc.find_dependent_by_name(service_name)
num_workers, worker_envs = allocator.get_worker_env(svc)
server_on_deployment(svc)
uds_path = tempfile.mkdtemp(prefix="bentoml-uds-")
Expand Down
2 changes: 1 addition & 1 deletion src/_bentoml_impl/worker/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def main(
service = import_service(bento_identifier)

if service_name and service_name != service.name:
service = service.find_dependent(service_name)
service = service.find_dependent_by_name(service_name)
server_context.service_type = "service"
else:
server_context.service_type = "entry_service"
Expand Down
19 changes: 13 additions & 6 deletions src/_bentoml_sdk/service/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,28 @@ def __repr__(self) -> str:
return f"<{self.__class__.__name__} name={self.name!r}>"

@lru_cache
def find_dependent(self, name_or_path: str) -> Service[t.Any]:
"""Find a service by name or path"""
attr_name, _, path = name_or_path.partition(".")
def find_dependent_by_path(self, path: str) -> Service[t.Any]:
"""Find a service by path"""
attr_name, _, path = path.partition(".")
if attr_name not in self.dependencies:
if attr_name in self.all_services():
return self.all_services()[attr_name]
else:
raise ValueError(f"Service {attr_name} not found")
raise BentoMLException(f"Service {attr_name} not found")
dependent = self.dependencies[attr_name]
if dependent.on is None:
raise ValueError(f"Service {attr_name} not found")
raise BentoMLException(f"Service {attr_name} not found")
if path:
return dependent.on.find_dependent(path)
return dependent.on.find_dependent_by_path(path)
return dependent

def find_dependent_by_name(self, name: str) -> Service[t.Any]:
"""Find a service by name"""
try:
return self.all_services()[name]
except KeyError:
raise BentoMLException(f"Service {name} not found") from None

@property
def url(self) -> str | None:
"""Get the URL of the service, or None if the service is not served"""
Expand Down

0 comments on commit b961a85

Please sign in to comment.