Merge "test_vnf_generic: fix unitests when files are present"
[yardstick.git] / api / utils / daemonthread.py
index 77a0f6a..0049834 100644 (file)
@@ -1,10 +1,18 @@
+##############################################################################
+# 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 datetime
 import errno
 
 from api import conf
-from api.utils.influx import write_data_tasklist
+from api.database.handlers import TasksHandler
 
 
 class DaemonThread(threading.Thread):
@@ -13,19 +21,24 @@ class DaemonThread(threading.Thread):
         super(DaemonThread, self).__init__(target=method, args=args)
         self.method = method
         self.command_list = args[0]
-        self.task_id = args[1]
+        self.task_dict = args[1]
 
     def run(self):
-        timestamp = datetime.datetime.now()
+        self.task_dict['status'] = 0
+        task_id = self.task_dict['task_id']
 
         try:
-            write_data_tasklist(self.task_id, timestamp, 0)
-            self.method(self.command_list, self.task_id)
-            write_data_tasklist(self.task_id, timestamp, 1)
+            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:
-            write_data_tasklist(self.task_id, timestamp, 2, error=str(e))
+            task_handler.update_status(task, 2)
+            task_handler.update_error(task, str(e))
         finally:
-            _handle_testsuite_file(self.task_id)
+            _handle_testsuite_file(task_id)
 
 
 def _handle_testsuite_file(task_id):