def __bulk_add(self, request, response): """ Function for bulk adding tasks. Args: request: taskqueue_service_pb.TaskQueueBulkAddRequest response: taskqueue_service_pb.TaskQUeueBulkAddResponse Raises: apiproxy_error.ApplicationError """ if request.add_request_size() == 0: return now = datetime.datetime.utcfromtimestamp(time.time()) # Assign names if needed and validate tasks error_found = False for add_request in request.add_request_list(): task_result = response.add_taskresult() result = tq_lib.verify_task_queue_add_request( add_request.app_id(), add_request, now) # Tasks go from SKIPPED to OK once its run. If there are # any failures from other tasks then we pass this request # back as skipped. if result == taskqueue_service_pb.TaskQueueServiceError.SKIPPED: task_name = None if add_request.has_task_name(): task_name = add_request.task_name() namespaced_name = tq_lib.choose_task_name( add_request.app_id(), add_request.queue_name(), user_chosen=task_name) add_request.set_task_name(namespaced_name) task_result.set_chosen_task_name(namespaced_name) else: error_found = True task_result.set_result(result) if error_found: return for add_request, task_result in zip(request.add_request_list(), response.taskresult_list()): # TODO make sure transactional tasks are handled first at the AppServer # level, and not at the taskqueue server level # if add_request.has_transaction() is true. try: self.__enqueue_push_task(add_request) except apiproxy_errors.ApplicationError, e: task_result.set_result(e.application_error) else: task_result.set_result( taskqueue_service_pb.TaskQueueServiceError.OK)
def __bulk_add(self, request, response): """ Function for bulk adding tasks. Args: request: taskqueue_service_pb.TaskQueueBulkAddRequest response: taskqueue_service_pb.TaskQUeueBulkAddResponse Raises: apiproxy_error.ApplicationError """ if request.add_request_size() == 0: return now = datetime.datetime.utcfromtimestamp(time.time()) # Assign names if needed and validate tasks error_found = False for add_request in request.add_request_list(): task_result = response.add_taskresult() result = tq_lib.verify_task_queue_add_request(add_request.app_id(), add_request, now) # Tasks go from SKIPPED to OK once its run. If there are # any failures from other tasks then we pass this request # back as skipped. if result == taskqueue_service_pb.TaskQueueServiceError.SKIPPED: task_name = None if add_request.has_task_name(): task_name = add_request.task_name() namespaced_name = tq_lib.choose_task_name(add_request.app_id(), add_request.queue_name(), user_chosen=task_name) add_request.set_task_name(namespaced_name) task_result.set_chosen_task_name(namespaced_name) else: error_found = True task_result.set_result(result) if error_found: return for add_request, task_result in zip(request.add_request_list(), response.taskresult_list()): # TODO make sure transactional tasks are handled first at the AppServer # level, and not at the taskqueue server level # if add_request.has_transaction() is true. try: self.__enqueue_push_task(add_request) except apiproxy_errors.ApplicationError, e: task_result.set_result(e.application_error) else: task_result.set_result(taskqueue_service_pb.TaskQueueServiceError.OK)
def __bulk_add(self, source_info, request, response): """ Function for bulk adding tasks. Args: source_info: A dictionary containing the application, module, and version ID that is sending this request. request: taskqueue_service_pb.TaskQueueBulkAddRequest. response: taskqueue_service_pb.TaskQueueBulkAddResponse. Raises: apiproxy_error.ApplicationError. """ if request.add_request_size() == 0: return now = datetime.datetime.utcfromtimestamp(time.time()) # Assign names if needed and validate tasks. error_found = False for add_request in request.add_request_list(): task_result = response.add_taskresult() if (add_request.has_mode() and add_request.mode() == taskqueue_service_pb.TaskQueueMode.PULL): queue = self.get_queue(add_request.app_id(), add_request.queue_name()) if not isinstance(queue, (PullQueue, PostgresPullQueue)): task_result.set_result( TaskQueueServiceError.INVALID_QUEUE_MODE) error_found = True continue encoded_payload = base64.urlsafe_b64encode(add_request.body()) task_info = { 'payloadBase64': encoded_payload, 'leaseTimestamp': add_request.eta_usec() } if add_request.has_task_name(): task_info['id'] = add_request.task_name() if add_request.has_tag(): task_info['tag'] = add_request.tag() new_task = Task(task_info) queue.add_task(new_task) task_result.set_result(TaskQueueServiceError.OK) task_result.set_chosen_task_name(new_task.id) continue result = tq_lib.verify_task_queue_add_request( add_request.app_id(), add_request, now) # Tasks go from SKIPPED to OK once they're run. If there are # any failures from other tasks then we pass this request # back as skipped. if result == TaskQueueServiceError.SKIPPED: task_name = None if add_request.has_task_name(): task_name = add_request.task_name() namespaced_name = tq_lib.choose_task_name( add_request.app_id(), add_request.queue_name(), user_chosen=task_name) add_request.set_task_name(namespaced_name) task_result.set_chosen_task_name(namespaced_name) else: error_found = True task_result.set_result(result) if error_found: return for add_request, task_result in zip(request.add_request_list(), response.taskresult_list()): if (add_request.has_mode() and add_request.mode() == taskqueue_service_pb.TaskQueueMode.PULL): continue try: self.__enqueue_push_task(source_info, add_request) except apiproxy_errors.ApplicationError as error: task_result.set_result(error.application_error) except InvalidTarget as e: logger.error(e.message) task_result.set_result(TaskQueueServiceError.INVALID_REQUEST) else: task_result.set_result(TaskQueueServiceError.OK)
def __bulk_add(self, source_info, request, response): """ Function for bulk adding tasks. Args: source_info: A dictionary containing the application, module, and version ID that is sending this request. request: taskqueue_service_pb.TaskQueueBulkAddRequest. response: taskqueue_service_pb.TaskQueueBulkAddResponse. Raises: apiproxy_error.ApplicationError. """ if request.add_request_size() == 0: return now = datetime.datetime.utcfromtimestamp(time.time()) # Assign names if needed and validate tasks. error_found = False for add_request in request.add_request_list(): task_result = response.add_taskresult() if (add_request.has_mode() and add_request.mode() == taskqueue_service_pb.TaskQueueMode.PULL): queue = self.get_queue(add_request.app_id(), add_request.queue_name()) if not isinstance(queue, PullQueue): task_result.set_result(TaskQueueServiceError.INVALID_QUEUE_MODE) error_found = True encoded_payload = base64.urlsafe_b64encode(add_request.body()) task_info = {'payloadBase64': encoded_payload, 'leaseTimestamp': add_request.eta_usec()} if add_request.has_task_name(): task_info['id'] = add_request.task_name() if add_request.has_tag(): task_info['tag'] = add_request.tag() new_task = Task(task_info) queue.add_task(new_task) task_result.set_result(TaskQueueServiceError.OK) task_result.set_chosen_task_name(new_task.id) continue result = tq_lib.verify_task_queue_add_request(add_request.app_id(), add_request, now) # Tasks go from SKIPPED to OK once they're run. If there are # any failures from other tasks then we pass this request # back as skipped. if result == TaskQueueServiceError.SKIPPED: task_name = None if add_request.has_task_name(): task_name = add_request.task_name() namespaced_name = tq_lib.choose_task_name(add_request.app_id(), add_request.queue_name(), user_chosen=task_name) add_request.set_task_name(namespaced_name) task_result.set_chosen_task_name(namespaced_name) else: error_found = True task_result.set_result(result) if error_found: return for add_request, task_result in zip(request.add_request_list(), response.taskresult_list()): if (add_request.has_mode() and add_request.mode() == taskqueue_service_pb.TaskQueueMode.PULL): continue try: self.__enqueue_push_task(source_info, add_request) except apiproxy_errors.ApplicationError as error: task_result.set_result(error.application_error) except InvalidTarget as e: logger.error(e.message) task_result.set_result(TaskQueueServiceError.INVALID_REQUEST) else: task_result.set_result(TaskQueueServiceError.OK)
def __bulk_add(self, source_info, request, response): """ Function for bulk adding tasks. Args: source_info: A dictionary containing the application, module, and version ID that is sending this request. request: taskqueue_service_pb2.TaskQueueBulkAddRequest. response: taskqueue_service_pb2.TaskQueueBulkAddResponse. Raises: apiproxy_error.ApplicationError. """ if len(request.add_request) == 0: return now = datetime.datetime.utcfromtimestamp(time.time()) # Assign names if needed and validate tasks. error_found = False namespaced_names = [None for _ in request.add_request] for index, add_request in enumerate(request.add_request): task_result = response.taskresult.add() if (add_request.HasField("mode") and add_request.mode == taskqueue_service_pb2.TaskQueueMode.PULL): queue = self.get_queue(add_request.app_id, add_request.queue_name) if not isinstance(queue, (PullQueue, PostgresPullQueue)): task_result.result = TaskQueueServiceError.INVALID_QUEUE_MODE error_found = True continue encoded_payload = base64.urlsafe_b64encode(add_request.body) task_info = {'payloadBase64': encoded_payload, 'leaseTimestamp': add_request.eta_usec} if add_request.HasField("task_name"): task_info['id'] = add_request.task_name if add_request.HasField("tag"): task_info['tag'] = add_request.tag new_task = Task(task_info) queue.add_task(new_task) task_result.result = TaskQueueServiceError.OK task_result.chosen_task_name = new_task.id continue result = tq_lib.verify_task_queue_add_request(add_request.app_id, add_request, now) # Tasks go from SKIPPED to OK once they're run. If there are # any failures from other tasks then we pass this request # back as skipped. if result == TaskQueueServiceError.SKIPPED: task_name = None if add_request.HasField("task_name"): task_name = add_request.task_name namespaced_name = tq_lib.choose_task_name(add_request.app_id, add_request.queue_name, user_chosen=task_name) add_request.task_name = namespaced_name # Wait until the result is known before setting the chosen task name on # the response. namespaced_names[index] = namespaced_name else: error_found = True task_result.result = result if error_found: return for index, add_request in enumerate(request.add_request): task_result = response.taskresult[index] if (add_request.HasField("mode") and add_request.mode == taskqueue_service_pb2.TaskQueueMode.PULL): continue try: self.__enqueue_push_task(source_info, add_request) except ApplicationError as error: task_result.result = error.application_error except InvalidTarget as e: logger.error(e.message) task_result.result = TaskQueueServiceError.INVALID_REQUEST else: task_result.result = TaskQueueServiceError.OK if namespaced_names[index] is not None: task_result.chosen_task_name = namespaced_names[index]