def run(self): """ Goes through each subscription and deletes items older than expiration_time + clean_up_time """ LOGGER.debug("Running Subscription Clean Up") subscriptions = KVStoreCollectionAccessObject( collection=SUBSCRIPTIONS_COLLECTION_NAME, session_key=self.session_key) searches = KVStoreCollectionAccessObject( collection=SEARCHES_COLLECTION_NAME, session_key=self.session_key) search_updates = KVStoreCollectionAccessObject( collection=SEARCH_UPDATES_COLLECTION_NAME, session_key=self.session_key) # clean all expired subscriptions self._clean_expired_subscriptions(subscriptions) # Build kvstore query to return any records not in valid subscription list # All subscriptions should be valid after above cleaning not_keys_query = not_subscription_keys_query(subscriptions) # Delete any search_updates, subscription_credentials that don't belong to valid subscriptions search_updates.delete_items_by_query(not_keys_query) self._clean_user_namespaced_items(not_keys_query) # Clean up any searches that have not been updated in a multiple of the clean_up time self._clean_expired_searches(searches) LOGGER.debug("Completed Subscription Clean up")
def delete_all_devices_of_type_for_user(user, app_type, authtoken, system_authtoken): """ Removes all devices of a given type from a single user in the kvstore """ kvstore = KvStore(constants.REGISTERED_DEVICES_COLLECTION_NAME, authtoken, owner=user) r, devices = kvstore.get_all_items() devices = json.loads(devices) kvstore.delete_items_by_query({'device_type': app_type}) for device in devices: if device['device_type'] == app_type: delete_device_from_spacebridge(device['device_id'], system_authtoken)
def _clean_user_namespaced_items(self, query): users = get_all_mobile_users(self.session_key) timestamp_before = time_utils.get_current_timestamp( ) - self.clean_up_time LOGGER.debug( 'Deleting credentials older than last_update_time=%s, users=%s', timestamp_before, len(users)) for owner in users: credentials = KVStoreCollectionAccessObject( collection=SUBSCRIPTION_CREDENTIALS_COLLECTION_NAME, owner=owner, session_key=self.session_key) credentials.delete_items_by_query(query)
def delete(self, request): """ delete a companion app using comma separated list of app_ids. e.g. app_ids=id1,id2,id3 """ session_token = request['session']['authtoken'] app_ids = extract_parameter(request['query'], self.APP_IDS_LABEL, "query") app_ids_lst = app_ids.strip().split(',') query = {constants.OR_OPERATOR: [{constants.KEY: app_id} for app_id in app_ids_lst]} kvstore_client = KVStoreCollectionAccessObject(collection=self.COMPANION_APPS_COLLECTION_NAME, session_key=session_token) r, content = kvstore_client.delete_items_by_query(query) return { 'payload': content.decode('utf-8'), 'status': r.status }