Skip to content

Commit

Permalink
Merge pull request #550 from golemfactory/az/service-cluster-properties
Browse files Browse the repository at this point in the history
Read-only properties for some Service/Cluster private fields
  • Loading branch information
shadeofblue authored Jul 19, 2021
2 parents 9510f6f + 16e1398 commit d86894e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
35 changes: 35 additions & 0 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Unit tests for `yapapi.engine` module."""
from unittest.mock import Mock

import pytest

from yapapi import Golem
import yapapi.engine
import yapapi.rest


@pytest.fixture(autouse=True)
def mock_rest_configuration(monkeypatch):
"""Mock `yapapi.rest.Configuration`."""
monkeypatch.setattr(yapapi.rest, "Configuration", Mock)


@pytest.mark.parametrize(
"default_subnet, subnet_arg, expected_subnet",
[
(None, None, None),
("my-little-subnet", None, "my-little-subnet"),
(None, "whole-golem", "whole-golem"),
("my-little-subnet", "whole-golem", "whole-golem"),
],
)
def test_set_subnet_tag(default_subnet, subnet_arg, expected_subnet, monkeypatch):
"""Check that `subnet_tag` argument takes precedence over `yapapi.engine.DEFAULT_SUBNET`."""

monkeypatch.setattr(yapapi.engine, "DEFAULT_SUBNET", default_subnet)

if subnet_arg is not None:
golem = Golem(budget=1.0, subnet_tag=subnet_arg)
else:
golem = Golem(budget=1.0)
assert golem.subnet_tag == expected_subnet
5 changes: 5 additions & 0 deletions yapapi/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ def strategy(self) -> MarketStrategy:
"""Return the instance of `MarketStrategy` used by this engine."""
return self._strategy

@property
def subnet_tag(self) -> Optional[str]:
"""Return the name of the subnet used by this engine, or `None` if it is not set."""
return self._subnet

def emit(self, event: events.Event) -> None:
"""Emit an event to be consumed by this engine's event consumer."""
if self._wrapped_consumer:
Expand Down
24 changes: 23 additions & 1 deletion yapapi/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ def __init__(self, cluster: "Cluster", ctx: WorkContext):
# stopping the service externally (e.g., via `cluster.stop()`) from internal transition
# (e.g., via returning from `Service.run()`).

@property
def cluster(self) -> "Cluster":
"""Return the Cluster to which this service instance belongs."""
return self._cluster

@property
def id(self):
"""Return the id of this service instance.
Expand Down Expand Up @@ -281,7 +286,9 @@ def __init__(
self._engine = engine
self._service_class = service_class
self._payload = payload
self._expiration = expiration or datetime.now(timezone.utc) + DEFAULT_SERVICE_EXPIRATION
self._expiration: datetime = (
expiration or datetime.now(timezone.utc) + DEFAULT_SERVICE_EXPIRATION
)
self._task_ids = itertools.count(1)
self._stack = AsyncExitStack()
self._respawn_unstarted_instances = respawn_unstarted_instances
Expand All @@ -292,6 +299,21 @@ def __init__(
self._instance_tasks: Set[asyncio.Task] = set()
"""Set of asyncio tasks that run spawn_service()"""

@property
def expiration(self) -> datetime:
"""Return the expiration datetime for agreements related to services in this Cluster."""
return self._expiration

@property
def payload(self) -> Payload:
"""Return the service runtime definition for this Cluster."""
return self._payload

@property
def service_class(self) -> Type[Service]:
"""Return the class instantiated by all service instances in this Cluster."""
return self._service_class

def __repr__(self):
return (
f"Cluster {self.id}: {len(self.__instances)}x[Service: {self._service_class.__name__}, "
Expand Down

0 comments on commit d86894e

Please sign in to comment.