def _persistence_loop(self, save_on_empty=False,
                          warmup_wait=WARM_BUILD_TIME):
        if self._stop:
            LOG.info("Quitting k8s builder loop")
            raise utils.ThreadExit()
        first_event_time = None
        affected_tenants = set(self.affected_tenants)
        squash_time = warmup_wait if not save_on_empty else float('inf')
        while squash_time > 0:
            event = self._get_event(warmup_wait)
            if not first_event_time:
                first_event_time = time.time()
            if not save_on_empty:
                squash_time = (first_event_time + warmup_wait -
                               time.time())
            if event:
                LOG.debug('Got save event from queue')
                affected_tenants |= self._process_event(event)
            elif save_on_empty:
                break

        if affected_tenants:
            LOG.info('Saving trees for tenants: %s', affected_tenants)
            try:
                # Save procedure can be context switched at this point
                self._save_trees(affected_tenants)
                self.affected_tenants = set()
            except Exception:
                LOG.error(traceback.format_exc())
                # Put the affected tenants back to the list since we couldn't
                # persist their trees.
                self.affected_tenants |= affected_tenants
    def _observe_and_monitor_loop(self):
        if self._stop:
            LOG.info("Quitting k8s observe and monitor loop")
            raise utils.ThreadExit()

        if not self._observe_thread_state:
            self._start_observers(self._k8s_types_to_observe)

        exc = self._check_observers()
        if exc:
            for ts in self._observe_thread_state.values():
                ts['watch_stop'] = True
            if self.klient.watch:
                self.klient.stop_watch()
            self._observe_thread_state = {}
            self._needs_init = True
            raise exc
        time.sleep(MONITOR_LOOP_MAX_WAIT)