()
| 422 | |
| 423 | |
| 424 | def fetch_channel_stats(): |
| 425 | redis_client = RedisClient.get_client() |
| 426 | |
| 427 | try: |
| 428 | # Basic info for all channels |
| 429 | channel_pattern = "live:channel:*:metadata" |
| 430 | all_channels = [] |
| 431 | |
| 432 | # Extract channel IDs from keys |
| 433 | cursor = 0 |
| 434 | while True: |
| 435 | cursor, keys = redis_client.scan(cursor, match=channel_pattern) |
| 436 | for key in keys: |
| 437 | channel_id_match = re.search(r"live:channel:(.*):metadata", key) |
| 438 | if channel_id_match: |
| 439 | ch_id = channel_id_match.group(1) |
| 440 | channel_info = ChannelStatus.get_basic_channel_info(ch_id) |
| 441 | if channel_info: |
| 442 | all_channels.append(channel_info) |
| 443 | |
| 444 | if cursor == 0: |
| 445 | break |
| 446 | |
| 447 | send_websocket_update( |
| 448 | "updates", |
| 449 | "update", |
| 450 | { |
| 451 | "success": True, |
| 452 | "type": "channel_stats", |
| 453 | "stats": json.dumps({'channels': all_channels, 'count': len(all_channels)}) |
| 454 | }, |
| 455 | collect_garbage=True |
| 456 | ) |
| 457 | |
| 458 | # Explicitly clean up large data structures |
| 459 | all_channels = None |
| 460 | |
| 461 | except Exception as e: |
| 462 | logger.error(f"Error in channel_status: {e}", exc_info=True) |
| 463 | return |
| 464 | |
| 465 | @shared_task |
| 466 | def rehash_streams(keys): |
no test coverage detected