Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,30 +36,56 @@ def get_current_status():
|
|
| 36 |
return f"β Connection Error: {str(e)}"
|
| 37 |
|
| 38 |
def manage_instance(action, confirmed=False):
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if action == "activate":
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
# Milestone 2:
|
| 48 |
try:
|
| 49 |
conn.compute.wait_for_server(server, status='ACTIVE', wait=600)
|
| 50 |
yield ("β
Milestone 2/3: Unshelved! VM is now ACTIVE.", gr.update(visible=False))
|
| 51 |
except exceptions.ResourceTimeout:
|
| 52 |
yield ("β οΈ Milestone 2/3: Booting is taking a long time (10+ mins).", gr.update(visible=False))
|
|
|
|
| 53 |
|
| 54 |
-
# Milestone 3
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
elif action == "shelve":
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
| 60 |
conn.compute.shelve_server(server)
|
| 61 |
-
|
| 62 |
-
# Also extend the shelve timeout to 10 minutes to be safe
|
| 63 |
conn.compute.wait_for_server(server, status='SHELVED_OFFLOADED', wait=600)
|
| 64 |
yield ("π Instance is now SHELVED.", gr.update(visible=False))
|
| 65 |
|
|
@@ -82,10 +108,10 @@ with gr.Blocks() as demo:
|
|
| 82 |
status_label = gr.Label(value="Ready")
|
| 83 |
link_box = gr.Markdown(visible=False)
|
| 84 |
|
| 85 |
-
# UI Logic
|
| 86 |
confirm_check.change(fn=lambda x: gr.update(interactive=x), inputs=confirm_check, outputs=shelve_btn)
|
| 87 |
|
| 88 |
-
# ACTION CLICKS:
|
| 89 |
activate_btn.click(
|
| 90 |
fn=manage_instance,
|
| 91 |
inputs=[gr.State("activate")],
|
|
@@ -98,10 +124,10 @@ with gr.Blocks() as demo:
|
|
| 98 |
outputs=[status_label, link_box]
|
| 99 |
)
|
| 100 |
|
| 101 |
-
# Auto-refreshing status
|
| 102 |
timer = gr.Timer(30)
|
| 103 |
timer.tick(fn=get_current_status, outputs=live_status)
|
| 104 |
demo.load(fn=get_current_status, outputs=live_status)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
-
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 36 |
return f"β Connection Error: {str(e)}"
|
| 37 |
|
| 38 |
def manage_instance(action, confirmed=False):
|
| 39 |
+
"""Handles Activate and Shelve actions with milestone updates."""
|
| 40 |
+
clean_link = "chat.mch250095.projects.jetstream-cloud.org"
|
| 41 |
+
full_url = f"https://{clean_link}/"
|
| 42 |
+
instance_id = os.getenv("OS_INSTANCE_ID")
|
| 43 |
+
|
| 44 |
try:
|
| 45 |
+
# CRITICAL FIX: Define 'conn' and 'server' at the very beginning of the try block
|
| 46 |
+
conn = get_conn()
|
| 47 |
+
server = conn.compute.find_server(instance_id)
|
| 48 |
+
|
| 49 |
+
if not server:
|
| 50 |
+
yield ("β Error: Instance ID not found.", gr.update(visible=False))
|
| 51 |
+
return
|
| 52 |
|
| 53 |
if action == "activate":
|
| 54 |
+
yield ("β³ Milestone 1/3: Un-shelving Begin...", gr.update(visible=False))
|
| 55 |
+
if server.status == "SHELVED_OFFLOADED":
|
| 56 |
+
conn.compute.unshelve_server(server)
|
| 57 |
|
| 58 |
+
# Milestone 2: Extended 10-minute (600s) timeout
|
| 59 |
try:
|
| 60 |
conn.compute.wait_for_server(server, status='ACTIVE', wait=600)
|
| 61 |
yield ("β
Milestone 2/3: Unshelved! VM is now ACTIVE.", gr.update(visible=False))
|
| 62 |
except exceptions.ResourceTimeout:
|
| 63 |
yield ("β οΈ Milestone 2/3: Booting is taking a long time (10+ mins).", gr.update(visible=False))
|
| 64 |
+
return
|
| 65 |
|
| 66 |
+
# Milestone 3: Web service check
|
| 67 |
+
yield ("π Milestone 3/3: Waiting for software...", gr.update(visible=False))
|
| 68 |
+
for i in range(24):
|
| 69 |
+
try:
|
| 70 |
+
response = requests.get(full_url, timeout=3)
|
| 71 |
+
if response.status_code < 400:
|
| 72 |
+
yield ("π Success! Ready.", gr.update(value=f"### [Visit: {clean_link}]({full_url})", visible=True))
|
| 73 |
+
return
|
| 74 |
+
except:
|
| 75 |
+
pass
|
| 76 |
+
yield (f"π Milestone 3/3: Software booting (Attempt {i+1}/24)...", gr.update(visible=False))
|
| 77 |
+
time.sleep(5)
|
| 78 |
+
|
| 79 |
+
yield ("β οΈ Service slow.", gr.update(value=f"### [Manual Link: {clean_link}]({full_url})", visible=True))
|
| 80 |
|
| 81 |
elif action == "shelve":
|
| 82 |
+
if not confirmed:
|
| 83 |
+
yield ("π Shelve aborted: Confirmation required.", gr.update(visible=False))
|
| 84 |
+
return
|
| 85 |
+
|
| 86 |
+
yield ("π€ Shelving Begin... (Saving and Stopping)", gr.update(visible=False))
|
| 87 |
conn.compute.shelve_server(server)
|
| 88 |
+
# Extended timeout for shelving as well
|
|
|
|
| 89 |
conn.compute.wait_for_server(server, status='SHELVED_OFFLOADED', wait=600)
|
| 90 |
yield ("π Instance is now SHELVED.", gr.update(visible=False))
|
| 91 |
|
|
|
|
| 108 |
status_label = gr.Label(value="Ready")
|
| 109 |
link_box = gr.Markdown(visible=False)
|
| 110 |
|
| 111 |
+
# UI Logic
|
| 112 |
confirm_check.change(fn=lambda x: gr.update(interactive=x), inputs=confirm_check, outputs=shelve_btn)
|
| 113 |
|
| 114 |
+
# ACTION CLICKS: Direct function reference with gr.State
|
| 115 |
activate_btn.click(
|
| 116 |
fn=manage_instance,
|
| 117 |
inputs=[gr.State("activate")],
|
|
|
|
| 124 |
outputs=[status_label, link_box]
|
| 125 |
)
|
| 126 |
|
| 127 |
+
# Auto-refreshing status logic for Gradio 5/6
|
| 128 |
timer = gr.Timer(30)
|
| 129 |
timer.tick(fn=get_current_status, outputs=live_status)
|
| 130 |
demo.load(fn=get_current_status, outputs=live_status)
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|
| 133 |
+
demo.launch(theme=gr.themes.Soft())
|