Skip to content

Commit 9e34a46

Browse files
committed
Streaming logs in deploy
1 parent c9d8ff5 commit 9e34a46

3 files changed

Lines changed: 56 additions & 21 deletions

File tree

nbs/00_core.ipynb

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"from pathlib import Path\n",
3535
"from uuid import uuid4\n",
3636
"from time import time, sleep\n",
37+
"import threading\n",
3738
"\n",
3839
"import io, os, re, tarfile, tomllib"
3940
]
@@ -326,6 +327,36 @@
326327
" return tarz, len(files)"
327328
]
328329
},
330+
{
331+
"cell_type": "code",
332+
"execution_count": null,
333+
"id": "19fd70cb",
334+
"metadata": {},
335+
"outputs": [],
336+
"source": [
337+
"#| export\n",
338+
"log_modes = str_enum('log_modes', 'build', 'app')"
339+
]
340+
},
341+
{
342+
"cell_type": "code",
343+
"execution_count": null,
344+
"id": "c1c1db20",
345+
"metadata": {},
346+
"outputs": [],
347+
"source": [
348+
"#| export\n",
349+
"def _logs_during_deploy(\n",
350+
" path:Path, # Path to project\n",
351+
" mode:str='build', # 'build' or 'run'\n",
352+
" tail:bool=True): # We always want to tail the logs\n",
353+
" # Give the server time to start\n",
354+
" sleep(1) \n",
355+
" # Unwrap the logs function to avoid the call_parse decorator so it can be wrapped in a thread\n",
356+
" log_thread = threading.Thread(target=logs.__wrapped__, kwargs=dict(path=path, mode='build', tail=True))\n",
357+
" log_thread.start()"
358+
]
359+
},
329360
{
330361
"cell_type": "code",
331362
"execution_count": null,
@@ -355,7 +386,8 @@
355386
" if resp.status_code == 200:\n",
356387
" print('✅ Upload complete! Your app is currently being built.')\n",
357388
" print(f'It will be live at {endpoint(sub=aid)}')\n",
358-
" else: print(f'Failure: {resp.status_code}\\n{resp.text}')"
389+
" else: print(f'Failure: {resp.status_code}\\n{resp.text}')\n",
390+
" _logs_during_deploy(path=path, mode='build', tail=True)"
359391
]
360392
},
361393
{
@@ -461,17 +493,6 @@
461493
"## App - logs"
462494
]
463495
},
464-
{
465-
"cell_type": "code",
466-
"execution_count": null,
467-
"id": "24e62f1f",
468-
"metadata": {},
469-
"outputs": [],
470-
"source": [
471-
"#| export\n",
472-
"log_modes = str_enum('log_modes', 'build', 'app')"
473-
]
474-
},
475496
{
476497
"cell_type": "code",
477498
"execution_count": null,

plash_cli/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'lib_path': 'plash_cli'},
88
'syms': { 'plash_cli.core': { 'plash_cli.core.PlashError': ('core.html#plasherror', 'plash_cli/core.py'),
99
'plash_cli.core._deps': ('core.html#_deps', 'plash_cli/core.py'),
10+
'plash_cli.core._logs_during_deploy': ('core.html#_logs_during_deploy', 'plash_cli/core.py'),
1011
'plash_cli.core.create_tar_archive': ('core.html#create_tar_archive', 'plash_cli/core.py'),
1112
'plash_cli.core.delete': ('core.html#delete', 'plash_cli/core.py'),
1213
'plash_cli.core.deploy': ('core.html#deploy', 'plash_cli/core.py'),

plash_cli/core.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_core.ipynb.
44

55
# %% auto 0
6-
__all__ = ['PLASH_CONFIG_HOME', 'PLASH_DOMAIN', 'pat', 'stop', 'start', 'log_modes', 'get_client', 'mk_auth_req', 'get_app_id',
6+
__all__ = ['PLASH_CONFIG_HOME', 'PLASH_DOMAIN', 'pat', 'log_modes', 'stop', 'start', 'get_client', 'mk_auth_req', 'get_app_id',
77
'endpoint', 'is_included', 'poll_cookies', 'login', 'PlashError', 'validate_app', 'create_tar_archive',
88
'deploy', 'view', 'delete', 'endpoint_func', 'logs', 'download']
99

@@ -14,6 +14,7 @@
1414
from pathlib import Path
1515
from uuid import uuid4
1616
from time import time, sleep
17+
import threading
1718

1819
import io, os, re, tarfile, tomllib
1920

@@ -121,6 +122,20 @@ def create_tar_archive(path:Path) -> tuple[io.BytesIO, int]:
121122
return tarz, len(files)
122123

123124
# %% ../nbs/00_core.ipynb 24
125+
log_modes = str_enum('log_modes', 'build', 'app')
126+
127+
# %% ../nbs/00_core.ipynb 25
128+
def _logs_during_deploy(
129+
path:Path, # Path to project
130+
mode:str='build', # 'build' or 'run'
131+
tail:bool=True): # We always want to tail the logs
132+
# Give the server time to start
133+
sleep(1)
134+
# Unwrap the logs function to avoid the call_parse decorator so it can be wrapped in a thread
135+
log_thread = threading.Thread(target=logs.__wrapped__, kwargs=dict(path=path, mode='build', tail=True))
136+
log_thread.start()
137+
138+
# %% ../nbs/00_core.ipynb 26
124139
@call_parse
125140
def deploy(
126141
path:Path=Path('.'), # Path to project
@@ -143,8 +158,9 @@ def deploy(
143158
print('✅ Upload complete! Your app is currently being built.')
144159
print(f'It will be live at {endpoint(sub=aid)}')
145160
else: print(f'Failure: {resp.status_code}\n{resp.text}')
161+
_logs_during_deploy(path=path, mode='build', tail=True)
146162

147-
# %% ../nbs/00_core.ipynb 26
163+
# %% ../nbs/00_core.ipynb 28
148164
@call_parse
149165
def view(
150166
path:Path=Path('.'), # Path to project
@@ -153,7 +169,7 @@ def view(
153169
print(f"Opening browser to view app :\n{url}\n")
154170
webbrowser.open(url)
155171

156-
# %% ../nbs/00_core.ipynb 28
172+
# %% ../nbs/00_core.ipynb 30
157173
@call_parse
158174
def delete(
159175
path:Path=Path('.'), # Path to project
@@ -170,7 +186,7 @@ def delete(
170186
r = mk_auth_req(endpoint(rt=f"/delete?aid={aid}"), "delete")
171187
return r.text
172188

173-
# %% ../nbs/00_core.ipynb 30
189+
# %% ../nbs/00_core.ipynb 32
174190
def endpoint_func(endpoint_name):
175191
'Creates a function for a specific API endpoint'
176192
@call_parse
@@ -191,10 +207,7 @@ def func(
191207
stop = endpoint_func('/stop')
192208
start = endpoint_func('/start')
193209

194-
# %% ../nbs/00_core.ipynb 32
195-
log_modes = str_enum('log_modes', 'build', 'app')
196-
197-
# %% ../nbs/00_core.ipynb 33
210+
# %% ../nbs/00_core.ipynb 34
198211
@call_parse
199212
def logs(
200213
path:Path=Path('.'), # Path to project
@@ -218,7 +231,7 @@ def logs(
218231
r = mk_auth_req(endpoint(rt=f"/logs?aid={aid}&mode={mode}"))
219232
return r.text
220233

221-
# %% ../nbs/00_core.ipynb 35
234+
# %% ../nbs/00_core.ipynb 36
222235
@call_parse
223236
def download(
224237
path:Path=Path('.'), # Path to project

0 commit comments

Comments
 (0)