Skip to content

Commit 7baa4da

Browse files
committed
feat: enhance execute_command example with asynchronous log streaming and environment status checks
1 parent 01f6ea4 commit 7baa4da

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

examples/execute_command.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
#!/usr/bin/env python
22

33
from gitpod import Gitpod
4-
from utils import get_context_url, get_authenticated_user, get_environment_class_id, get_environment_class, create_environment
5-
import time
6-
import requests
7-
8-
def check_environment_status(client, environment_id):
9-
environment = client.environments.retrieve(environment_id=environment_id).environment
10-
print(f"Environment status: {environment.status.phase}")
11-
if environment.status.phase in ["ENVIRONMENT_PHASE_STOPPING", "ENVIRONMENT_PHASE_STOPPED", "ENVIRONMENT_PHASE_DELETING", "ENVIRONMENT_PHASE_DELETED"]:
12-
raise RuntimeError(f"Environment {environment_id} is in an unexpected phase: {environment.status.phase}")
13-
elif environment.status.failure_message and len(environment.status.failure_message) > 0:
14-
raise RuntimeError(f"Environment {environment_id} failed: {'; '.join(environment.status.failure_message)}")
4+
from utils import get_context_url, get_authenticated_user, get_environment_class_id, get_environment_class, create_environment, check_environment_status, stream_logs
5+
import asyncio
156

167
def main() -> None:
178
client: Gitpod = Gitpod()
@@ -61,7 +52,7 @@ def main() -> None:
6152
)
6253

6354
log_url = None
64-
for event in event_stream:
55+
for event in event_stream._raw_iterator:
6556
print(f"Received event: {event}")
6657
if event.resource_type == "RESOURCE_TYPE_ENVIRONMENT" and event.resource_id == environment_id:
6758
check_environment_status(client, environment_id)
@@ -78,14 +69,11 @@ def main() -> None:
7869
print(f"Task logs are available at: {log_url}")
7970

8071
# Stream the log URL and print the logs
81-
response = requests.get(log_url, stream=True, headers={"Authorization": f"Bearer {logs_access_token}"})
82-
for line in response.iter_lines():
83-
if line:
84-
print(line.decode('utf-8'))
72+
asyncio.run(stream_logs(log_url, logs_access_token))
8573

8674
finally:
8775
client.environments.delete(environment_id=environment_id)
8876
print(f"Deleted environment: {environment_id}")
8977

9078
if __name__ == "__main__":
91-
main()
79+
main()

examples/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from gitpod.types.environment_retrieve_response import Environment
66
from gitpod.types.user_get_authenticated_user_response import User
77
from gitpod.types.environments.class_list_response import ClassListResponse
8+
import httpx
89

910
def get_context_url() -> str:
1011
return os.environ.get("CONTEXT_URL", "https://github.com/gitpod-io/empty")
@@ -75,3 +76,10 @@ def check_environment_status(client: Gitpod, environment_id: str) -> None:
7576
elif environment.status.failure_message and len(environment.status.failure_message) > 0:
7677
raise RuntimeError(f"Environment {environment_id} failed: {'; '.join(environment.status.failure_message)}")
7778

79+
async def stream_logs(log_url: str, logs_access_token: str) -> None:
80+
async with httpx.AsyncClient() as client:
81+
async with client.stream("GET", log_url, headers={"Authorization": f"Bearer {logs_access_token}"}) as response:
82+
async for line in response.aiter_lines():
83+
if line:
84+
print(line)
85+

0 commit comments

Comments
 (0)