From c8c8ed1775560f78beb6dab2e57031247cb0d341 Mon Sep 17 00:00:00 2001 From: Hao Liu Date: Wed, 29 Mar 2023 16:57:04 -0400 Subject: [PATCH] Raise ValueError when no ready and enabled task instance --- awx/main/dispatch/__init__.py | 38 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/awx/main/dispatch/__init__.py b/awx/main/dispatch/__init__.py index 009438307a..2029f540c2 100644 --- a/awx/main/dispatch/__init__.py +++ b/awx/main/dispatch/__init__.py @@ -1,3 +1,4 @@ +import os import psycopg2 import select @@ -5,8 +6,6 @@ from contextlib import contextmanager from django.conf import settings from django.db import connection as pg_connection -import os - NOT_READY = ([], [], []) @@ -16,24 +15,27 @@ def get_local_queuename(): def get_task_queuename(): - if os.getenv('AWX_COMPONENT') == 'web': - from awx.main.models.ha import Instance - - return ( - Instance.objects.filter( - node_type__in=[Instance.Types.CONTROL, Instance.Types.HYBRID], - node_state=Instance.States.READY, - enabled=True, - ) - .only('hostname') - .order_by('?') - .first() - .hostname - ) - - else: + if os.getenv('AWX_COMPONENT') != 'web': return settings.CLUSTER_HOST_ID + from awx.main.models.ha import Instance + + random_task_instance = ( + Instance.objects.filter( + node_type__in=(Instance.Types.CONTROL, Instance.Types.HYBRID), + node_state=Instance.States.READY, + enabled=True, + ) + .only('hostname') + .order_by('?') + .first() + ) + + if random_task_instance is None: + raise ValueError('No task instances are READY and Enabled.') + + return random_task_instance.hostname + class PubSub(object): def __init__(self, conn):