Skip to content

Commit bc9648d

Browse files
committed
Refactor a little bit more
1 parent ee46cf0 commit bc9648d

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

simulate.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,43 @@
1313
from dataclasses import dataclass
1414
from playwright.async_api import async_playwright, Browser
1515

16+
17+
@dataclass
18+
class HubAccess:
19+
"""
20+
Information needed to talk to a hub
21+
"""
22+
url: URL
23+
token: str
24+
1625
@dataclass
1726
class Server:
27+
"""
28+
Represents a user server
29+
"""
1830
servername: str
1931
username: str
20-
hub_url: URL
32+
hub_access: HubAccess
2133

2234
@dataclass
2335
class RunningServer(Server):
36+
"""
37+
Represents a running user server
38+
"""
2439
start_request_time: datetime
2540
start_completion_time: datetime
2641
server_url: URL
2742
startup_events: List[dict]
2843

2944
@dataclass
3045
class FailedServer(Server):
46+
"""
47+
Represents a user server that failed to start
48+
"""
3149
start_request_time: datetime
3250
start_failure_time: datetime
3351
startup_events: List[dict]
3452

35-
@dataclass
36-
class ServerStartResult:
37-
servername: str
38-
username: str
39-
start_time: datetime
40-
completion_time: datetime
41-
started_successfully: bool
42-
events: List[dict]
43-
server_url: URL
44-
45-
@property
46-
def startup_duration(self):
47-
return self.completion_time - self.start_time
48-
49-
@override
50-
def __str__(self) -> str:
51-
return f"user:{self.username} server:{self.servername} started:{self.started_successfully} startup_duration:{self.startup_duration}"
52-
5353

5454
async def load_nbgitpuller_url(browser: Browser, server: RunningServer, token: str, nbgitpuller_url: URL, screenshot_name: str):
5555
print(f"visiting {server.server_url}")
@@ -78,14 +78,17 @@ async def start_named_server(session: aiohttp.ClientSession, server: Server) ->
7878
Try to start a named server as defined
7979
8080
"""
81-
server_api_url = server.hub_url / "hub/api/users" / server.username / "servers" / server.servername
81+
headers = {
82+
"Authorization": f"token {server.hub_access.token}"
83+
}
84+
server_api_url = server.hub_access.url / "hub/api/users" / server.username / "servers" / server.servername
8285
events = []
83-
async with session.post(server_api_url) as resp:
86+
async with session.post(server_api_url, headers=headers) as resp:
8487
start_time = datetime.now()
8588
if resp.status == 202:
8689
# we are awaiting start, let's look for events
8790
print(f"server {server.servername} waiting to start")
88-
async with session.get(server_api_url / "progress") as progress_resp:
91+
async with session.get(server_api_url / "progress", headers=headers) as progress_resp:
8992
async for line in progress_resp.content:
9093
if line.decode().strip() == '':
9194
# Empty line, just continue
@@ -97,11 +100,11 @@ async def start_named_server(session: aiohttp.ClientSession, server: Server) ->
97100
return RunningServer(
98101
servername=server.servername,
99102
username=server.username,
100-
hub_url=server.hub_url,
103+
hub_access=server.hub_access,
101104
start_request_time=start_time,
102105
start_completion_time=datetime.now(),
103106
startup_events=events,
104-
server_url=URL(server.hub_url / progress_event['url'][1:]) # Trim leading slashG
107+
server_url=URL(server.hub_access.url / progress_event['url'][1:]) # Trim leading slashG
105108
)
106109
elif resp.status == 201:
107110
# Means the server is immediately ready, and i don't want to deal with that yet
@@ -136,10 +139,8 @@ async def main():
136139
hub_url = URL(args.hub_url)
137140
async with async_playwright() as p:
138141
browser = await p.firefox.launch(headless=False)
139-
async with aiohttp.ClientSession(headers={
140-
"Authorization": f"token {token}"
141-
}) as session:
142-
servers_to_start = [Server(f"perf-{i}", args.username, hub_url) for i in range(args.servers_count)]
142+
async with aiohttp.ClientSession() as session:
143+
servers_to_start = [Server(f"perf-{i}", args.username, HubAccess(hub_url, token)) for i in range(args.servers_count)]
143144
await aiometer.run_all(
144145
[partial(payload, session, browser, token, nbgitpuller_url, server) for server in servers_to_start],
145146
max_at_once=args.max_concurrency

0 commit comments

Comments
 (0)