Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit 71dc421

Browse files
committed
Implement SmartExporterServiceStub
1 parent dd06a79 commit 71dc421

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

  • packages/jumpstarter/jumpstarter/client

packages/jumpstarter/jumpstarter/client/grpc.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from __future__ import annotations
22

3-
from dataclasses import dataclass, field
3+
from collections import OrderedDict
4+
from dataclasses import InitVar, dataclass, field
45
from datetime import datetime, timedelta
56

67
import yaml
78
from google.protobuf import duration_pb2, field_mask_pb2, json_format
9+
from grpc import ChannelConnectivity
810
from grpc.aio import Channel
9-
from jumpstarter_protocol import client_pb2, client_pb2_grpc, kubernetes_pb2
11+
from jumpstarter_protocol import client_pb2, client_pb2_grpc, jumpstarter_pb2_grpc, kubernetes_pb2
1012
from pydantic import BaseModel, ConfigDict, Field, field_serializer
1113

1214
from jumpstarter.common.grpc import translate_grpc_exceptions
@@ -250,3 +252,25 @@ async def DeleteLease(self, *, name: str):
250252
name="namespaces/{}/leases/{}".format(self.namespace, name),
251253
)
252254
)
255+
256+
257+
@dataclass(frozen=True, slots=True)
258+
class SmartExporterServiceStub:
259+
channels: InitVar[list[Channel]]
260+
261+
__stubs: dict[Channel, jumpstarter_pb2_grpc.ExporterServiceStub] = field(
262+
init=False,
263+
default_factory=OrderedDict,
264+
)
265+
266+
def __post_init__(self, channels):
267+
for channel in channels:
268+
self.__stubs[channel] = jumpstarter_pb2_grpc.ExporterServiceStub(channel)
269+
270+
def __getattr__(self, name):
271+
for channel, stub in self.__stubs.items():
272+
# find the first channel that's ready
273+
if channel.get_state(try_to_connect=True) == ChannelConnectivity.READY:
274+
return getattr(stub, name)
275+
# or fallback to the last channel (via router)
276+
return getattr(next(reversed(self.__stubs.values())), name)

0 commit comments

Comments
 (0)