da03 commited on
Commit
941cf55
·
1 Parent(s): 8b76adf
Files changed (1) hide show
  1. dispatcher.py +34 -4
dispatcher.py CHANGED
@@ -182,6 +182,8 @@ class UserSession:
182
  ip_address: Optional[str] = None
183
  interaction_count: int = 0
184
  queue_start_time: Optional[float] = None
 
 
185
 
186
  @dataclass
187
  class WorkerInfo:
@@ -332,23 +334,27 @@ class SessionManager:
332
  elapsed = current_time - session.last_activity if session.last_activity else 0
333
  remaining = session.max_session_time - elapsed
334
 
335
- # Send warning at 15 seconds before timeout
336
- if remaining <= 15 and remaining > 10:
337
  await session.websocket.send_json({
338
  "type": "session_warning",
339
  "time_remaining": remaining,
340
  "queue_size": len(self.session_queue)
341
  })
 
 
342
 
343
  # Grace period handling
344
  elif remaining <= 10 and remaining > 0:
345
  # Check if queue is empty - if so, extend session
346
  if len(self.session_queue) == 0:
347
  session.max_session_time = None # Remove time limit
 
348
  await session.websocket.send_json({
349
  "type": "time_limit_removed",
350
  "reason": "queue_empty"
351
  })
 
352
  else:
353
  await session.websocket.send_json({
354
  "type": "grace_period",
@@ -367,11 +373,13 @@ class SessionManager:
367
  if idle_time >= self.IDLE_TIMEOUT:
368
  await self.end_session(session_id, SessionStatus.TIMEOUT)
369
  return
370
- elif idle_time >= self.QUEUE_WARNING_TIME:
371
  await session.websocket.send_json({
372
  "type": "idle_warning",
373
  "time_remaining": self.IDLE_TIMEOUT - idle_time
374
  })
 
 
375
 
376
  await asyncio.sleep(1) # Check every second
377
 
@@ -510,11 +518,33 @@ class SessionManager:
510
  return full_cycles_time + current_cycle_time
511
 
512
  async def handle_user_activity(self, session_id: str):
513
- """Update user activity timestamp"""
514
  session = self.sessions.get(session_id)
515
  if session:
 
516
  session.last_activity = time.time()
517
  session.interaction_count += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
  if not session.user_has_interacted:
519
  session.user_has_interacted = True
520
  logger.info(f"User started interacting in session {session_id}")
 
182
  ip_address: Optional[str] = None
183
  interaction_count: int = 0
184
  queue_start_time: Optional[float] = None
185
+ idle_warning_sent: bool = False
186
+ session_warning_sent: bool = False
187
 
188
  @dataclass
189
  class WorkerInfo:
 
334
  elapsed = current_time - session.last_activity if session.last_activity else 0
335
  remaining = session.max_session_time - elapsed
336
 
337
+ # Send warning at 15 seconds before timeout (only once)
338
+ if remaining <= 15 and remaining > 10 and not session.session_warning_sent:
339
  await session.websocket.send_json({
340
  "type": "session_warning",
341
  "time_remaining": remaining,
342
  "queue_size": len(self.session_queue)
343
  })
344
+ session.session_warning_sent = True
345
+ logger.info(f"Session warning sent to {session_id}, time remaining: {remaining:.1f}s")
346
 
347
  # Grace period handling
348
  elif remaining <= 10 and remaining > 0:
349
  # Check if queue is empty - if so, extend session
350
  if len(self.session_queue) == 0:
351
  session.max_session_time = None # Remove time limit
352
+ session.session_warning_sent = False # Reset warning since limit removed
353
  await session.websocket.send_json({
354
  "type": "time_limit_removed",
355
  "reason": "queue_empty"
356
  })
357
+ logger.info(f"Session time limit removed for {session_id} (queue empty)")
358
  else:
359
  await session.websocket.send_json({
360
  "type": "grace_period",
 
373
  if idle_time >= self.IDLE_TIMEOUT:
374
  await self.end_session(session_id, SessionStatus.TIMEOUT)
375
  return
376
+ elif idle_time >= self.QUEUE_WARNING_TIME and not session.idle_warning_sent:
377
  await session.websocket.send_json({
378
  "type": "idle_warning",
379
  "time_remaining": self.IDLE_TIMEOUT - idle_time
380
  })
381
+ session.idle_warning_sent = True
382
+ logger.info(f"Idle warning sent to {session_id}, time remaining: {self.IDLE_TIMEOUT - idle_time:.1f}s")
383
 
384
  await asyncio.sleep(1) # Check every second
385
 
 
518
  return full_cycles_time + current_cycle_time
519
 
520
  async def handle_user_activity(self, session_id: str):
521
+ """Update user activity timestamp and reset warning flags"""
522
  session = self.sessions.get(session_id)
523
  if session:
524
+ old_time = session.last_activity
525
  session.last_activity = time.time()
526
  session.interaction_count += 1
527
+
528
+ # Reset warning flags if user activity detected after warnings
529
+ warning_reset = False
530
+ if session.idle_warning_sent:
531
+ logger.info(f"User activity detected, resetting idle warning for session {session_id}")
532
+ session.idle_warning_sent = False
533
+ warning_reset = True
534
+
535
+ if session.session_warning_sent:
536
+ logger.info(f"User activity detected, resetting session warning for session {session_id}")
537
+ session.session_warning_sent = False
538
+ warning_reset = True
539
+
540
+ # Notify client that activity was detected and warnings are reset
541
+ if warning_reset:
542
+ try:
543
+ await session.websocket.send_json({"type": "activity_reset"})
544
+ logger.info(f"Activity reset message sent to session {session_id}")
545
+ except Exception as e:
546
+ logger.error(f"Failed to send activity reset to session {session_id}: {e}")
547
+
548
  if not session.user_has_interacted:
549
  session.user_has_interacted = True
550
  logger.info(f"User started interacting in session {session_id}")