diff options
Diffstat (limited to 'api/utils')
-rw-r--r-- | api/utils/common.py | 22 | ||||
-rw-r--r-- | api/utils/daemonthread.py | 49 | ||||
-rw-r--r-- | api/utils/thread.py | 37 |
3 files changed, 38 insertions, 70 deletions
diff --git a/api/utils/common.py b/api/utils/common.py index f8b0d40ba..8398b8f60 100644 --- a/api/utils/common.py +++ b/api/utils/common.py @@ -13,10 +13,8 @@ import logging from flask import jsonify import six -from api.utils.daemonthread import DaemonThread -from yardstick.cmd.cli import YardstickCLI - logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) def translate_to_str(obj): @@ -29,24 +27,6 @@ def translate_to_str(obj): return obj -def get_command_list(command_list, opts, args): - - command_list.append(args) - - command_list.extend(('--{}'.format(k) for k in opts if k != 'task-args')) - - task_args = opts.get('task-args', '') - if task_args: - command_list.extend(['--task-args', str(task_args)]) - - return command_list - - -def exec_command_task(command_list, task_dict): # pragma: no cover - daemonthread = DaemonThread(YardstickCLI().api, (command_list, task_dict)) - daemonthread.start() - - def error_handler(message): logger.debug(message) result = { diff --git a/api/utils/daemonthread.py b/api/utils/daemonthread.py deleted file mode 100644 index 3d5625547..000000000 --- a/api/utils/daemonthread.py +++ /dev/null @@ -1,49 +0,0 @@ -############################################################################## -# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others. -# -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 -############################################################################## -from __future__ import absolute_import -import threading -import os -import errno - -from yardstick.common import constants as consts -from api.database.handlers import TasksHandler - - -class DaemonThread(threading.Thread): - - def __init__(self, method, args): - super(DaemonThread, self).__init__(target=method, args=args) - self.method = method - self.command_list = args[0] - self.task_dict = args[1] - - def run(self): - self.task_dict['status'] = 0 - task_id = self.task_dict['task_id'] - - try: - task_handler = TasksHandler() - task = task_handler.insert(self.task_dict) - - self.method(self.command_list, task_id) - - task_handler.update_status(task, 1) - except Exception as e: - task_handler.update_status(task, 2) - task_handler.update_error(task, str(e)) - finally: - _handle_testsuite_file(task_id) - - -def _handle_testsuite_file(task_id): - try: - os.remove(os.path.join(consts.TESTSUITE_DIR, task_id + '.yaml')) - except OSError as e: - if e.errno != errno.ENOENT: - raise 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) |