Skip to content

Commit

Permalink
feat(client): add update to Job
Browse files Browse the repository at this point in the history
PR Closed: #1174
  • Loading branch information
graczhual committed Dec 21, 2021
1 parent 614f0b0 commit c74eb30
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 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
from time import sleep
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,17 @@ 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)

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

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 c74eb30

Please sign in to comment.