Skip to content

Commit 4052ae4

Browse files
committed
Refactoring the poller a bit
1 parent 1ad17ec commit 4052ae4

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

bbblb/services/poller.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
from dataclasses import dataclass
23
import datetime
34
import random
45
import time
@@ -17,6 +18,16 @@
1718
LOG = logging.getLogger(__name__)
1819

1920

21+
@dataclass
22+
class ServerStats:
23+
meetings = 0
24+
users = 0
25+
video = 0
26+
voice = 0
27+
largest = 0
28+
load = 0.0
29+
30+
2031
class MeetingPoller(BackgroundService):
2132
def __init__(self, config: BBBLBConfig):
2233
self.config = config
@@ -109,8 +120,7 @@ async def poll_one(self, server_id):
109120

110121
LOG.info(f"Polling {server.api_base} (state={server.health.name})")
111122
running_ids = set()
112-
users = 0
113-
load = 0.0
123+
stats = ServerStats()
114124
success = True
115125
try:
116126
async with self.bbb.connect(server.api_base, server.secret) as client:
@@ -126,17 +136,25 @@ async def poll_one(self, server_id):
126136
parent_id = mxml.findtext("breakout/parentMeetingID")
127137
running_ids.add(meeting_id)
128138

129-
load += self.load_base
130-
users += int(mxml.findtext("participantCount") or 0)
131-
load += int(mxml.findtext("participantCount") or 0) * self.load_user
132-
load += (
133-
int(mxml.findtext("voiceParticipantCount") or 0) * self.load_voice
134-
)
135-
load += int(mxml.findtext("videoCount") or 0) * self.load_video
136-
139+
users = int(mxml.findtext("participantCount") or 0)
140+
voice = int(mxml.findtext("voiceParticipantCount") or 0)
141+
video = int(mxml.findtext("videoCount") or 0)
137142
age = max(0.0, time.time() - int(mxml.findtext("createTime") or 0))
143+
144+
stats.meetings += 1
145+
stats.users += users
146+
stats.voice += voice
147+
stats.video += video
148+
stats.largest = max(stats.largest, users)
149+
150+
stats.load += self.load_base
151+
stats.load += users * self.load_user
152+
stats.load += voice * self.load_voice
153+
stats.load += video * self.load_video
138154
if age < self.load_cooldown:
139-
load += self.load_prediction * (1.0 - (age / self.load_cooldown))
155+
stats.load += self.load_prediction * (
156+
1.0 - (age / self.load_cooldown)
157+
)
140158

141159
if meeting_id not in meetings:
142160
if parent_id:
@@ -175,13 +193,13 @@ async def poll_one(self, server_id):
175193
old_health = server.health
176194

177195
if success:
178-
server.load = load
196+
server.load = stats.load
179197
server.mark_success(self.minsuccess)
180198
else:
181199
server.mark_error(self.maxerror)
182200

183201
LOG.info(
184-
f"[{server.domain}] meetings={len(running_ids)} users={users} load={load:.1f} health={server.health.name}"
202+
f"[{server.domain}] meetings={stats.meetings} users={stats.users} load={stats.load:.1f} health={server.health.name}"
185203
)
186204

187205
# Log all state changes (including recovery) as warnings

0 commit comments

Comments
 (0)