Skip to content

Commit c657cd9

Browse files
authored
Merge pull request #54 from InfuseAI/feature/sc-21679/primehub-sdk-make-the-job-stream-follow-print
Fix the log follow issue. Should show carriage return correctly
2 parents e80f710 + 38f9e08 commit c657cd9

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

primehub/deployments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def delete(self, id, **kwargs):
401401
# TODO: handle invalid pod
402402
@cmd(name='logs', description='Get deployment logs by id',
403403
optionals=[('pod', str), ('follow', toggle_flag), ('tail', int)])
404-
def logs(self, id, **kwargs) -> Iterator[str]:
404+
def logs(self, id, **kwargs) -> Iterator[bytes]:
405405
"""
406406
Get logs of a deployment
407407

primehub/jobs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def wait(self, id, **kwargs):
362362
return self.get(id)
363363

364364
@cmd(name='logs', description='Get job logs by id', optionals=[('follow', toggle_flag), ('tail', int)])
365-
def logs(self, id, **kwargs) -> Iterator[str]:
365+
def logs(self, id, **kwargs) -> Iterator[bytes]:
366366
"""
367367
Get logs of a job
368368

primehub/utils/display.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def wrapper_generator(gen):
8484
is_type_sent = False
8585
for x in gen:
8686
if not is_type_sent:
87-
yield isinstance(x, str)
87+
yield isinstance(x, (str, bytes))
8888
is_type_sent = True
8989
yield x
9090

@@ -125,6 +125,8 @@ def display_single(self, action: dict, value: Any, file: TextIO):
125125
logger.debug('display-single')
126126
if isinstance(value, str):
127127
print(value, file=file)
128+
elif isinstance(value, bytes):
129+
file.write(value.decode())
128130
else:
129131
json.dump(value, file)
130132

@@ -182,5 +184,7 @@ def display_single(self, action: dict, value: Any, file: TextIO):
182184
logger.debug('display-single')
183185
if isinstance(value, str):
184186
print(value, file=file)
187+
elif isinstance(value, bytes):
188+
file.write(value.decode())
185189
else:
186190
display_tree_like_format(value, file)

primehub/utils/http_client.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def request(self, variables: dict, query: str, error_handler: Callable = None):
3737
except BaseException as e:
3838
raise RequestException(e)
3939

40-
def request_logs(self, endpoint, follow, tail) -> Iterator[str]:
40+
def request_logs(self, endpoint, follow, tail) -> Iterator[bytes]:
4141
params = {'follow': 'false'}
4242
if follow:
4343
params['follow'] = 'true'
@@ -46,11 +46,8 @@ def request_logs(self, endpoint, follow, tail) -> Iterator[str]:
4646
headers = {'authorization': 'Bearer {}'.format(self.primehub_config.api_token)}
4747

4848
with requests.get(endpoint, headers=headers, params=params, stream=follow) as response:
49-
if follow:
50-
for line in response.iter_lines():
51-
yield line.decode()
52-
else:
53-
yield response.text
49+
for chunk in response.iter_content(chunk_size=8192):
50+
yield chunk
5451

5552
def request_file(self, endpoint, dest):
5653
headers = {'authorization': 'Bearer {}'.format(self.primehub_config.api_token)}

tests/test_http_logs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ def test_request_logs(self):
2727
count = 0
2828
for x in g:
2929
count = count + 1
30-
self.assertEqual(1, count, 'get one result when no following')
30+
self.assertTrue(count > 1, 'Generator gives lots of lines')

0 commit comments

Comments
 (0)