3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
11 Used to handle multi-thread tasks
17 from oslo_serialization import jsonutils
20 LOGGER = logging.getLogger(__name__)
23 class TaskThread(threading.Thread):
24 """ Task Thread Class """
26 def __init__(self, target, args, handler):
27 super(TaskThread, self).__init__(target=target, args=args)
30 self.handler = handler
33 """ Override the function run: run testcase and update database """
34 update_data = {'task_id': self.args.get('task_id'),
35 'status': 'IN PROGRESS'}
36 self.handler.insert(update_data)
38 LOGGER.info('Starting running test case')
41 data = self.target(self.args)
42 except Exception as err: # pylint: disable=broad-except
43 LOGGER.exception('Task Failed')
44 update_data = {'status': 'FAIL', 'error': str(err)}
45 self.handler.update_attr(self.args.get('task_id'), update_data)
47 LOGGER.info('Task Finished')
48 LOGGER.debug('Result: %s', data)
49 new_data = {'status': 'FINISHED',
50 'result': jsonutils.dumps(data.get('result', {}))}
52 self.handler.update_attr(self.args.get('task_id'), new_data)