async def _collect_states_to_replicate(self): states_to_report = [] for key in self._redis_client: try: idval, state_type = self._parse_key(key) except ValueError as err: logging.debug(err) continue state_scope = self._serdes[state_type].state_scope device_id = self.make_scoped_device_id(idval, state_scope) in_mem_key = self.make_mem_key(device_id, state_type) redis_version = self._redis_client.get_version(idval, state_type) if in_mem_key in self._state_versions and \ self._state_versions[in_mem_key] == redis_version: continue redis_state = self._redis_client.get(key) if self._serdes[state_type].state_format == PROTO_FORMAT: state_to_serialize = MessageToDict(redis_state) else: state_to_serialize = redis_state serialized_json_state = json.dumps(state_to_serialize) state_proto = State(type=state_type, deviceID=device_id, value=serialized_json_state.encode("utf-8"), version=redis_version) states_to_report.append(state_proto) if len(states_to_report) == 0: logging.debug("Not replicating state. No state has changed!") return None return ReportStatesRequest(states=states_to_report)
async def _collect_states_to_replicate(self): states_to_report = [] for redis_dict in self._redis_dicts: for key in redis_dict: device_id = make_scoped_device_id(key, redis_dict.state_scope) in_mem_key = make_mem_key(device_id, redis_dict.redis_type) redis_version = redis_dict.get_version(key) self._state_keys_from_current_iteration.add(in_mem_key) if in_mem_key in self._state_versions and \ self._state_versions[in_mem_key] == redis_version: continue redis_state = redis_dict.get(key) if redis_dict.state_format == PROTO_FORMAT: state_to_serialize = MessageToDict(redis_state) serialized_json_state = json.dumps(state_to_serialize) else: serialized_json_state = jsonpickle.encode(redis_state) state_proto = State(type=redis_dict.redis_type, deviceID=device_id, value=serialized_json_state.encode("utf-8"), version=redis_version) states_to_report.append(state_proto) if len(states_to_report) == 0: logging.debug("Not replicating state. No state has changed!") return None return ReportStatesRequest(states=states_to_report)
async def _collect_states_to_replicate(self): states_to_report = [] for redis_dict in self._redis_dicts: for key in redis_dict: redis_state = redis_dict.get(key) device_id = make_scoped_device_id(key, redis_dict.state_scope) in_mem_key = make_mem_key(device_id, redis_dict.redis_type) if redis_state is None: logging.debug( "Content of key %s is empty, skipping", in_mem_key, ) continue redis_version = redis_dict.get_version(key) self._state_keys_from_current_iteration.add(in_mem_key) if in_mem_key in self._state_versions and \ self._state_versions[in_mem_key] == redis_version: logging.debug( "key %s already read on this iteration, skipping", in_mem_key, ) continue try: if redis_dict.state_format == PROTO_FORMAT: state_to_serialize = MessageToDict(redis_state) serialized_json_state = json.dumps(state_to_serialize) else: serialized_json_state = jsonpickle.encode(redis_state) except Exception as e: # pylint: disable=broad-except logging.error( "Found bad state for %s for %s, not " "replicating this state: %s", key, device_id, e, ) continue state_proto = State( type=redis_dict.redis_type, deviceID=device_id, value=serialized_json_state.encode("utf-8", ), version=redis_version, ) logging.debug( "key with version, %s contains: %s", in_mem_key, serialized_json_state, ) states_to_report.append(state_proto) if len(states_to_report) == 0: logging.debug("Not replicating state. No state has changed!") return None return ReportStatesRequest(states=states_to_report)
async def _collect_states(self) -> Optional[ReportStatesRequest]: states = [] for service in self._service_info_by_name: result = await self._get_state(service=service) if result is not None: states.extend(result) gw_state = self._get_gw_state() if gw_state is not None: states.append(gw_state) if len(states) == 0: return None return ReportStatesRequest(states=states, )
async def _collect_states(self) -> Optional[ReportStatesRequest]: states = [] for service in self._service_info_by_name: result = await self._get_operational_states(service=service) states.extend(result) gw_state = self._get_gw_state() if gw_state is not None: states.append(gw_state) if len(states) > 0: # ReportStates returns error on empty request return ReportStatesRequest(states=states) return None
async def test_checkin(proxy_cloud_connections=True): """Send checkin using either proxy or direct to cloud connection""" chan = ServiceRegistry.get_rpc_channel( 'state', ServiceRegistry.CLOUD, proxy_cloud_connections=proxy_cloud_connections, ) client = StateServiceStub(chan) # Construct a simple state to send for test value = json.dumps({"datetime": datetime.datetime.now()}, default=str) states = [ State(type="string_map", deviceID=snowflake.snowflake(), value=value.encode('utf-8')), ] request = ReportStatesRequest(states=states) timeout = 1000 await grpc_async_wrapper(client.ReportStates.future(request, timeout))
async def test(): # force bootstrap to be called on the first error self.state_reporter._error_handler.fail_threshold = 0 # use dummy state servicer get_rpc_mock.return_value = self.channel # mock out get_proxy_config get_proxy_config_mock.return_value = { "cloud_address": 1, "cloud_port": 2, "gateway_cert": 3, "gateway_key": 4, } # force cert_is_invalid to return false future = asyncio.Future() future.set_result(True) cert_is_invalid_mock.return_value = future # make schedule_bootstrap_now awaitable future = asyncio.Future() future.set_result(None) bootstrap_manager.schedule_bootstrap_now.return_value = future request = ReportStatesRequest(states=[State(type="fail")]) await self.state_reporter._send_to_state_service(request)