def h1(sv, pv): global _dev if _dev is None: return # benchmark global _forward_set if deep_get(sv, "brightness.intent") == 0.1 and not _forward_set: resp, e = patch_spec(*_measure, {"obs": {"forward_leaf": time.time()}}) if e is None: _forward_set = True status = lifx.get(_dev) power = status.get("power", 0) color = list(status.get("color", [1, 1, 1, 1])) p, b = deep_get(sv, "power.intent"), deep_get(sv, "brightness.intent") if p is not None: power = convert["power"]["to"](p) if b is not None: color[2] = convert["brightness"]["to"](b) # benchmark lifx.put(_dev, power, color)
def send_request(auri, s: dict): global measure resp, e = patch_spec(*auri, s) if e is not None: print(f"bench: encountered error {e} \n {resp}") exit()
def run(self): while True: if self.stop_flag: break status = lifx.get(self.dev) if status is None: continue p, b = status.get("power", {}), status.get("color", {})[2] p, b = convert["power"]["from"](p), convert["brightness"]["from"]( b) patch = { "control": { "power": { "status": p, }, "brightness": { "status": b, }, } } resp, e = patch_spec(*digi.auri, patch) if e is not None: logger.error(f"unable to update status {e}") # benchmark global _backward_set if b == 0.1 and not _backward_set: resp, e = patch_spec(*_measure, {"obs": { "backward_leaf": time.time() }}) if e is None: _backward_set = True time.sleep(self.interval)
def _prune_mounts(mounts, meta): rv = meta["resourceVersion"] while True: to_prune = list() for gvr_str, models in mounts.items(): if len(models) == 0: to_prune.append(gvr_str) if len(to_prune) == 0: return patch = {"mounts": {gvr_str: None for gvr_str in to_prune}} _, e = util.patch_spec(g, v, r, n, ns, patch, rv=rv) if e is None: self._logger.info(f"Prune mount: {patch}") return elif e.status != 409: self._logger.warning(f"Prune mount failed due to {e}") return self._logger.info(f"Prune mount will retry due to: {e}") spec, rv, _ = util.get_spec(g, v, r, n, ns) mounts = spec.get("mount", {})
def _sync_to_parent(group, version, plural, name, namespace, meta, spec, diff, attrs_to_trim=None, *args, **kwargs): _, _ = args, kwargs # propagation from child retries until succeed while True: parent, prv, pgn = util.get_spec(g, v, r, n, ns) # check if child exists mounts = parent.get("mount", {}) gvr_str = util.gvr(group, version, plural) nsn_str = util.spaced_name(name, namespace) if (gvr_str not in mounts or (nsn_str not in mounts[gvr_str] and name not in mounts[gvr_str])): self._logger.warning( f"unable to find the {nsn_str} or {name} in the {parent}" ) return models = mounts[gvr_str] n_ = name if name in models else nsn_str if spec is None: parent_patch = None # will convert to json null else: if models[n_].get("mode", "hide") == "hide": if attrs_to_trim is None: attrs_to_trim = set() attrs_to_trim.add("mount") # TBD rename to _gen_parent_spec parent_patch = _gen_parent_patch(spec, diff, attrs_to_trim) # add roots if parent_patch is None: # only child if len(mounts[gvr_str]) == 1: parent_patch = {"mount": {gvr_str: None}} else: parent_patch = {"mount": {gvr_str: {n_: None}}} else: parent_patch = { "mount": { gvr_str: { n_: { "spec": parent_patch, "generation": meta["generation"], } } } } # maybe rejected if parent has been updated; # continue to try until succeed resp, e = util.patch_spec(g, v, r, n, ns, parent_patch, rv=prv) if e is not None: self._logger.warning( f"Failed to sync to parent due to {e}") if e.status != 409: return # time.sleep(1) else: new_gen = resp["metadata"]["generation"] if pgn + 1 == new_gen: self._parent_skip_gen = new_gen break