[Kimchi-devel] [PATCH V2] [Kimchi] Update usage of add_task() method.

pvital at linux.vnet.ibm.com pvital at linux.vnet.ibm.com
Mon Aug 29 15:42:21 UTC 2016


From: Paulo Vital <pvital at linux.vnet.ibm.com>

After move AsyncTask to memory, the call to add a new task has changed.
This patch updates all add_task() calls to address the new way.

Signed-off-by: Paulo Vital <pvital at linux.vnet.ibm.com>
---
 mockmodel.py            |  9 +++++----
 model/storagepools.py   |  7 ++++---
 model/storagevolumes.py |  9 +++++----
 model/vmhostdevs.py     | 26 +++++++++++++-------------
 model/vms.py            | 18 +++++++++---------
 model/vmsnapshots.py    |  6 +++---
 tests/test_model.py     | 21 ++++++++++-----------
 tests/test_rest.py      | 15 ++++++---------
 8 files changed, 55 insertions(+), 56 deletions(-)

diff --git a/mockmodel.py b/mockmodel.py
index cd06ee3..d0ba8bd 100644
--- a/mockmodel.py
+++ b/mockmodel.py
@@ -28,9 +28,10 @@ from collections import defaultdict
 from lxml import objectify
 from lxml.builder import E
 
+from wok.asynctask import AsyncTask
 from wok.exception import NotFoundError, OperationFailed
 from wok.objectstore import ObjectStore
-from wok.utils import add_task, convert_data_size
+from wok.utils import convert_data_size
 from wok.xmlutils.utils import xml_item_update
 
 from wok.plugins.kimchi import imageinfo
@@ -416,9 +417,9 @@ class MockModel(Model):
     def _mock_vmsnapshots_create(self, vm_name, params):
         name = params.get('name', unicode(int(time.time())))
         params = {'vm_name': vm_name, 'name': name}
-        taskid = add_task(u'/plugins/kimchi/vms/%s/snapshots/%s' %
-                          (vm_name, name), self._vmsnapshots_create_task,
-                          self.objstore, params)
+        taskid = AsyncTask(u'/plugins/kimchi/vms/%s/snapshots/%s' %
+                           (vm_name, name), self._vmsnapshots_create_task,
+                           params).id
         return self.task_lookup(taskid)
 
     def _vmsnapshots_create_task(self, cb, params):
diff --git a/model/storagepools.py b/model/storagepools.py
index a2dbaec..92d6436 100644
--- a/model/storagepools.py
+++ b/model/storagepools.py
@@ -21,9 +21,10 @@ import libvirt
 import lxml.etree as ET
 from lxml.builder import E
 
+from wok.asynctask import AsyncTask
 from wok.exception import InvalidOperation, MissingParameter
 from wok.exception import NotFoundError, OperationFailed
-from wok.utils import add_task, run_command, wok_log
+from wok.utils import run_command, wok_log
 from wok.xmlutils.utils import xpath_get_text
 
 from wok.plugins.kimchi.config import config, get_kimchi_version, kimchiPaths
@@ -252,8 +253,8 @@ class StoragePoolsModel(object):
 
         params['path'] = self.scanner.scan_dir_prepare(params['name'])
         scan_params['pool_path'] = params['path']
-        task_id = add_task('/plugins/kimchi/storagepools/%s' % ISO_POOL_NAME,
-                           self.scanner.start_scan, self.objstore, scan_params)
+        task_id = AsyncTask('/plugins/kimchi/storagepools/%s' % ISO_POOL_NAME,
+                            self.scanner.start_scan, scan_params).id
         # Record scanning-task/storagepool mapping for future querying
         try:
             with self.objstore as session:
diff --git a/model/storagevolumes.py b/model/storagevolumes.py
index 7b2272b..4708674 100644
--- a/model/storagevolumes.py
+++ b/model/storagevolumes.py
@@ -28,9 +28,10 @@ import time
 import urllib2
 from lxml.builder import E
 
+from wok.asynctask import AsyncTask
 from wok.exception import InvalidOperation, InvalidParameter, IsoFormatError
 from wok.exception import MissingParameter, NotFoundError, OperationFailed
-from wok.utils import add_task, get_unique_file_name
+from wok.utils import get_unique_file_name
 from wok.utils import probe_file_permission_as_user, wok_log
 from wok.xmlutils.utils import xpath_get_text
 from wok.model.tasks import TaskModel
@@ -124,7 +125,7 @@ class StorageVolumesModel(object):
         params['pool'] = pool_name
         targeturi = '/plugins/kimchi/storagepools/%s/storagevolumes/%s' \
                     % (pool_name, name)
-        taskid = add_task(targeturi, create_func, self.objstore, params)
+        taskid = AsyncTask(targeturi, create_func, params).id
         return self.task.lookup(taskid)
 
     def _create_volume_with_capacity(self, cb, params):
@@ -441,8 +442,8 @@ class StorageVolumeModel(object):
                   'new_pool': new_pool,
                   'new_name': new_name}
         target_uri = u'/plugins/kimchi/storagepools/%s/storagevolumes/%s/clone'
-        taskid = add_task(target_uri % (pool, new_name), self._clone_task,
-                          self.objstore, params)
+        taskid = AsyncTask(target_uri % (pool, new_name), self._clone_task,
+                           params).id
         return self.task.lookup(taskid)
 
     def _clone_task(self, cb, params):
diff --git a/model/vmhostdevs.py b/model/vmhostdevs.py
index e2299e2..0c89959 100644
--- a/model/vmhostdevs.py
+++ b/model/vmhostdevs.py
@@ -26,12 +26,13 @@ from lxml import etree, objectify
 from lxml.builder import E, ElementMaker
 from operator import itemgetter
 
+from wok.asynctask import AsyncTask
 from wok.exception import InvalidOperation, InvalidParameter, NotFoundError
 from wok.exception import OperationFailed
 from wok.message import WokMessage
 from wok.model.tasks import TaskModel
 from wok.rollbackcontext import RollbackContext
-from wok.utils import add_task, run_command, wok_log
+from wok.utils import run_command, wok_log
 
 from wok.plugins.kimchi.model.config import CapabilitiesModel
 from wok.plugins.kimchi.model.host import DeviceModel, DevicesModel
@@ -92,11 +93,11 @@ class VMHostDevsModel(object):
         dev_info = self.dev_model.lookup(dev_name)
 
         if dev_info['device_type'] == 'pci':
-            taskid = add_task(u'/plugins/kimchi/vms/%s/hostdevs/' %
-                              VMModel.get_vm(vmid, self.conn).name(),
-                              self._attach_pci_device, self.objstore,
-                              {'vmid': vmid, 'dev_info': dev_info,
-                               'lock': threading.RLock()})
+            taskid = AsyncTask(u'/plugins/kimchi/vms/%s/hostdevs/' %
+                               VMModel.get_vm(vmid, self.conn).name(),
+                               self._attach_pci_device,
+                               {'vmid': vmid, 'dev_info': dev_info,
+                                'lock': threading.RLock()}).id
             return self.task.lookup(taskid)
 
         with RollbackContext() as rollback:
@@ -110,11 +111,11 @@ class VMHostDevsModel(object):
 
             rollback.commitAll()
 
-        taskid = add_task(u'/plugins/kimchi/vms/%s/hostdevs/' %
-                          VMModel.get_vm(vmid, self.conn).name(),
-                          '_attach_%s_device' % dev_info['device_type'],
-                          self.objstore, {'vmid': vmid, 'dev_info': dev_info,
-                                          'lock': threading.RLock()})
+        taskid = AsyncTask(u'/plugins/kimchi/vms/%s/hostdevs/' %
+                           VMModel.get_vm(vmid, self.conn).name(),
+                           '_attach_%s_device' % dev_info['device_type'],
+                           {'vmid': vmid, 'dev_info': dev_info,
+                            'lock': threading.RLock()}).id
 
         return self.task.lookup(taskid)
 
@@ -604,8 +605,7 @@ class VMHostDevModel(object):
                        'lock': threading.RLock()}
         task_uri = u'/plugins/kimchi/vms/%s/hostdevs/%s' % \
             (VMModel.get_vm(vmid, self.conn).name(), dev_name)
-        taskid = add_task(task_uri, self._detach_device, self.objstore,
-                          task_params)
+        taskid = AsyncTask(task_uri, self._detach_device, task_params).id
         return self.task.lookup(taskid)
 
     def _event_devices(self, conn, dom, alias, opaque):
diff --git a/model/vms.py b/model/vms.py
index 3380278..7f607f5 100644
--- a/model/vms.py
+++ b/model/vms.py
@@ -34,12 +34,13 @@ from lxml import etree, objectify
 from lxml.builder import E
 from xml.etree import ElementTree
 
+from wok.asynctask import AsyncTask
 from wok.config import config
 from wok.exception import InvalidOperation, InvalidParameter
 from wok.exception import NotFoundError, OperationFailed
 from wok.model.tasks import TaskModel
 from wok.rollbackcontext import RollbackContext
-from wok.utils import add_task, convert_data_size
+from wok.utils import convert_data_size
 from wok.utils import import_class, run_setfacl_set_attr, run_command, wok_log
 from wok.xmlutils.utils import dictize, xpath_get_text, xml_item_insert
 from wok.xmlutils.utils import xml_item_remove, xml_item_update
@@ -144,8 +145,8 @@ class VMsModel(object):
                 'graphics': params.get('graphics', {}),
                 "title": params.get("title", ""),
                 "description": params.get("description", "")}
-        taskid = add_task(u'/plugins/kimchi/vms/%s' % name, self._create_task,
-                          self.objstore, data)
+        taskid = AsyncTask(u'/plugins/kimchi/vms/%s' % name, self._create_task,
+                           data).id
 
         return self.task.lookup(taskid)
 
@@ -349,9 +350,9 @@ class VMModel(object):
         new_name = get_next_clone_name(current_vm_names, name, ts=True)
 
         # create a task with the actual clone function
-        taskid = add_task(u'/plugins/kimchi/vms/%s/clone' % new_name,
-                          self._clone_task, self.objstore,
-                          {'name': name, 'new_name': new_name})
+        taskid = AsyncTask(u'/plugins/kimchi/vms/%s/clone' % new_name,
+                           self._clone_task, {'name': name,
+                                              'new_name': new_name}).id
 
         return self.task.lookup(taskid)
 
@@ -1898,9 +1899,8 @@ class VMModel(object):
                   'non_shared': non_shared,
                   'remote_host': remote_host,
                   'user': user}
-        task_id = add_task('/plugins/kimchi/vms/%s/migrate' % name,
-                           self._migrate_task,
-                           self.objstore, params)
+        task_id = AsyncTask('/plugins/kimchi/vms/%s/migrate' % name,
+                            self._migrate_task, params).id
 
         return self.task.lookup(task_id)
 
diff --git a/model/vmsnapshots.py b/model/vmsnapshots.py
index 6f2483c..6589306 100644
--- a/model/vmsnapshots.py
+++ b/model/vmsnapshots.py
@@ -23,8 +23,8 @@ import time
 from lxml import objectify
 from lxml.builder import E
 
+from wok.asynctask import AsyncTask
 from wok.exception import InvalidOperation, NotFoundError, OperationFailed
-from wok.utils import add_task
 from wok.xmlutils.utils import xpath_get_text
 from wok.model.tasks import TaskModel
 
@@ -74,8 +74,8 @@ class VMSnapshotsModel(object):
         name = params.get('name', unicode(int(time.time())))
 
         task_params = {'vm_name': vm_name, 'name': name}
-        taskid = add_task(u'/plugins/kimchi/vms/%s/snapshots/%s' % (vm_name,
-                          name), self._create_task, self.objstore, task_params)
+        taskid = AsyncTask(u'/plugins/kimchi/vms/%s/snapshots/%s' % (vm_name,
+                           name), self._create_task, task_params).id
         return self.task.lookup(taskid)
 
     def _create_task(self, cb, params):
diff --git a/tests/test_model.py b/tests/test_model.py
index e77d4fd..d9ffd5e 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -36,12 +36,13 @@ from mock import call, mock_open, patch
 import tests.utils as utils
 
 import wok.objectstore
+from wok.asynctask import AsyncTask
 from wok.basemodel import Singleton
 from wok.config import config
 from wok.exception import InvalidOperation
 from wok.exception import InvalidParameter, NotFoundError, OperationFailed
 from wok.rollbackcontext import RollbackContext
-from wok.utils import add_task, convert_data_size, get_task_id
+from wok.utils import convert_data_size
 from wok.xmlutils.utils import xpath_get_text
 
 from wok.plugins.gingerbase import netinfo
@@ -1401,30 +1402,28 @@ class ModelTests(unittest.TestCase):
 
         inst = model.Model('test:///default',
                            objstore_loc=self.tmp_store)
-        taskid = add_task('', quick_op, inst.objstore, 'Hello')
+        taskid = AsyncTask('', quick_op, 'Hello').id
         inst.task_wait(taskid)
-        self.assertEquals(get_task_id(), taskid)
         self.assertEquals('finished', inst.task_lookup(taskid)['status'])
         self.assertEquals('Hello', inst.task_lookup(taskid)['message'])
 
-        taskid = add_task('', long_op, inst.objstore,
-                          {'delay': 3, 'result': False,
-                           'message': 'It was not meant to be'})
-        self.assertEquals(get_task_id(), taskid)
+        params = {'delay': 3, 'result': False,
+                  'message': 'It was not meant to be'}
+        taskid = AsyncTask('', long_op, params).id
         self.assertEquals('running', inst.task_lookup(taskid)['status'])
-        self.assertEquals('OK', inst.task_lookup(taskid)['message'])
+        self.assertEquals('The request is being processing.',
+                          inst.task_lookup(taskid)['message'])
         inst.task_wait(taskid)
         self.assertEquals('failed', inst.task_lookup(taskid)['status'])
         self.assertEquals('It was not meant to be',
                           inst.task_lookup(taskid)['message'])
-        taskid = add_task('', abnormal_op, inst.objstore, {})
+        taskid = AsyncTask('', abnormal_op, {}).id
         inst.task_wait(taskid)
         self.assertEquals('Exception raised',
                           inst.task_lookup(taskid)['message'])
         self.assertEquals('failed', inst.task_lookup(taskid)['status'])
 
-        taskid = add_task('', continuous_ops, inst.objstore,
-                          {'result': True})
+        taskid = AsyncTask('', continuous_ops, {'result': True}).id
         self.assertEquals('running', inst.task_lookup(taskid)['status'])
         inst.task_wait(taskid, timeout=10)
         self.assertEquals('finished', inst.task_lookup(taskid)['status'])
diff --git a/tests/test_rest.py b/tests/test_rest.py
index 80596c2..90a808a 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -30,8 +30,8 @@ from functools import partial
 from tests.utils import get_free_port, patch_auth, request
 from tests.utils import run_server, wait_task
 
+from wok.asynctask import AsyncTask
 from wok.rollbackcontext import RollbackContext
-from wok.utils import add_task
 
 from wok.plugins.kimchi import mockmodel
 from wok.plugins.kimchi.osinfo import get_template_default
@@ -1305,22 +1305,19 @@ class RestTests(unittest.TestCase):
         )
 
     def test_tasks(self):
-        id1 = add_task('/plugins/kimchi/tasks/1', self._async_op,
-                       model.objstore)
-        id2 = add_task('/plugins/kimchi/tasks/2', self._except_op,
-                       model.objstore)
-        id3 = add_task('/plugins/kimchi/tasks/3', self._intermid_op,
-                       model.objstore)
+        id1 = AsyncTask('/plugins/kimchi/tasks/1', self._async_op).id
+        id2 = AsyncTask('/plugins/kimchi/tasks/2', self._except_op).id
+        id3 = AsyncTask('/plugins/kimchi/tasks/3', self._intermid_op).id
 
         target_uri = urllib2.quote('^/plugins/kimchi/tasks/*', safe="")
         filter_data = 'status=running&target_uri=%s' % target_uri
         tasks = json.loads(
             self.request('/plugins/kimchi/tasks?%s' % filter_data).read()
         )
-        self.assertEquals(3, len(tasks))
+        self.assertLessEqual(3, len(tasks))
 
         tasks = json.loads(self.request('/plugins/kimchi/tasks').read())
-        tasks_ids = [int(t['id']) for t in tasks]
+        tasks_ids = [t['id'] for t in tasks]
         self.assertEquals(set([id1, id2, id3]) - set(tasks_ids), set([]))
         wait_task(self._task_lookup, id2)
         foo2 = json.loads(
-- 
2.7.4




More information about the Kimchi-devel mailing list