summaryrefslogtreecommitdiffstats
path: root/restful_server/db.py
diff options
context:
space:
mode:
authorzhifeng.jiang <jiang.zhifeng@zte.com.cn>2016-09-09 23:46:07 +0800
committerzhifeng.jiang <jiang.zhifeng@zte.com.cn>2016-09-20 20:16:00 +0800
commit64db4bb925f771b053f922fe324a624c5da75ad9 (patch)
treed12977196a087f7a355f9349fe6872a08bc4d77a /restful_server/db.py
parent471817a86ae20cb288347fbedcb865ac7e31e60f (diff)
Run benchmark test in restful server post api
modification: Call ansible async in restful server post api Set the job state 'finished' when finish benchmark test Terminate the next benchmark in restful server delete api job result will be in next commit JIRA:QTIP-97 Change-Id: I252482dddd9b35ba33f992e8ea19037d8919fad6 Signed-off-by: zhifeng.jiang <jiang.zhifeng@zte.com.cn>
Diffstat (limited to 'restful_server/db.py')
-rw-r--r--restful_server/db.py41
1 files changed, 32 insertions, 9 deletions
diff --git a/restful_server/db.py b/restful_server/db.py
index 42808b80..916fc031 100644
--- a/restful_server/db.py
+++ b/restful_server/db.py
@@ -10,6 +10,7 @@ from datetime import datetime
import uuid
jobs = {}
+threads = {}
def create_job(args):
@@ -23,8 +24,8 @@ def create_job(args):
'suite_name': args["suite_name"],
'max-minutes': args["max-minutes"],
'type': args["type"],
- 'start-time': str(datetime.now()),
- 'end-time': None,
+ 'start_time': str(datetime.now()),
+ 'end_time': None,
'state': 'processing',
'state_detail': [],
'result': []}
@@ -33,7 +34,9 @@ def create_job(args):
def delete_job(job_id):
- if job_id in jobs.keys():
+ if job_id in threads:
+ stop_thread(job_id)
+ if job_id in jobs:
jobs[job_id]['end_time'] = str(datetime.now())
jobs[job_id]['state'] = 'terminated'
return True
@@ -42,23 +45,24 @@ def delete_job(job_id):
def get_job_info(job_id):
- if job_id in jobs.keys():
+ if job_id in jobs:
return jobs[job_id]
else:
return None
-def finish_job(job_id, state):
- jobs[job_id]['end-time'] = str(datetime.now())
- jobs[job_id]['state'] = state
+def finish_job(job_id):
+ jobs[job_id]['end_time'] = str(datetime.now())
+ jobs[job_id]['state'] = 'finished'
+ del threads[job_id]
def update_job_state_detail(job_id, state_detail):
- jobs[job_id][state_detail] = state_detail
+ jobs[job_id]['state_detail'] = state_detail
def update_job_result(job_id, result):
- jobs[job_id][result] = result
+ jobs[job_id]['result'] = result
def is_job_timeout(job_id):
@@ -66,3 +70,22 @@ def is_job_timeout(job_id):
"%Y-%m-%d %H:%M:%S.%f")
return True if jobs[job_id]['max-minutes'] * 60 < period.total_seconds()\
else False
+
+
+def start_thread(job_id, thread, thread_stop):
+ threads[job_id] = {'thread': thread,
+ 'thread_stop': thread_stop}
+ thread.start()
+
+
+def stop_thread(job_id):
+ if threads[job_id]['thread'].isAlive():
+ threads[job_id]['thread_stop'].set()
+ threads[job_id]['thread'].join()
+ if job_id in threads:
+ del threads[job_id]
+
+
+def update_benmark_state_in_state_detail(job_id, benchmark, benchmark_state):
+ filter(lambda x: x["benchmark"] == benchmark,
+ get_job_info(job_id)["state_detail"])[0]['state'] = benchmark_state