Merge "associate an uuid to yardstick_key and yardstick_key.pub"
[yardstick.git] / api / utils / daemonthread.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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 errno
12
13 from api import conf
14 from api.database.handlers import TasksHandler
15
16
17 class DaemonThread(threading.Thread):
18
19     def __init__(self, method, args):
20         super(DaemonThread, self).__init__(target=method, args=args)
21         self.method = method
22         self.command_list = args[0]
23         self.task_dict = args[1]
24
25     def run(self):
26         self.task_dict['status'] = 0
27         task_id = self.task_dict['task_id']
28
29         try:
30             task_handler = TasksHandler()
31             task = task_handler.insert(self.task_dict)
32
33             self.method(self.command_list, task_id)
34
35             task_handler.update_status(task, 1)
36         except Exception as e:
37             task_handler.update_status(task, 2)
38             task_handler.update_error(task, str(e))
39         finally:
40             _handle_testsuite_file(task_id)
41
42
43 def _handle_testsuite_file(task_id):
44     try:
45         os.remove(os.path.join(conf.TEST_SUITE_PATH, task_id + '.yaml'))
46     except OSError as e:
47         if e.errno != errno.ENOENT:
48             raise