Skip to content
This repository was archived by the owner on Sep 22, 2023. It is now read-only.

Commit 599160d

Browse files
committed
feat: Implement progress bar for rescanning images using bgtask API
1 parent 99e1e3c commit 599160d

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

src/ai/backend/client/cli/admin/images.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
import click
55
from tabulate import tabulate
6+
from tqdm import tqdm
67

78
from . import admin
89
from ...compat import asyncio_run
9-
from ...session import Session
10+
from ...session import Session, AsyncSession
1011
from ..pretty import print_done, print_warn, print_fail, print_error
1112

1213

@@ -49,7 +50,7 @@ def rescan_images(registry: str) -> None:
4950
"""
5051

5152
async def rescan_images_impl(registry: str) -> None:
52-
async with Session() as session:
53+
async with AsyncSession() as session:
5354
try:
5455
result = await session.Image.rescan_images(registry)
5556
except Exception as e:
@@ -61,10 +62,27 @@ async def rescan_images_impl(registry: str) -> None:
6162
print_done("Started updating the image metadata from the configured registries.")
6263
task_id = result['task_id']
6364
bgtask = session.BackgroundTask(task_id)
64-
async with bgtask.listen_events() as response:
65-
async for ev in response:
66-
print(click.style(ev.event, fg='cyan', bold=True), json.loads(ev.data))
67-
print_done("Finished registry scanning.")
65+
try:
66+
completion_msg_func = lambda: print_done("Finished registry scanning.")
67+
with tqdm(unit='image') as pbar:
68+
async with bgtask.listen_events() as response:
69+
async for ev in response:
70+
data = json.loads(ev.data)
71+
if ev.event == 'task_updated':
72+
pbar.total = data['total_progress']
73+
pbar.write(data['message'])
74+
pbar.update(data['current_progress'] - pbar.n)
75+
elif ev.event == 'task_failed':
76+
error_msg = data['message']
77+
completion_msg_func = \
78+
lambda: print_fail(f"Error occurred: {error_msg}")
79+
elif ev.event == 'task_cancelled':
80+
pbar.write(ev.data)
81+
completion_msg_func = \
82+
lambda: print_warn("Registry scanning has been "
83+
"cancelled in the middle.")
84+
finally:
85+
completion_msg_func()
6886

6987
asyncio_run(rescan_images_impl(registry))
7088

src/ai/backend/client/func/image.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@
99

1010

1111
class Image:
12-
'''
12+
"""
1313
Provides a shortcut of :func:`Admin.query()
1414
<ai.backend.client.admin.Admin.query>` that fetches the information about
1515
available images.
16-
'''
16+
"""
1717

1818
session = None
19-
'''The client session instance that this function class is bound to.'''
19+
"""The client session instance that this function class is bound to."""
2020

2121
@api_function
2222
@classmethod
2323
async def list(cls,
2424
operation: bool = False,
2525
fields: Iterable[str] = None) -> Sequence[dict]:
26-
'''
26+
"""
2727
Fetches the list of registered images in this cluster.
28-
'''
28+
"""
2929

3030
if fields is None:
3131
fields = (
@@ -56,7 +56,7 @@ async def list(cls,
5656
async def rescan_images(cls, registry: str):
5757
q = 'mutation($registry: String) {' \
5858
' rescan_images(registry:$registry) {' \
59-
' ok msg' \
59+
' ok msg task_id' \
6060
' }' \
6161
'}'
6262
variables = {

src/ai/backend/client/request.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,6 @@ async def fetch_events(self) -> AsyncIterator[SSEMessage]:
651651
if len(msg_lines) == 0:
652652
continue
653653
event_type = 'message'
654-
event_data = ''
655654
event_id = None
656655
event_retry = None
657656
data_lines = []
@@ -678,6 +677,8 @@ async def fetch_events(self) -> AsyncIterator[SSEMessage]:
678677
id=event_id,
679678
retry=event_retry,
680679
)
680+
if event_type == 'server_close':
681+
break
681682
else:
682683
msg_lines.append(line.decode('utf-8'))
683684

0 commit comments

Comments
 (0)