From 1296c6d0eb004a5c15b392bf7154db9217200631 Mon Sep 17 00:00:00 2001 From: Sabari Kumar Murugesan Date: Mon, 18 May 2015 14:08:32 -0700 Subject: [PATCH] Make create task as non-blocking Currently, the create task api is blocking and waits until the task is complete to return a 201 but that too with a Pending status. The patch runs the task executor in an eventlet thread. Closes-Bug: #1459051 Change-Id: I94f932afb5b754d9bcfa7bbbd1c450bdef7f28ef --- glance/api/v2/tasks.py | 4 +++- glance/tests/integration/v2/test_tasks_api.py | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/glance/api/v2/tasks.py b/glance/api/v2/tasks.py index 1645d16185..28a46486e7 100644 --- a/glance/api/v2/tasks.py +++ b/glance/api/v2/tasks.py @@ -25,6 +25,7 @@ import six import six.moves.urllib.parse as urlparse import webob.exc +from glance.api import common from glance.api import policy from glance.common import exception from glance.common import utils @@ -67,7 +68,8 @@ class TasksController(object): task_input=task['input']) task_repo.add(new_task) task_executor = executor_factory.new_task_executor(req.context) - new_task.run(task_executor) + pool = common.get_thread_pool("tasks_eventlet_pool") + pool.spawn_n(new_task.run, task_executor) except exception.Forbidden as e: msg = (_LW("Forbidden to create task. Reason: %(reason)s") % {'reason': utils.exception_to_str(e)}) diff --git a/glance/tests/integration/v2/test_tasks_api.py b/glance/tests/integration/v2/test_tasks_api.py index 6fe606825c..13f5efadb6 100644 --- a/glance/tests/integration/v2/test_tasks_api.py +++ b/glance/tests/integration/v2/test_tasks_api.py @@ -146,6 +146,10 @@ class TestTasksApi(base.ApiTest): headers=minimal_task_headers()) self.assertEqual(200, response.status) + # NOTE(sabari): wait for all task executions to finish before checking + # task status. + self._wait_on_task_execution() + # 4. GET /tasks # Get all tasks (not deleted) path = "/v2/tasks" @@ -169,10 +173,6 @@ class TestTasksApi(base.ApiTest): self.assertIsNotNone(task['created_at']) self.assertIsNotNone(task['updated_at']) - # NOTE(nikhil): wait for all task executions to finish before exiting - # else there is a risk of running into deadlock - self._wait_on_task_execution() - def test_task_schema_api(self): # 0. GET /schemas/task # Verify schema for task