def _EnqueueMasterRecallTask(self, owner_email, message_criteria, task_key_id): """Add master recall task with error handling. Args: owner_email: String email address of user running this recall. message_criteria: String criteria (message-id) to recall. task_key_id: Int unique id of the parent task. Raises: re-raises any task queue errors. """ task_name = '%s_%s' % (view_utils.CreateSafeUserEmailForTaskName( owner_email), view_utils.GetCurrentDateTimeForTaskName()) master_task = Task(name=task_name, params={ 'owner_email': owner_email, 'task_key_id': task_key_id, 'message_criteria': message_criteria }, target='0.recall-backend', url='/backend/recall_messages') try: master_task.add(queue_name='recall-messages-queue') except TaskQueueError: view_utils.FailRecallTask( task_key_id=task_key_id, reason_string='Failed to enqueue master task.') raise
def _IncrementRetrievalEndedTasksCount(self): """Increment sharded counter when each user-retrieval-task ends. Returns: Final value of the counter after the increment of 1. """ retrieval_ended_count = sharded_counter.IncrementCounterAndGetCount( name=view_utils.MakeRetrievalEndedCounterName(self._task_key_id)) if not retrieval_ended_count: view_utils.FailRecallTask( task_key_id=self._task_key_id, reason_string='Unexpected retrieval_ended count: %s.' % retrieval_ended_count, raise_exception=True) return retrieval_ended_count
def _AddUserRetrievalTask(self, task): """Helper to transactionally add the tasks. Do not set transactional=True in the Task because Push Queues have a 5 Task per add limit when transactional=True per: http://developers.google.com/appengine/docs/python/taskqueue/overview-push Args: task: Task or list of Tasks to retrieve domain users. Raises: re-raises any taskqueue errors raised. """ try: Queue('retrieve-users-queue').add(task=task) except TaskQueueError: view_utils.FailRecallTask( task_key_id=self._task_key_id, reason_string='Failed to enqueue retrieve users tasks.') raise
def _RetrieveAndAddUsers(self, email_prefix, owner_email): """Search domain users and add returned users to the data store. Each tasks searches a domain user subset based on the email_prefix. Args: email_prefix: String with the first n characters of an email address. owner_email: String email address of the user who owns the task. The search will occur in this users domain. """ try: for user_tuples_page in user_retriever.DomainUserRetriever( owner_email=owner_email, user_domain=view_utils.GetUserDomain(owner_email), search_query='email:%s*' % email_prefix).RetrieveDomainUsers(): self._AddUserRecordsPage(user_tuples=user_tuples_page) except recall_errors.MessageRecallError: view_utils.FailRecallTask( task_key_id=self._task_key_id, reason_string='Failure retrieving users.') raise
def _AddUserRecordsPage(self, user_tuples): """Helper to add a user record to the data model for this recall task. User retrieval is optimized for minimum rpc's. Users are retrieved using the API by the largest possible page size (500 users). But, users are efficiently added to NDB in batches of 100. Args: user_tuples: List of tuples (1 for each user) with a String email address and the suspended status of the users to check. """ current_user_index = 0 user_count = len(user_tuples) users_to_add = [] for user_email, is_suspended in user_tuples: if recall_task.RecallTaskModel.IsTaskAborted(self._task_key_id): return if domain_user.DomainUserToCheckModel.IsUserEmailEntityInTask( self._task_key_id, user_email): user_count -= 1 continue user_to_add = domain_user.DomainUserToCheckModel( recall_task_id=self._task_key_id, user_email=user_email) if is_suspended: user_to_add.message_state = domain_user.MESSAGE_UNKNOWN user_to_add.user_state = domain_user.USER_SUSPENDED users_to_add.append(user_to_add) if (len(users_to_add) == _USER_PUT_MULTI_BATCH_SIZE or (current_user_index == (user_count - 1) and users_to_add)): self._AddUserRecordsToDB(users_to_add) users_to_add = [] current_user_index += 1 if users_to_add: view_utils.FailRecallTask( task_key_id=self._task_key_id, reason_string='Unexpectedly found %s users not stored.' % len(users_to_add), raise_exception=True)