|
1 | 1 | import asyncio |
2 | | -import json |
3 | 2 | import os |
4 | 3 | import re |
5 | 4 | import traceback |
|
9 | 8 | from io import BytesIO |
10 | 9 | from tempfile import TemporaryDirectory, NamedTemporaryFile |
11 | 10 |
|
12 | | -import urllib |
| 11 | +# import json |
| 12 | +# import urllib |
13 | 13 |
|
14 | 14 | import oyaml as yaml |
15 | 15 | import requests |
16 | 16 | from celery._state import app_or_default |
17 | 17 | from django.conf import settings |
18 | | -from django_redis import get_redis_connection |
| 18 | +# from django_redis import get_redis_connection |
19 | 19 | from django.core.exceptions import ObjectDoesNotExist |
20 | 20 | from django.core.files.base import ContentFile |
21 | 21 | from django.db.models import Subquery, OuterRef, Count, Case, When, Value, F |
|
24 | 24 | from django.utils.timezone import now |
25 | 25 | from rest_framework.exceptions import ValidationError |
26 | 26 |
|
27 | | -from celery_config import app, app_for_vhost |
| 27 | +from celery_config import app # , app_for_vhost |
28 | 28 | from competitions.models import Submission, CompetitionCreationTaskStatus, SubmissionDetails, Competition, \ |
29 | 29 | CompetitionDump, Phase |
30 | | -from queues.models import Queue |
| 30 | +# from queues.models import Queue |
31 | 31 | from competitions.unpackers.utils import CompetitionUnpackingException |
32 | 32 | from competitions.unpackers.v1 import V15Unpacker |
33 | 33 | from competitions.unpackers.v2 import V2Unpacker |
|
41 | 41 |
|
42 | 42 | import logging |
43 | 43 |
|
44 | | -from utils.worker_utils import WORKER_HEARTBEAT_TTL, WORKERS_REGISTRY_KEY, extract_queue_names, is_compute_worker, known_compute_queue_names |
| 44 | +# from utils.worker_utils import WORKER_HEARTBEAT_TTL, WORKERS_REGISTRY_KEY, extract_queue_names, is_compute_worker, known_compute_queue_names |
45 | 45 | logger = logging.getLogger(__name__) |
46 | 46 |
|
47 | 47 | COMPETITION_FIELDS = [ |
@@ -819,95 +819,3 @@ def _broadcast_worker_state(payload): |
819 | 819 | "worker": payload, |
820 | 820 | }, |
821 | 821 | ) |
822 | | - |
823 | | - |
824 | | -@app.task(queue="site-worker", soft_time_limit=120) |
825 | | -def refresh_compute_worker_health(): |
826 | | - celery_app = app |
827 | | - r = get_redis_connection("default") |
828 | | - known_queue_names = known_compute_queue_names() |
829 | | - broker_sources = [] |
830 | | - broker_sources.append(("default", celery_app.conf.broker_url, celery_app)) |
831 | | - |
832 | | - private_queues = ( |
833 | | - Queue.objects.filter(competitions__isnull=False) |
834 | | - .exclude(name__isnull=True) |
835 | | - .exclude(name="") |
836 | | - .distinct() |
837 | | - ) |
838 | | - for queue in private_queues: |
839 | | - if not queue.broker_url: |
840 | | - continue |
841 | | - parsed = urllib.parse.urlparse(queue.broker_url) |
842 | | - vhost = parsed.path |
843 | | - broker_url = urllib.parse.urljoin(celery_app.conf.broker_url, vhost) |
844 | | - broker_sources.append((queue.name, broker_url, app_for_vhost(vhost))) |
845 | | - |
846 | | - inspected_brokers = set() |
847 | | - for source_name, broker_url, broker_app in broker_sources: |
848 | | - if broker_url in inspected_brokers: |
849 | | - continue |
850 | | - inspected_brokers.add(broker_url) |
851 | | - |
852 | | - try: |
853 | | - # timeout=5 : 4 appels × 5s × N brokers |
854 | | - inspector = broker_app.control.inspect(timeout=5) |
855 | | - if inspector is None: |
856 | | - logger.warning( |
857 | | - "Celery inspect returned None for broker=%s", source_name |
858 | | - ) |
859 | | - continue |
860 | | - stats = inspector.stats() or {} |
861 | | - active = inspector.active() or {} |
862 | | - reserved = inspector.reserved() or {} |
863 | | - active_queues = inspector.active_queues() or {} |
864 | | - except Exception: |
865 | | - logger.exception( |
866 | | - "Unable to inspect Celery workers for broker %s", source_name |
867 | | - ) |
868 | | - continue |
869 | | - |
870 | | - for worker_name in stats.keys(): |
871 | | - queues = active_queues.get(worker_name, []) or [] |
872 | | - queue_names = extract_queue_names(queues) |
873 | | - if not is_compute_worker(worker_name, queue_names, known_queue_names): |
874 | | - continue |
875 | | - |
876 | | - running_jobs = len(active.get(worker_name, [])) + len( |
877 | | - reserved.get(worker_name, []) |
878 | | - ) |
879 | | - status = "busy" if running_jobs > 0 else "available" |
880 | | - payload = { |
881 | | - "hostname": worker_name, |
882 | | - "status": status, |
883 | | - "running_jobs": running_jobs, |
884 | | - "timestamp": now().timestamp(), |
885 | | - "queue_source": source_name, |
886 | | - "queue_names": sorted(queue_names), |
887 | | - } |
888 | | - heartbeat_key = f"worker:{source_name}:{worker_name}:heartbeat" |
889 | | - r.set(heartbeat_key, json.dumps(payload), ex=WORKER_HEARTBEAT_TTL) |
890 | | - r.hset( |
891 | | - WORKERS_REGISTRY_KEY, |
892 | | - f"{source_name}:{worker_name}", |
893 | | - json.dumps( |
894 | | - { |
895 | | - "hostname": worker_name, |
896 | | - "status": status, |
897 | | - "running_jobs": running_jobs, |
898 | | - "last_seen": payload["timestamp"], |
899 | | - "queue_source": source_name, |
900 | | - "queue_names": sorted(queue_names), |
901 | | - } |
902 | | - ), |
903 | | - ) |
904 | | - _broadcast_worker_state(payload) |
905 | | - # Logs about CW health HERE |
906 | | - # logger.info( |
907 | | - # "[WORKER-HEALTH] source=%s worker=%s status=%s jobs=%d queues=%s", |
908 | | - # source_name, |
909 | | - # worker_name, |
910 | | - # status, |
911 | | - # running_jobs, |
912 | | - # sorted(queue_names), |
913 | | - # ) |
0 commit comments