def register_service_meta(self, service_meta: ServiceMeta): """Register service meta """ from playwell.service.config import playwell_api response = requests.post(urljoin(playwell_api, "/v1/service_meta/register"), json={ "name": service_meta.name, "message_bus": service_meta.message_bus, "config": service_meta.config }) return Result.from_response(response)
def refresh_all(self): """Refresh all service meta from API """ from playwell.service.config import playwell_api result = Result.from_response( requests.get(urljoin(playwell_api, "/v1/service_meta/all"))) if not result.is_ok(): raise PlaywellServiceException( "Refresh service meta failure, the API return: %s" % result) self._all_service_meta = { service_meta["name"]: ServiceMeta(**service_meta) for service_meta in result.data["services"] }
def register_input_message_bus(self): """注册input message bus """ from playwell.service.config import input_message_bus_config if not input_message_bus_config: return if self._input_message_bus is not None: raise PlaywellServiceException( "The input message bus has already been registered!") from playwell.service.config import playwell_api response = requests.post(urljoin(playwell_api, "/v1/message_bus/register"), json={"config": input_message_bus_config}) result = Result.from_response(response) if not result.is_ok(): raise PlaywellServiceException( "Register message bus error, the API return %s" % result) response = requests.post( urljoin(playwell_api, "/v1/message_bus/open"), json={"name": input_message_bus_config["name"]}) result = Result.from_response(response) if not result.is_ok() and result.error_code != "already_opened": raise PlaywellServiceException( "Open message bus error, the API return: %s" % result) from playwell.service.message.http import HttpMessageBus self._input_message_bus = HttpMessageBus( name=input_message_bus_config["name"], clazz=input_message_bus_config["class"], alive=True, opened=True, available=True, config=input_message_bus_config) self._input_message_bus.init_web_server() self._all_playwell_message_bus[ self._input_message_bus.name] = self._input_message_bus
def refresh_all(self): from playwell.service.config import playwell_api response = requests.get(urljoin(playwell_api, "/v1/message_bus/all")) result = Result.from_response(response) if not result.is_ok(): raise PlaywellServiceException( "Refresh message bus failure, the API return %s" % result) message_bus_data_seq = result.data.get("buses", []) all_names = set() for message_bus_data in message_bus_data_seq: name = message_bus_data["name"] clazz = message_bus_data["class"] all_names.add(name) if name in self._all_playwell_message_bus: message_bus = self._all_playwell_message_bus[name] if message_bus.opened != message_bus_data["opened"]: message_bus.opened = message_bus_data["opened"] if message_bus.alive != message_bus_data["alive"]: message_bus.alive = message_bus_data["alive"] if message_bus.available != message_bus_data["available"]: message_bus.available = message_bus_data["available"] else: from playwell.service.message.http import HttpMessageBus if clazz == HttpMessageBus.CLASS_NAME: message_bus = HttpMessageBus( name=name, clazz=clazz, alive=message_bus_data["alive"], opened=message_bus_data["opened"], available=message_bus_data["available"], config=message_bus_data.get("config", {})) self._all_playwell_message_bus[ message_bus.name] = message_bus for message_bus in list(self._all_playwell_message_bus.values()): if message_bus.name not in all_names: del self._all_playwell_message_bus[message_bus.name]