From c0d38e91f5c41e7a7bba90a692b09c643b8a99e9 Mon Sep 17 00:00:00 2001 From: Jim Ladd Date: Wed, 10 Feb 2021 20:23:44 -0800 Subject: [PATCH] When saving JobEvents, include job_created * this is the partition key * .. used to determine which partition job event rows are sent to --- awx/main/models/events.py | 15 ++++++++++----- awx/main/tasks.py | 9 +++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/awx/main/models/events.py b/awx/main/models/events.py index 8a1bf23f70..96172641f6 100644 --- a/awx/main/models/events.py +++ b/awx/main/models/events.py @@ -430,6 +430,11 @@ class BasePlaybookEvent(CreatedModifiedModel): event = cls(**kwargs) if workflow_job_id: setattr(event, 'workflow_job_id', workflow_job_id) + # shouldn't job_created _always_ be present? + # if it's not, how could we save the event to the db? + job_created = kwargs.pop('job_created', None) + if job_created: + setattr(event, 'job_created', job_created) setattr(event, 'host_map', host_map) event._update_from_event_data() return event @@ -444,7 +449,7 @@ class JobEvent(BasePlaybookEvent): An event/message logged from the callback when running a job. """ - VALID_KEYS = BasePlaybookEvent.VALID_KEYS + ['job_id', 'workflow_job_id'] + VALID_KEYS = BasePlaybookEvent.VALID_KEYS + ['job_id', 'workflow_job_id', 'job_created'] class Meta: app_label = 'main' @@ -567,7 +572,7 @@ class JobEvent(BasePlaybookEvent): class ProjectUpdateEvent(BasePlaybookEvent): - VALID_KEYS = BasePlaybookEvent.VALID_KEYS + ['project_update_id', 'workflow_job_id'] + VALID_KEYS = BasePlaybookEvent.VALID_KEYS + ['project_update_id', 'workflow_job_id', 'job_created'] class Meta: app_label = 'main' @@ -685,7 +690,7 @@ class BaseCommandEvent(CreatedModifiedModel): class AdHocCommandEvent(BaseCommandEvent): - VALID_KEYS = BaseCommandEvent.VALID_KEYS + ['ad_hoc_command_id', 'event', 'host_name', 'host_id', 'workflow_job_id'] + VALID_KEYS = BaseCommandEvent.VALID_KEYS + ['ad_hoc_command_id', 'event', 'host_name', 'host_id', 'workflow_job_id', 'job_created'] class Meta: app_label = 'main' @@ -774,7 +779,7 @@ class AdHocCommandEvent(BaseCommandEvent): class InventoryUpdateEvent(BaseCommandEvent): - VALID_KEYS = BaseCommandEvent.VALID_KEYS + ['inventory_update_id', 'workflow_job_id'] + VALID_KEYS = BaseCommandEvent.VALID_KEYS + ['inventory_update_id', 'workflow_job_id', 'job_created'] class Meta: app_label = 'main' @@ -808,7 +813,7 @@ class InventoryUpdateEvent(BaseCommandEvent): class SystemJobEvent(BaseCommandEvent): - VALID_KEYS = BaseCommandEvent.VALID_KEYS + ['system_job_id'] + VALID_KEYS = BaseCommandEvent.VALID_KEYS + ['system_job_id', 'job_created'] class Meta: app_label = 'main' diff --git a/awx/main/tasks.py b/awx/main/tasks.py index e5bfacf483..6d5a774d23 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -781,6 +781,7 @@ class BaseTask(object): self.parent_workflow_job_id = None self.host_map = {} self.guid = GuidMiddleware.get_guid() + self.job_created = None def update_model(self, pk, _attempt=0, **updates): """Reload the model instance from the database and update the @@ -1158,6 +1159,10 @@ class BaseTask(object): event_data.pop('parent_uuid', None) if self.parent_workflow_job_id: event_data['workflow_job_id'] = self.parent_workflow_job_id + # Do we have to check if the field exists? if it doesn't + # how will be eventually store the event in the db? + if self.job_created: + event_data['job_created'] = self.job_created if self.host_map: host = event_data.get('event_data', {}).get('host', '').strip() if host: @@ -1283,6 +1288,10 @@ class BaseTask(object): if self.instance.spawned_by_workflow: self.parent_workflow_job_id = self.instance.get_workflow_job().id + # TODO: can we count on instance always having created? + # If we can't how can we store the job_event? + self.job_created = self.instance.created + try: self.instance.send_notification_templates("running") private_data_dir = self.build_private_data_dir(self.instance)