Skip to content

Commit 0e87edd

Browse files
committed
feat: try grabbing query files from artifacts directory
1 parent ada42d7 commit 0e87edd

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.16 on 2025-01-15 15:07
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('main', '0200_delete_token_cleanup_job'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='job',
15+
name='job_queries',
16+
field=models.JSONField(help_text='The discovered queries for indirect host counting', null=True),
17+
),
18+
]

awx/main/models/jobs.py

+1
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ class Meta:
607607
default=1,
608608
help_text=_("If ran as part of sliced jobs, the total number of slices. If 1, job is not part of a sliced job."),
609609
)
610+
job_queries = models.JSONField(null=True, help_text=_("The discovered queries for indirect host counting"))
610611

611612
def _get_parent_field_name(self):
612613
return 'job_template'

awx/main/tasks/callback.py

+34
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import json
2+
import os.path
23
import time
34
import logging
45
from collections import deque
56

67
# Django
78
from django.conf import settings
9+
from django.contrib.messages.api import error
810
from django_guid import get_guid
911
from django.utils.functional import cached_property
1012
from django.db import connections
@@ -15,11 +17,39 @@
1517
from awx.main.utils.update_model import update_model
1618
from awx.main.queue import CallbackQueueDispatcher
1719

20+
from flags.state import flag_enabled
21+
1822
logger = logging.getLogger('awx.main.tasks.callback')
1923

2024

25+
def try_load_query_file(artifact_dir) -> (bool, dict):
26+
if not flag_enabled("FEATURE_INDIRECT_NODE_COUNTING_ENABLED"):
27+
return False, None
28+
29+
queries_path = os.path.join(artifact_dir, "ansible_data.json")
30+
if not os.path.isfile(queries_path):
31+
logger.info(f"no query file found: {queries_path}")
32+
return False, None
33+
34+
try:
35+
f = open(queries_path, "r")
36+
except OSError as e:
37+
logger.error(f"error opening query file {queries_path}: {e}")
38+
return False, None
39+
40+
with f:
41+
try:
42+
queries = json.load(f)
43+
except ValueError as e:
44+
logger.error(f"error parsing query file {queries_path}: {e}")
45+
return False, None
46+
47+
return True, queries
48+
49+
2150
class RunnerCallback:
2251
def __init__(self, model=None):
52+
self.instance = None
2353
self.parent_workflow_job_id = None
2454
self.host_map = {}
2555
self.guid = get_guid()
@@ -214,6 +244,10 @@ def status_handler(self, status_data, runner_config):
214244
self.delay_update(**{field_name: field_value})
215245

216246
def artifacts_handler(self, artifact_dir):
247+
success, query_file_contents = try_load_query_file(artifact_dir)
248+
if success:
249+
self.instance = self.update_model(self.instance.pk, job_queries=query_file_contents)
250+
217251
self.artifacts_processed = True
218252

219253

0 commit comments

Comments
 (0)