From 7da52c8bef05beae963e90b9b45d226b30ed8410 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Tue, 28 Feb 2017 12:39:55 -0500 Subject: [PATCH 1/4] emit job status over socket after database commit * Wait until the newly created job record hits the database before telling the websocket clients that the job's status is "pending" --- awx/main/models/unified_jobs.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index f17b2b4c55..7744f27821 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -964,10 +964,13 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique # Save the pending status, and inform the SocketIO listener. self.update_fields(start_args=json.dumps(kwargs), status='pending') - self.websocket_emit_status("pending") - from awx.main.scheduler.tasks import run_job_launch - connection.on_commit(lambda: run_job_launch.delay(self.id)) + def post_commit(): + from awx.main.scheduler.tasks import run_job_launch + self.websocket_emit_status("pending") + run_job_launch.delay(self.id) + + connection.on_commit(post_commit) # Each type of unified job has a different Task class; get the # appropirate one. From b85361732be4fd4db9ba6457fc1e9967eacfb6b2 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Tue, 28 Feb 2017 12:53:59 -0500 Subject: [PATCH 2/4] Revert "emit job status over socket after database commit" This reverts commit edefeeacdaf11cf484a4b4893ee9acde5ef85390. --- awx/main/models/unified_jobs.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index 7744f27821..f17b2b4c55 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -964,13 +964,10 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique # Save the pending status, and inform the SocketIO listener. self.update_fields(start_args=json.dumps(kwargs), status='pending') + self.websocket_emit_status("pending") - def post_commit(): - from awx.main.scheduler.tasks import run_job_launch - self.websocket_emit_status("pending") - run_job_launch.delay(self.id) - - connection.on_commit(post_commit) + from awx.main.scheduler.tasks import run_job_launch + connection.on_commit(lambda: run_job_launch.delay(self.id)) # Each type of unified job has a different Task class; get the # appropirate one. From e09497108e1883dc07ecb03463b657e3bbf6934f Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Tue, 28 Feb 2017 12:56:55 -0500 Subject: [PATCH 3/4] all job status change websockets events should happen after the job hits the database --- awx/main/models/unified_jobs.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index f17b2b4c55..2ccae7fdaf 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -880,7 +880,7 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique workflow_node_id=self.workflow_node_id)) return websocket_data - def websocket_emit_status(self, status): + def _websocket_emit_status(self, status): status_data = dict(unified_job_id=self.id, status=status) status_data.update(self.websocket_emit_data()) status_data['group_name'] = 'jobs' @@ -890,6 +890,9 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique status_data['group_name'] = "workflow_events" emit_channel_notification('workflow_events-' + str(self.workflow_job_id), status_data) + def websocket_emit_status(self, status): + connection.on_commit(lambda: self._websocket_emit_status(status)) + def notification_data(self): return dict(id=self.id, name=self.name, From c4fb88c0d9c59030af0349886a91b88b8a290d31 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Tue, 28 Feb 2017 13:04:21 -0500 Subject: [PATCH 4/4] remove uneeded post commit wrapper * Since we changed the lower level method to always use post commit message emit --- awx/main/scheduler/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awx/main/scheduler/__init__.py b/awx/main/scheduler/__init__.py index a48ca3ad23..dc0a4c82e0 100644 --- a/awx/main/scheduler/__init__.py +++ b/awx/main/scheduler/__init__.py @@ -382,7 +382,7 @@ class TaskManager(): )) task_obj.save() _send_notification_templates(task_obj, 'failed') - connection.on_commit(lambda: task_obj.websocket_emit_status('failed')) + task_obj.websocket_emit_status('failed') logger.error("Task %s appears orphaned... marking as failed" % task)