Skip to content

Commit

Permalink
Wait for service ready instead of pod.
Browse files Browse the repository at this point in the history
  • Loading branch information
julialawrence committed Oct 24, 2023
1 parent ee68287 commit 1170e51
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions app/cluster_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uuid
import requests
from kubernetes import client, config, watch
from kubernetes.config import ConfigException
import time
Expand Down Expand Up @@ -115,6 +115,25 @@ def wait_for_pod_ready(namespace, pod_name):

return False # Default case, though your logic might differ based on how you want to handle timeouts

def wait_for_service_ready(service_url, timeout=300):
"""
Wait for the service to become ready by sending requests to the service URL.
:param service_url: The URL of the service to check.
:param timeout: The maximum time to wait, in seconds.
:return: True if the service becomes ready, False if the timeout is reached.
"""
start_time = time.time()
while time.time() - start_time < timeout:
try:
response = requests.get(service_url, timeout=5)
if response.status_code == 200:
# Service is ready
return True
except requests.RequestException as e:
print(f"Request failed: {e}")
# Wait for a while before retrying
time.sleep(5)
return False # Timeout reached

def launch_vscode_for_user(user_id):
# Step 1: Create a service account for the user
Expand All @@ -131,7 +150,8 @@ def launch_vscode_for_user(user_id):
pod_name = f"vscode-server-{sanitized_user_id}"
namespace = "dataaccessmanager"

if wait_for_pod_ready(namespace, pod_name):
# if wait_for_pod_ready(namespace, pod_name):
if wait_for_service_ready(service_url="http://vscode-service-dbe0354c6b5f4bdc8a356af8d4ec68ed.dataaccessmanager.svc.cluster.local/"):
print("VS Code server is ready for use.")
else:
print("There was a problem starting the VS Code server.")
Expand Down

0 comments on commit 1170e51

Please sign in to comment.