def execute_with_blob_streaming(self, command, file_path, filename=None,
                                    mime_type=None, **params):
        """Execute an Automation operation using a batch upload as an input

        Upload is streamed.
        """
        batch_id = self._generate_unique_id()
        tick = time.time()
        action = FileAction("Upload", file_path, filename)
        try:
            upload_result = self.upload(batch_id, file_path, filename=filename,
                                        mime_type=mime_type)
            upload_duration = int(time.time() - tick)
            action.transfer_duration = upload_duration
            # Use upload duration * 2 as Nuxeo transaction timeout
            tx_timeout = max(DEFAULT_NUXEO_TX_TIMEOUT, upload_duration * 2)
            log.trace('Using %d seconds [max(%d, 2 * upload time=%d)] as Nuxeo'
                      ' transaction timeout for batch execution of %s'
                      ' with file %s', tx_timeout, DEFAULT_NUXEO_TX_TIMEOUT,
                      upload_duration, command, file_path)
            if upload_duration > 0:
                log.trace("Speed for %d o is %d s : %f o/s", os.stat(file_path).st_size, upload_duration, os.stat(file_path).st_size / upload_duration)
            if upload_result['uploaded'] == 'true':
                result = self.execute_batch(command, batch_id, '0', tx_timeout,
                                          **params)
                return result
            else:
                raise ValueError("Bad response from batch upload with id '%s'"
                                 " and file path '%s'" % (batch_id, file_path))
        finally:
            self.end_action()
Esempio n. 2
0
    def execute_with_blob_streaming(self,
                                    command,
                                    file_path,
                                    filename=None,
                                    mime_type=None,
                                    **params):
        """Execute an Automation operation using a batch upload as an input

        Upload is streamed.
        """
        tick = time.time()
        action = FileAction("Upload", file_path, filename)
        try:
            batch_id = None
            if self.is_new_upload_api_available():
                try:
                    # Init resumable upload getting a batch id generated by the server
                    # This batch id is to be used as a resumable session id
                    batch_id = self.init_upload()['batchId']
                except NewUploadAPINotAvailable:
                    log.debug('New upload API is not available on server %s',
                              self.server_url)
                    self.new_upload_api_available = False
            if batch_id is None:
                # New upload API is not available, generate a batch id
                batch_id = self._generate_unique_id()
            upload_result = self.upload(batch_id,
                                        file_path,
                                        filename=filename,
                                        mime_type=mime_type)
            upload_duration = int(time.time() - tick)
            action.transfer_duration = upload_duration
            # Use upload duration * 2 as Nuxeo transaction timeout
            tx_timeout = max(DEFAULT_NUXEO_TX_TIMEOUT, upload_duration * 2)
            log.trace(
                'Using %d seconds [max(%d, 2 * upload time=%d)] as Nuxeo'
                ' transaction timeout for batch execution of %s'
                ' with file %s', tx_timeout, DEFAULT_NUXEO_TX_TIMEOUT,
                upload_duration, command, file_path)
            if upload_duration > 0:
                log.trace("Speed for %d o is %d s : %f o/s",
                          os.stat(file_path).st_size, upload_duration,
                          os.stat(file_path).st_size / upload_duration)
            # NXDRIVE-433: Compat with 7.4 intermediate state
            if upload_result.get('uploaded') is None:
                self.new_upload_api_available = False
            if upload_result.get('batchId') is not None:
                result = self.execute_batch(command, batch_id, '0', tx_timeout,
                                            **params)
                return result
            else:
                raise ValueError("Bad response from batch upload with id '%s'"
                                 " and file path '%s'" % (batch_id, file_path))
        except InvalidBatchException:
            self.cookie_jar.clear_session_cookies()
        finally:
            self.end_action()
    def execute_with_blob_streaming(self, command, file_path, filename=None,
                                    mime_type=None, **params):
        """Execute an Automation operation using a batch upload as an input

        Upload is streamed.
        """
        tick = time.time()
        action = FileAction("Upload", file_path, filename)
        retry = True
        num_retries = 0
        while retry and num_retries < 2:
            try:
                batch_id = None
                if self.is_new_upload_api_available():
                    try:
                        # Init resumable upload getting a batch id generated by the server
                        # This batch id is to be used as a resumable session id
                        batch_id = self.init_upload()['batchId']
                    except NewUploadAPINotAvailable:
                        log.debug('New upload API is not available on server %s', self.server_url)
                        self.new_upload_api_available = False
                if batch_id is None:
                    # New upload API is not available, generate a batch id
                    batch_id = self._generate_unique_id()
                upload_result = self.upload(batch_id, file_path, filename=filename,
                                            mime_type=mime_type)
                upload_duration = int(time.time() - tick)
                action.transfer_duration = upload_duration
                # Use upload duration * 2 as Nuxeo transaction timeout
                tx_timeout = max(DEFAULT_NUXEO_TX_TIMEOUT, upload_duration * 2)
                log.trace('Using %d seconds [max(%d, 2 * upload time=%d)] as Nuxeo'
                          ' transaction timeout for batch execution of %s'
                          ' with file %s', tx_timeout, DEFAULT_NUXEO_TX_TIMEOUT,
                          upload_duration, command, file_path)
                if upload_duration > 0:
                    log.trace("Speed for %d o is %d s : %f o/s", os.stat(file_path).st_size, upload_duration, os.stat(file_path).st_size / upload_duration)
                # NXDRIVE-433: Compat with 7.4 intermediate state
                if upload_result.get('uploaded') is None:
                    self.new_upload_api_available = False
                if upload_result.get('batchId') is not None:
                    result = self.execute_batch(command, batch_id, '0', tx_timeout,
                                              **params)
                    return result
                else:
                    raise ValueError("Bad response from batch upload with id '%s'"
                                     " and file path '%s'" % (batch_id, file_path))
            except InvalidBatchException:
                num_retries += 1
                self.cookie_jar.clear_session_cookies()
            finally:
                self.end_action()
    def execute_with_blob_streaming(self, command, file_path, filename=None,
                                    mime_type=None, **params):
        """Execute an Automation operation using a batch upload as an input

        Upload is streamed.
        """
        tick = time.time()
        action = FileAction("Upload", file_path, filename)
        try:
            batch_id = None
            if self.is_chunking_upload_available():
                try:
                    # Init resumable upload getting a batch id generated by the server
                    # This batch id is to be used as a resumable session id
                    batch_id = self.init_upload()['batchId']
                except Exception:
                    log.debug('Chunking upload API is not available on server %s', self.server_url)
                    self.chunking_upload_available = False
            if batch_id is None:
                # Chunking upload API is not available, generate a batch id
                batch_id = self._generate_unique_id()
            upload_result = self.upload(batch_id, file_path, filename=filename,
                                        mime_type=mime_type)
            upload_duration = int(time.time() - tick)
            action.transfer_duration = upload_duration
            # Use upload duration * 2 as Nuxeo transaction timeout
            tx_timeout = max(DEFAULT_NUXEO_TX_TIMEOUT, upload_duration * 2)
            log.trace('Using %d seconds [max(%d, 2 * upload time=%d)] as Nuxeo'
                      ' transaction timeout for batch execution of %s'
                      ' with file %s', tx_timeout, DEFAULT_NUXEO_TX_TIMEOUT,
                      upload_duration, command, file_path)
            if upload_duration > 0:
                log.trace("Speed for %d o is %d s : %f o/s", os.stat(file_path).st_size, upload_duration, os.stat(file_path).st_size / upload_duration)
            if upload_result['uploaded'] == 'true':
                result = self.execute_batch(command, batch_id, '0', tx_timeout,
                                          **params)
                return result
            else:
                raise ValueError("Bad response from batch upload with id '%s'"
                                 " and file path '%s'" % (batch_id, file_path))
        finally:
            self.end_action()