JatinkInnovision commited on
Commit
8048413
Β·
verified Β·
1 Parent(s): 7bce5c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -15
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
- # ... (Keep existing setup code) ...
 
 
 
 
40
  try:
41
- # ... (Keep connection and server find logic) ...
 
 
 
 
 
 
42
 
43
  if action == "activate":
44
- # Milestone 1...
45
- # [Previous logic for unshelving]
 
46
 
47
- # Milestone 2: Unshelved with extended 10-minute (600s) timeout
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
- # [Previous polling logic]
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  elif action == "shelve":
58
- # [Confirmation check]
59
- yield ("πŸ’€ Shelving Begin...", gr.update(visible=False))
 
 
 
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: Enable button only if checkbox is ticked
86
  confirm_check.change(fn=lambda x: gr.update(interactive=x), inputs=confirm_check, outputs=shelve_btn)
87
 
88
- # ACTION CLICKS: Using gr.State to avoid Lambda Generator issues
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())