77a0f6ab7ec931d773d7b21050b8a63305fa02d3
[yardstick.git] / api / utils / daemonthread.py
1 import threading
2 import os
3 import datetime
4 import errno
5
6 from api import conf
7 from api.utils.influx import write_data_tasklist
8
9
10 class DaemonThread(threading.Thread):
11
12     def __init__(self, method, args):
13         super(DaemonThread, self).__init__(target=method, args=args)
14         self.method = method
15         self.command_list = args[0]
16         self.task_id = args[1]
17
18     def run(self):
19         timestamp = datetime.datetime.now()
20
21         try:
22             write_data_tasklist(self.task_id, timestamp, 0)
23             self.method(self.command_list, self.task_id)
24             write_data_tasklist(self.task_id, timestamp, 1)
25         except Exception as e:
26             write_data_tasklist(self.task_id, timestamp, 2, error=str(e))
27         finally:
28             _handle_testsuite_file(self.task_id)
29
30
31 def _handle_testsuite_file(task_id):
32     try:
33         os.remove(os.path.join(conf.TEST_SUITE_PATH, task_id + '.yaml'))
34     except OSError as e:
35         if e.errno != errno.ENOENT:
36             raise