Updating NSBperf to print right testcase
[yardstick.git] / api / utils / thread.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import threading
10 import os
11 import logging
12
13 from oslo_serialization import jsonutils
14
15 from yardstick.common import constants as consts
16
17 LOG = logging.getLogger(__name__)
18 LOG.setLevel(logging.DEBUG)
19
20
21 class TaskThread(threading.Thread):
22
23     def __init__(self, target, args, handler):
24         super(TaskThread, self).__init__(target=target, args=args)
25         self.target = target
26         self.args = args
27         self.handler = handler
28
29     def run(self):
30         if self.handler.__class__.__name__.lower().startswith('v2'):
31             self.handler.update_attr(self.args.task_id, {'status': consts.TASK_NOT_DONE})
32         else:
33             update_data = {'task_id': self.args.task_id, 'status': consts.TASK_NOT_DONE}
34             self.handler.insert(update_data)
35
36         LOG.info('Starting run task')
37         try:
38             data = self.target(self.args)
39         except Exception as e:
40             LOG.exception('Task Failed')
41             update_data = {'status': consts.TASK_FAILED, 'error': str(e)}
42             self.handler.update_attr(self.args.task_id, update_data)
43         else:
44             LOG.info('Task Finished')
45             LOG.debug('Result: %s', data)
46
47             if self.handler.__class__.__name__.lower().startswith('v2'):
48                 new_data = {'status': consts.TASK_DONE, 'result': jsonutils.dumps(data['result'])}
49                 self.handler.update_attr(self.args.task_id, new_data)
50                 os.remove(self.args.inputfile[0])
51             else:
52                 data['result'] = jsonutils.dumps(data.get('result', {}))
53                 self.handler.update_attr(self.args.task_id, data)