Skip to content

Commit

Permalink
feat(client): add update to Job
Browse files Browse the repository at this point in the history
  • Loading branch information
graczhual committed Dec 21, 2021
1 parent 27f0aa4 commit b1afefd
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion tensorbay/client/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@

"""Basic structures of asynchronous jobs."""

from typing import Any, Dict, Optional, Tuple, Type, TypeVar
import time
from typing import Any, Callable, Dict, Optional, Tuple, Type, TypeVar

from tensorbay.client.struct import Draft
from tensorbay.utility import AttrsMixin, ReprMixin, ReprType, attr, camel, common_loads

_JOB_UPDATE_INTERVAL = 5
_JOB_NOT_COMPLETE_STATUS = {"QUEUING", "PROCESSING"}


class Job(AttrsMixin, ReprMixin): # pylint: disable=too-many-instance-attributes
"""This class defines :class:`Job`.
Arguments:
job_updater: The function to update the information of the Job instance.
title: Title of the Job.
job_id: ID of the Job.
arguments: Arguments of the Job.
Expand Down Expand Up @@ -56,6 +61,7 @@ class Job(AttrsMixin, ReprMixin): # pylint: disable=too-many-instance-attribute

def __init__( # pylint: disable=too-many-arguments
self,
job_updater: Callable[[str], Dict[str, Any]],
title: str,
job_id: str,
arguments: Dict[str, Any],
Expand All @@ -67,6 +73,7 @@ def __init__( # pylint: disable=too-many-arguments
result: Optional[Dict[str, Any]],
description: Optional[str] = "",
) -> None:
self._job_updater = job_updater
self.title = title
self.job_id = job_id
self.arguments = arguments
Expand Down Expand Up @@ -114,6 +121,21 @@ def update(self, until_complete: bool = False) -> None:
until_complete: Whether to update job information until it is complete.
"""
job_info = self._job_updater(self.job_id)

while until_complete:
status = job_info["status"]
if status in _JOB_NOT_COMPLETE_STATUS:
time.sleep(_JOB_UPDATE_INTERVAL)
job_info = self._job_updater(self.job_id)
continue

break

self.finished_at = job_info["finishedAt"]
self.status = job_info["status"]
self.error_message = job_info["errorMessage"]
self._result = job_info["results"]

def abort(self) -> None:
"""Abort a :class:`Job`."""
Expand Down

0 comments on commit b1afefd

Please sign in to comment.