From 54f99749eb2229dcd0abe62c61b81a4420075e1a Mon Sep 17 00:00:00 2001 From: Aaron Tan Date: Fri, 25 Nov 2016 21:23:29 -0500 Subject: [PATCH] Provide linkage from spawned job back to wfj. --- awx/api/serializers.py | 2 +- .../0053_v310_linkage_back_to_workflow_job.py | 20 +++++++++++++++++++ awx/main/models/unified_jobs.py | 9 ++++++++- awx/main/scheduler/__init__.py | 5 +++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 awx/main/migrations/0053_v310_linkage_back_to_workflow_job.py diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 3103703039..9677c3a62b 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -566,7 +566,7 @@ class UnifiedJobSerializer(BaseSerializer): fields = ('*', 'unified_job_template', 'launch_type', 'status', 'failed', 'started', 'finished', 'elapsed', 'job_args', 'job_cwd', 'job_env', 'job_explanation', 'result_stdout', - 'execution_node', 'result_traceback') + 'execution_node', 'result_traceback', 'source_workflow_job') extra_kwargs = { 'unified_job_template': { 'source': 'unified_job_template_id', diff --git a/awx/main/migrations/0053_v310_linkage_back_to_workflow_job.py b/awx/main/migrations/0053_v310_linkage_back_to_workflow_job.py new file mode 100644 index 0000000000..f98840c3e4 --- /dev/null +++ b/awx/main/migrations/0053_v310_linkage_back_to_workflow_job.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('main', '0052_v310_inventory_name_non_unique'), + ] + + operations = [ + migrations.AddField( + model_name='unifiedjob', + name='source_workflow_job', + field=models.ForeignKey(related_name='spawned_jobs', on_delete=django.db.models.deletion.SET_NULL, default=None, editable=False, to='main.WorkflowJob', null=True), + ), + ] diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index 7e95e5abd7..92903a376e 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -528,7 +528,14 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique blank=True, related_name='%(class)s_labels' ) - + source_workflow_job = models.ForeignKey( + 'WorkflowJob', + null=True, + default=None, + editable=False, + related_name='spawned_jobs', + on_delete=models.SET_NULL, + ) def get_absolute_url(self): real_instance = self.get_real_instance() diff --git a/awx/main/scheduler/__init__.py b/awx/main/scheduler/__init__.py index 0ac737c375..ba4aa5eed5 100644 --- a/awx/main/scheduler/__init__.py +++ b/awx/main/scheduler/__init__.py @@ -116,6 +116,11 @@ class TaskManager(): for spawn_node in spawn_nodes: kv = spawn_node.get_job_kwargs() job = spawn_node.unified_job_template.create_unified_job(**kv) + # source_workflow_job is a job-specific field rather than a field copied from job + # template, therefore does not fit into the copy routine and should be put outside + # of create_unified_job. + job.source_workflow_job = workflow_job + job.save() spawn_node.job = job spawn_node.save() can_start = job.signal_start(**kv)