Esempio n. 1
0
    def get_result(self, wait=False, timeout=None):
        """
        Attempt to load the result for this upload task. After returning
        from this method without an exception raised, the information for
        the task is available through the various properties.

        :param bool wait: Whether to wait for the task to complete or raise
            a :exc:`~descarteslabs.common.tasks.futuretask.TransientResultError`
            if the task hasn't completed yet.

        :param int timeout: How long to wait in seconds for the task to complete, or
            :const:`None` to wait indefinitely.

        :raises NotFoundError: When the upload id or task id does not exist.

        :raises TransientResultError: When the result is not ready yet (and not waiting).

        :raises TimeoutError: When the timeout has been reached (if waiting and set).
        """

        # Things are complicated compared to FutureTask, because the upload task
        # can have completed, but the BigQuery upload process may not yet have terminated.
        # We wait for termination of the BigQuery upload.
        if (self._task_result is None
                or self._task_result.status == self.PENDING or
            (self._task_result.status == self.SUCCESS and
             ('load' not in self._task_result or self._task_result.load.state
              in (self.PENDING, self.RUNNING)))):  # noqa
            if self._upload_id:
                id = self._upload_id
            elif self.tuid:
                id = self.tuid
            else:
                raise ValueError(
                    "Cannot retrieve upload task without task id or upload id")

            start = time.time()

            while timeout is None or (time.time() - start) < timeout:
                result = self.client.get_upload_result(self.guid,
                                                       id,
                                                       pending=True)
                if result.data.attributes.status != self.PENDING:
                    # we have actual task results
                    id = self.tuid = result.data.id
                self._task_result = result.data.attributes
                if (self._task_result.status == self.FAILURE
                        or (self._task_result.status == self.SUCCESS
                            and 'load' in self._task_result
                            and self._task_result.load.state
                            not in (self.PENDING, self.RUNNING))):  # noqa
                    break
                if not wait:
                    raise TransientResultError()

                time.sleep(self.COMPLETION_POLL_INTERVAL_SECONDS)
            else:
                raise TimeoutError()
Esempio n. 2
0
    def get_file(self, file_obj):
        """Download the exported Storage object to a local file.

        :param str file_obj: A file-like object or name of file to download into.

        :raises TransientResultError: If the export hasn't completed yet.
        """
        if self.key is None:
            raise TransientResultError()
        else:
            return Storage().get_file(self.key, file_obj)
Esempio n. 3
0
    def get_result(self, wait=False, timeout=None):
        """
        Attempt to load the result for this upload task. After returning
        from this method without an exception raised, the information for
        the task is available through the various properties.

        Note that depending on the ``status`` and the ``load_state`` not
        all attributes may be available.

        Parameters
        ----------
        wait : bool
            Whether to wait for the task to complete or raise
            a `TransientResultException` if the task hasn't completed
            yet.
        timeout : int
            How long to wait in seconds for the task to complete, or
            ``None`` to wait indefinitely.

        Raises
        ------
        `TransientResultException`
            When the result is not ready yet (and not waiting).

        RuntimeError
            When the timeout has been reached (if waiting and set).
        """
        if self._task_result is None:
            if self._upload_id is None:
                raise ValueError(
                    "Cannot retrieve upload task without upload id")

            start = time.time()

            while timeout is None or (time.time() - start) < timeout:
                try:
                    result = self.client.get_upload_result(
                        self.guid, self._upload_id)
                    self.tuid = result.data.id
                    self._task_result = result.data.attributes
                except NotFoundError:
                    if not wait:
                        raise TransientResultError()
                else:
                    break

                time.sleep(self.COMPLETION_POLL_INTERVAL_SECONDS)
            else:
                raise TimeoutError()
    def get_result(self, wait=False, timeout=None):
        """
        Attempt to load the result for this export task. After returning
        from this method without an exception raised, the information for
        the task is available through the various properties.

        :param bool wait: Whether to wait for the task to complete or raise
            a :exc:`~descarteslabs.common.tasks.futuretask.TransientResultError`
            if the task hasn't completed yet.

        :param int timeout: How long to wait in seconds for the task to complete, or
            :const:`None` to wait indefinitely.

        :raises TransientResultError: When the result is not ready yet (and not waiting).

        :raises TimeoutError: When the timeout has been reached (if waiting and set).
        """

        # We have to go through the vector service since we don't
        # own the task group
        if self._task_result is None:
            start = time.time()

            while timeout is None or (time.time() - start) < timeout:
                try:
                    result = self.client.get_export_result(
                        self.guid, self.tuid)
                    self._task_result = result.data.attributes
                    self._set_key(None)
                except NotFoundError:
                    if not wait:
                        raise TransientResultError()
                else:
                    break

                time.sleep(self.COMPLETION_POLL_INTERVAL_SECONDS)
            else:
                raise TimeoutError()
Esempio n. 5
0
    def get_result(self, wait=False, timeout=None):
        """
        Attempt to load the result for this upload task. After returning
        from this method without an exception raised, the information for
        the task is available through the various properties.

        Parameters
        ----------
        wait : bool
            Whether to wait for the task to complete or raise
            a ``TransientResultError`` if the task hasn't completed
            yet.
        timeout : int
            How long to wait in seconds for the task to complete, or
            ``None`` to wait indefinitely.

        Raises
        ------
        ``TransientResultError``
            When the result is not ready yet (and not waiting).

        ``TimeoutError``
            When the timeout has been reached (if waiting and set).
        """

        # Things are complicated compared to FutureTask, because the upload task
        # can have completed, but the BigQuery upload process may not yet have terminated.
        # We wait for termination of the BigQuery upload.
        if self._task_result is None or (
                self._task_result.status == self.SUCCESS and
            ('load' not in self._task_result or self._task_result.load.state in
             (self._PENDING, self._RUNNING))):
            if self._upload_id:
                id = self._upload_id
            elif self.tuid:
                id = self.tuid
            else:
                raise ValueError(
                    "Cannot retrieve upload task without task id or upload id")

            start = time.time()

            while timeout is None or (time.time() - start) < timeout:
                try:
                    result = self.client.get_upload_result(self.guid, id)
                    self.tuid = result.data.id
                    self._task_result = result.data.attributes
                except NotFoundError:
                    if not wait:
                        raise TransientResultError()
                else:
                    if (self._task_result.status == self.SUCCESS
                            and self._task_result.load.state
                            not in (self._PENDING, self._RUNNING)):  # noqa
                        break
                    if not wait:
                        raise TransientResultError()

                time.sleep(self.COMPLETION_POLL_INTERVAL_SECONDS)
            else:
                raise TimeoutError()