Merge "Add smoke, components, features and performance test suite for Yatdstick"
[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 datetime
12 import errno
13
14 from api import conf
15 from api.utils.influx import write_data_tasklist
16
17
18 class DaemonThread(threading.Thread):
19
20     def __init__(self, method, args):
21         super(DaemonThread, self).__init__(target=method, args=args)
22         self.method = method
23         self.command_list = args[0]
24         self.task_id = args[1]
25
26     def run(self):
27         timestamp = datetime.datetime.now()
28
29         try:
30             write_data_tasklist(self.task_id, timestamp, 0)
31             self.method(self.command_list, self.task_id)
32             write_data_tasklist(self.task_id, timestamp, 1)
33         except Exception as e:
34             write_data_tasklist(self.task_id, timestamp, 2, error=str(e))
35         finally:
36             _handle_testsuite_file(self.task_id)
37
38
39 def _handle_testsuite_file(task_id):
40     try:
41         os.remove(os.path.join(conf.TEST_SUITE_PATH, task_id + '.yaml'))
42     except OSError as e:
43         if e.errno != errno.ENOENT:
44             raise