aboutsummaryrefslogtreecommitdiffstats
path: root/api/utils/thread.py
diff options
context:
space:
mode:
authorchenjiankun <chenjiankun1@huawei.com>2017-06-26 09:46:24 +0000
committerchenjiankun <chenjiankun1@huawei.com>2017-06-29 11:25:17 +0000
commitda62008a0a98cb8793ea42827a7da5e149edd144 (patch)
treef91e35eabe04ebb8c6618345c0fd1b76c5f37a2c /api/utils/thread.py
parent1ff9df7e724eb0c981aebd5f5b8aa90db0da292b (diff)
Call core code directly in the API of run test case
JIRA: YARDSTICK-688 We need to call core code directly in the API of runTestCase. It would be more stable. Change-Id: I431a85ded7cd3b20da0462f947c25d91bb99decd Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
Diffstat (limited to 'api/utils/thread.py')
-rw-r--r--api/utils/thread.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/api/utils/thread.py b/api/utils/thread.py
new file mode 100644
index 000000000..2106548f5
--- /dev/null
+++ b/api/utils/thread.py
@@ -0,0 +1,37 @@
+import threading
+import logging
+
+from oslo_serialization import jsonutils
+
+from api.database.v1.handlers import TasksHandler
+from yardstick.common import constants as consts
+
+logger = logging.getLogger(__name__)
+logger.setLevel(logging.DEBUG)
+
+
+class TaskThread(threading.Thread):
+
+ def __init__(self, target, args):
+ super(TaskThread, self).__init__(target=target, args=args)
+ self.target = target
+ self.args = args
+
+ def run(self):
+ task_handler = TasksHandler()
+ data = {'task_id': self.args.task_id, 'status': consts.TASK_NOT_DONE}
+ task_handler.insert(data)
+
+ logger.info('Starting run task')
+ try:
+ data = self.target(self.args)
+ except Exception as e:
+ logger.exception('Task Failed')
+ update_data = {'status': consts.TASK_FAILED, 'error': str(e)}
+ task_handler.update_attr(self.args.task_id, update_data)
+ else:
+ logger.info('Task Finished')
+ logger.debug('Result: %s', data)
+
+ data['result'] = jsonutils.dumps(data.get('result', {}))
+ task_handler.update_attr(self.args.task_id, data)