Make task_time_to_live work

CONF.task.task_time_to_live doesn't work now. When change it's value,
the task's properity 'expires_at' and 'updated_at' still have the
difference of 48 hours.

The reason is that taskflow create a new task object and doesn't use
the task_time_to_live which glance api passed before.

Change-Id: Iec77be56585b7fe183d9903515e25fd096a6c0b0
Closes-bug: #1488360
This commit is contained in:
wangxiyuan 2015-08-25 15:33:50 +08:00
parent 841d11f519
commit 5c3a3bd3d2
3 changed files with 42 additions and 5 deletions

View File

@ -61,11 +61,9 @@ class TasksController(object):
task_factory = self.gateway.get_task_factory(req.context)
executor_factory = self.gateway.get_task_executor_factory(req.context)
task_repo = self.gateway.get_task_repo(req.context)
live_time = CONF.task.task_time_to_live
try:
new_task = task_factory.new_task(task_type=task['type'],
owner=req.context.owner,
task_time_to_live=live_time,
task_input=task['input'])
task_repo.add(new_task)
task_executor = executor_factory.new_task_executor(req.context)

View File

@ -349,7 +349,7 @@ class Task(object):
def __init__(self, task_id, task_type, status, owner,
expires_at, created_at, updated_at,
task_input, result, message, task_time_to_live=48):
task_input, result, message):
if task_type not in self._supported_task_type:
raise exception.InvalidTaskType(task_type)
@ -364,6 +364,7 @@ class Task(object):
self.expires_at = expires_at
# NOTE(nikhil): We use '_time_to_live' to determine how long a
# task should live from the time it succeeds or fails.
task_time_to_live = CONF.task.task_time_to_live
self._time_to_live = datetime.timedelta(hours=task_time_to_live)
self.created_at = created_at
self.updated_at = updated_at
@ -458,7 +459,7 @@ class TaskStub(object):
class TaskFactory(object):
def new_task(self, task_type, owner, task_time_to_live=48,
def new_task(self, task_type, owner,
task_input=None, **kwargs):
task_id = str(uuid.uuid4())
status = 'pending'
@ -478,7 +479,6 @@ class TaskFactory(object):
task_input,
kwargs.get('message'),
kwargs.get('result'),
task_time_to_live
)

View File

@ -18,6 +18,7 @@ import datetime
import uuid
import mock
from oslo_config import cfg
from oslo_serialization import jsonutils
from oslo_utils import timeutils
# NOTE(jokke): simplified transition to py3, behaves like py2 xrange
@ -82,6 +83,9 @@ def _domain_fixture(task_id, **kwargs):
task = glance.domain.Task(**task_properties)
return task
CONF = cfg.CONF
CONF.import_opt('task_time_to_live', 'glance.common.config', group='task')
class TestTasksController(test_utils.BaseTestCase):
@ -96,6 +100,8 @@ class TestTasksController(test_utils.BaseTestCase):
self.policy,
self.notifier,
self.store)
self.gateway = glance.gateway.Gateway(self.db, self.store,
self.notifier, self.policy)
def _create_tasks(self):
now = timeutils.utcnow()
@ -325,6 +331,39 @@ class TestTasksController(test_utils.BaseTestCase):
self.assertEqual(
1, get_task_executor_factory.new_task_executor.call_count)
@mock.patch('glance.common.scripts.utils.get_image_data_iter')
@mock.patch('glance.common.scripts.utils.validate_location_uri')
def test_create_with_live_time(self, mock_validate_location_uri,
mock_get_image_data_iter):
request = unit_test_utils.get_fake_request()
task = {
"type": "import",
"input": {
"import_from": "http://download.cirros-cloud.net/0.3.4/"
"cirros-0.3.4-x86_64-disk.img",
"import_from_format": "qcow2",
"image_properties": {
"disk_format": "qcow2",
"container_format": "bare",
"name": "test-task"
}
}
}
new_task = self.controller.create(request, task=task)
executor_factory = self.gateway.get_task_executor_factory(
request.context)
task_executor = executor_factory.new_task_executor(request.context)
task_executor.begin_processing(new_task.task_id)
success_task = self.controller.get(request, new_task.task_id)
# ignore microsecond
task_live_time = (success_task.expires_at.replace(microsecond=0) -
success_task.updated_at.replace(microsecond=0))
task_live_time_hour = (task_live_time.days * 24 +
task_live_time.seconds / 3600)
self.assertEqual(CONF.task.task_time_to_live, task_live_time_hour)
@mock.patch.object(glance.gateway.Gateway, 'get_task_factory')
def test_notifications_on_create(self, mock_get_task_factory):
request = unit_test_utils.get_fake_request()