Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 8901a91

Browse files
committed
Feature PS-9868:
Prepare logic for retrieving job logs
1 parent 3fce0c3 commit 8901a91

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

paperspace/cli/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from paperspace import constants, client, config
77
from paperspace.cli.common import api_key_option, del_if_value_is_none
88
from paperspace.cli.jobs import jobs_group
9+
from paperspace.cli.logs import logs_group
910
from paperspace.cli.projects import projects_group
1011
from paperspace.cli.types import ChoiceType, json_string
1112
from paperspace.cli.validators import validate_mutually_exclusive, validate_email
@@ -1054,3 +1055,4 @@ def version():
10541055

10551056
cli.add_command(jobs_group)
10561057
cli.add_command(projects_group)
1058+
cli.add_command(logs_group)

paperspace/cli/logs.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import click
2+
3+
from paperspace import client, config
4+
from paperspace.cli import common
5+
from paperspace.commands import logs as logs_commands
6+
7+
8+
@click.group("logs", help="Manage gradient logs")
9+
def logs_group():
10+
pass
11+
12+
13+
@logs_group.command("list", help="List job logs")
14+
@click.option(
15+
"--jobId",
16+
"job_id",
17+
required=True
18+
)
19+
@common.api_key_option
20+
def list_logs(job_id, api_key=None):
21+
logs_api = client.API(config.CONFIG_LOG_HOST, api_key=api_key)
22+
command = logs_commands.ListLogsCommand(api=logs_api)
23+
command.execute(job_id)

paperspace/cli/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ def convert(self, value, param, ctx):
1717

1818
def json_string(val):
1919
"""Wraps json.loads so the cli help shows proper option's type name instead of 'LOADS'"""
20-
return json.loads(val)
20+
return json.loads(val)

paperspace/commands/logs.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from paperspace.commands import CommandBase
2+
3+
4+
class LogsCommandBase(CommandBase):
5+
def _log_message(self, response, success_msg_template, error_msg):
6+
if response.ok:
7+
try:
8+
handle = response.json()
9+
except (ValueError, KeyError):
10+
self.logger.log(success_msg_template)
11+
else:
12+
msg = success_msg_template.format(**handle)
13+
self.logger.log(msg)
14+
else:
15+
try:
16+
data = response.json()
17+
self.logger.log_error_response(data)
18+
except ValueError:
19+
self.logger.log(error_msg)
20+
21+
22+
class ListLogsCommand(LogsCommandBase):
23+
def execute(self, job_id):
24+
url = f"/jobs/logs?jobId={job_id}"
25+
response = self.api.get(url)
26+
self._log_message(
27+
response,
28+
"Job logs retrieved",
29+
"Unknown error while retrieving job logs"
30+
)

0 commit comments

Comments
 (0)