Record task status if running via CLI 41/27241/3
authorchenjiankun <chenjiankun1@huawei.com>
Thu, 19 Jan 2017 20:24:48 +0000 (20:24 +0000)
committerchenjiankun <chenjiankun1@huawei.com>
Fri, 20 Jan 2017 10:25:04 +0000 (10:25 +0000)
JIRA: YARDSTICK-542

Currently we do not record task status when using CLI to run task.
So I add this function.
If status=0, task is not done.
if status=1, task is done.
if status=2, there is an error.

Change-Id: Ib9b3c8abd233909c04f792115199250419fa8d7a
Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
yardstick/benchmark/core/task.py
yardstick/cmd/commands/task.py
yardstick/common/constants.py
yardstick/common/utils.py
yardstick/dispatcher/file.py

index 14b4686..87d70f4 100644 (file)
@@ -66,8 +66,6 @@ class Task(object):     # pragma: no cover
         if args.parse_only:
             sys.exit(0)
 
-        if os.path.isfile(args.output_file):
-            os.remove(args.output_file)
         # parse task_files
         for i in range(0, len(task_files)):
             one_task_start_time = time.time()
index d7c0a01..20ab086 100644 (file)
@@ -1,4 +1,4 @@
-##############################################################################
+#############################################################################
 # Copyright (c) 2015 Ericsson AB and others.
 #
 # All rights reserved. This program and the accompanying materials
@@ -9,10 +9,12 @@
 
 """ Handler for yardstick command 'task' """
 from __future__ import print_function
-
 from __future__ import absolute_import
+
 from yardstick.benchmark.core.task import Task
 from yardstick.common.utils import cliargs
+from yardstick.common.utils import write_json_to_file
+from yardstick.common import constants as consts
 from yardstick.cmd.commands import change_osloobj_to_paras
 
 output_file_default = "/tmp/yardstick.out"
@@ -42,4 +44,18 @@ class TaskCommands(object):
              action="store_true")
     def do_start(self, args, **kwargs):
         param = change_osloobj_to_paras(args)
-        Task().start(param, **kwargs)
+
+        self._init_result_file()
+
+        try:
+            Task().start(param)
+        except Exception as e:
+            self._write_error_data(e)
+
+    def _init_result_file(self):
+        data = {'status': 0, 'result': []}
+        write_json_to_file(consts.DEFAULT_OUTPUT_FILE, data)
+
+    def _write_error_data(self, error):
+        data = {'status': 2, 'result': str(error)}
+        write_json_to_file(consts.DEFAULT_OUTPUT_FILE, data)
index ffca4b3..85ef9fc 100644 (file)
@@ -57,3 +57,5 @@ ENV_ACTION_API = BASE_URL + '/yardstick/env/action'
 ASYNC_TASK_API = BASE_URL + '/yardstick/asynctask'
 
 SQLITE = 'sqlite:////tmp/yardstick.db'
+
+DEFAULT_OUTPUT_FILE = '/tmp/yardstick.out'
index 57ace14..473bbf5 100644 (file)
@@ -30,6 +30,7 @@ from keystoneauth1 import identity
 from keystoneauth1 import session
 from neutronclient.v2_0 import client
 from oslo_utils import importutils
+from oslo_serialization import jsonutils
 
 import yardstick
 
@@ -145,3 +146,12 @@ def get_neutron_client():
     sess = get_openstack_session()
     neutron_client = client.Client(session=sess)
     return neutron_client
+
+
+def write_json_to_file(path, data, mode='w'):
+    write_file(path, jsonutils.dump_as_bytes(data), mode)
+
+
+def write_file(path, data, mode='w'):
+    with open(path, mode) as f:
+        f.write(data)
index 9c728e9..8d3c369 100644 (file)
 
 from __future__ import absolute_import
 
-import logging
-import logging.handlers
-
-from oslo_serialization import jsonutils
-from oslo_config import cfg
-
 from yardstick.dispatcher.base import Base as DispatchBase
-
-CONF = cfg.CONF
-OPTS = [
-    cfg.StrOpt('file_path',
-               default='/tmp/yardstick.out',
-               help='Name and the location of the file to record '
-                    'data.'),
-    cfg.IntOpt('max_bytes',
-               default=0,
-               help='The max size of the file.'),
-    cfg.IntOpt('backup_count',
-               default=0,
-               help='The max number of the files to keep.'),
-]
-CONF.register_opts(OPTS, group="dispatcher_file")
+from yardstick.common import constants as consts
+from yardstick.common import utils
 
 
 class FileDispatcher(DispatchBase):
@@ -50,29 +31,13 @@ class FileDispatcher(DispatchBase):
 
     def __init__(self, conf):
         super(FileDispatcher, self).__init__(conf)
-        self.log = None
-
-        # if the directory and path are configured, then log to the file
-        if CONF.dispatcher_file.file_path:
-            dispatcher_logger = logging.Logger('dispatcher.file')
-            dispatcher_logger.setLevel(logging.INFO)
-            # create rotating file handler which logs result
-            rfh = logging.handlers.RotatingFileHandler(
-                self.conf.get('file_path', CONF.dispatcher_file.file_path),
-                maxBytes=CONF.dispatcher_file.max_bytes,
-                backupCount=CONF.dispatcher_file.backup_count,
-                encoding='utf8')
-
-            rfh.setLevel(logging.INFO)
-            # Only wanted the data to be saved in the file, not the
-            # project root logger.
-            dispatcher_logger.propagate = False
-            dispatcher_logger.addHandler(rfh)
-            self.log = dispatcher_logger
+        self.result = []
 
     def record_result_data(self, data):
-        if self.log:
-            self.log.info(jsonutils.dump_as_bytes(data))
+        self.result.append(data)
 
     def flush_result_data(self):
-        pass
+        file_path = self.conf.get('file_path', consts.DEFAULT_OUTPUT_FILE)
+
+        data = {'status': 1, 'result': self.result}
+        utils.write_json_to_file(file_path, data)