This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Added function "wait_for_results" that does just that (and returns re… #41
Open
mellesies
wants to merge
1
commit into
DEV3
Choose a base branch
from
feature/wait_for_results
base: DEV3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,25 @@ | |
This module is contains a base client. From this base client the container | ||
client (client used by master algorithms) and the user client are derived. | ||
""" | ||
from typing import Optional | ||
|
||
import sys | ||
import logging | ||
import pickle | ||
import time | ||
from tempfile import TemporaryFile | ||
import typing | ||
import jwt | ||
from numpy import isin | ||
import requests | ||
import pyfiglet | ||
import json as json_lib | ||
import itertools | ||
|
||
import dateutil | ||
import time | ||
from datetime import datetime | ||
|
||
|
||
from pathlib import Path | ||
from typing import Tuple | ||
|
@@ -622,6 +633,109 @@ def authenticate(self, username: str, password: str) -> None: | |
self.log.info(f'--> Retrieving additional user info failed!') | ||
self.log.debug(e) | ||
|
||
def wait_for_results(self, task_or_id, sleep=1): | ||
|
||
# Disable logging | ||
if isinstance(self.log, logging.Logger): | ||
prev_level = self.log.level | ||
self.log.setLevel(logging.WARN) | ||
elif isinstance(self.log, UserClient.Log): | ||
prev_level = self.log.enabled | ||
|
||
# Retrieve task details if necesary. | ||
if isinstance(task_or_id, int): | ||
task_id = task_or_id | ||
task = self.task.get(task_id) | ||
else: | ||
task = task_or_id | ||
task_id = task['id'] | ||
|
||
# Determine when the task was first started. We'll use the 1st result | ||
# to determine this, since task itself doesn't record this. | ||
if task['results']: | ||
result_id = task['results'][0]['id'] | ||
result = self.result.get(result_id) | ||
start = dateutil.parser.isoparse(result['assigned_at']) | ||
else: | ||
start = None | ||
result = None | ||
|
||
try: | ||
from rich.progress import Progress, ProgressColumn, SpinnerColumn, TimeElapsedColumn | ||
from rich.text import Text | ||
from rich.table import Column | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move try, except block to the import section |
||
|
||
class TrueTimeElapsedColumn(ProgressColumn): | ||
"""Renders time elapsed.""" | ||
def __init__(self, start: datetime, table_column: Optional[Column] = None): | ||
super().__init__(table_column) | ||
self.start = start | ||
|
||
def render(self, task: "Task") -> Text: | ||
"""Show time remaining.""" | ||
if self.start is None: | ||
return Text("-:--:--", style="progress.elapsed") | ||
|
||
# elapsed = task.finished_time if task.finished else task.elapsed | ||
if task.fields.get('my_finished_date'): | ||
now = dateutil.parser.isoparse(task.fields.get('my_finished_date')) | ||
else: | ||
now = datetime.now(start.tzinfo) | ||
|
||
delta = now - self.start | ||
d = str(delta).split('.')[0] | ||
return Text(d, style="progress.elapsed") | ||
|
||
cols = [ | ||
"[progress.description]{task.description}", | ||
SpinnerColumn(), | ||
"Time elapsed:", | ||
TrueTimeElapsedColumn(start), | ||
", Last check at: {task.fields[last_check]}" | ||
] | ||
|
||
if result: | ||
finished_at = result['finished_at'] | ||
else: | ||
finished_at = None | ||
|
||
with Progress(*cols) as progress: | ||
ptask = progress.add_task( | ||
f"Waiting for task {task_id}", | ||
last_check=time.strftime('%H:%M:%S'), | ||
my_finished_date=finished_at, | ||
) | ||
|
||
while not self.task.get(task_id)['complete']: | ||
time.sleep(sleep) | ||
|
||
progress.update( | ||
ptask, | ||
last_check=time.strftime('%H:%M:%S'), | ||
my_finished_date=None, | ||
) | ||
|
||
except ImportError: | ||
animation = itertools.cycle(['|', '/', '-', '\\']) | ||
t = time.time() | ||
|
||
while not self.task.get(task_id)['complete']: | ||
frame = next(animation) | ||
sys.stdout.write(f'\r{frame} Waiting for task {task_id} ({int(time.time()-t)}s)') | ||
sys.stdout.flush() | ||
time.sleep(sleep) | ||
|
||
sys.stdout.write('\rDone! ') | ||
|
||
|
||
# Re-enable logging | ||
if isinstance(self.log, logging.Logger): | ||
self.log.setLevel(prev_level) | ||
elif isinstance(self.log, UserClient.Log): | ||
self.log.enabled = prev_level | ||
|
||
return self.get_results(task_id=task_id) | ||
|
||
class Util(ClientBase.SubClient): | ||
"""Collection of general utilities""" | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imports need to be sorted