Skip to content

Commit 861f62e

Browse files
committed
Add in_cluster notebook detection
1 parent dc239a4 commit 861f62e

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

polyaxon_client/tracking/base.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
from __future__ import absolute_import, division, print_function
33

44
import atexit
5+
import json
6+
import os
57
import sys
68
import time
79

810
from polystores.stores.manager import StoreManager
911

1012
from polyaxon_client import PolyaxonClient, settings
1113
from polyaxon_client.exceptions import PolyaxonClientException
14+
from polyaxon_client.tracking.in_cluster import ensure_in_custer
1215
from polyaxon_client.tracking.paths import get_outputs_path
1316
from polyaxon_client.tracking.utils.project import get_project_info
1417

@@ -28,6 +31,9 @@ def __init__(self,
2831

2932
if not settings.IN_CLUSTER and project is None:
3033
raise PolyaxonClientException('Please provide a valid project.')
34+
elif self.is_notebook_job:
35+
job_info = self.get_notebook_job_info()
36+
project = job_info['project_name']
3137

3238
self.last_status = None
3339
self.client = client or PolyaxonClient()
@@ -52,6 +58,29 @@ def __init__(self,
5258
if outputs_store is None and settings.IN_CLUSTER and self.REQUIRES_OUTPUTS:
5359
self.set_outputs_store(outputs_path=get_outputs_path(), set_env_vars=True)
5460

61+
def get_notebook_job_info(self):
62+
if settings.NO_OP:
63+
return None
64+
65+
ensure_in_custer()
66+
67+
info = os.getenv('POLYAXON_NOTEBOOK_INFO', None)
68+
try:
69+
return json.loads(info) if info else None
70+
except (ValueError, TypeError):
71+
print('Could get experiment info, '
72+
'please make sure this is running inside a polyaxon job.')
73+
return None
74+
75+
@property
76+
def is_notebook_job(self):
77+
if settings.NO_OP:
78+
return None
79+
80+
ensure_in_custer()
81+
82+
return 'POLYAXON_NOTEBOOK_INFO' in os.environ
83+
5584
def get_data(self):
5685
raise NotImplementedError
5786

polyaxon_client/tracking/experiment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self,
3232
if settings.NO_OP:
3333
return
3434

35-
if project is None and settings.IN_CLUSTER:
35+
if project is None and settings.IN_CLUSTER and not self.is_notebook_job:
3636
experiment_info = self.get_experiment_info()
3737
project = experiment_info['project_name']
3838
experiment_id = experiment_info['experiment_name'].split('.')[-1]
@@ -67,7 +67,7 @@ def __init__(self,
6767
self._set_health_url()
6868

6969
# Track run env
70-
if settings.IN_CLUSTER and self.track_env:
70+
if settings.IN_CLUSTER and self.track_env and not self.is_notebook_job:
7171
self.log_run_env()
7272

7373
def get_data(self):

polyaxon_client/tracking/job.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self,
2323
if settings.NO_OP:
2424
return
2525

26-
if project is None and settings.IN_CLUSTER:
26+
if project is None and settings.IN_CLUSTER and not self.is_notebook_job:
2727
job_info = self.get_job_info()
2828
project = job_info['project_name']
2929
job_id = job_info['job_name'].split('.')[-1]

0 commit comments

Comments
 (0)