mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
testclient: fix fix asyn job query for sync command
This commit is contained in:
parent
f8aaf1a126
commit
9777b7d193
@ -20,21 +20,25 @@ class jobStatus(object):
|
||||
self.jobId = None
|
||||
self.responsecls = None
|
||||
class workThread(threading.Thread):
|
||||
def __init__(self, in_queue, outqueue, apiClient, db=None):
|
||||
def __init__(self, in_queue, outqueue, apiClient, db=None, lock=None):
|
||||
threading.Thread.__init__(self)
|
||||
self.inqueue = in_queue
|
||||
self.output = outqueue
|
||||
self.connection = copy.copy(apiClient.connection)
|
||||
self.connection = apiClient.connection
|
||||
self.db = None
|
||||
self.lock = lock
|
||||
|
||||
def queryAsynJob(self, job):
|
||||
if job.jobId is None:
|
||||
return job
|
||||
|
||||
try:
|
||||
self.lock.acquire()
|
||||
result = self.connection.pollAsyncJob(job.jobId, job.responsecls).jobresult
|
||||
except cloudstackException.cloudstackAPIException, e:
|
||||
result = str(e)
|
||||
finally:
|
||||
self.lock.release()
|
||||
|
||||
job.result = result
|
||||
return job
|
||||
@ -45,8 +49,11 @@ class workThread(threading.Thread):
|
||||
jobstatus = jobStatus()
|
||||
jobId = None
|
||||
try:
|
||||
if not cmd.isAsync:
|
||||
self.lock.acquire()
|
||||
|
||||
if cmd.isAsync == "false":
|
||||
jobstatus.startTime = time.time()
|
||||
|
||||
result = self.connection.make_request(cmd)
|
||||
jobstatus.result = result
|
||||
jobstatus.endTime = time.time()
|
||||
@ -69,6 +76,9 @@ class workThread(threading.Thread):
|
||||
except:
|
||||
jobstatus.status = False
|
||||
jobstatus.result = sys.exc_info()
|
||||
finally:
|
||||
self.lock.release()
|
||||
|
||||
return jobstatus
|
||||
|
||||
def run(self):
|
||||
@ -146,11 +156,11 @@ class asyncJobMgr(object):
|
||||
|
||||
def waitForComplete(self, workers=10):
|
||||
self.inqueue.join()
|
||||
|
||||
lock = threading.Lock()
|
||||
resultQueue = Queue.Queue()
|
||||
'''intermediate result is stored in self.outqueue'''
|
||||
for i in range(workers):
|
||||
worker = workThread(self.outqueue, resultQueue, self.apiClient, self.db)
|
||||
worker = workThread(self.outqueue, resultQueue, self.apiClient, self.db, lock)
|
||||
worker.start()
|
||||
|
||||
self.outqueue.join()
|
||||
@ -166,9 +176,9 @@ class asyncJobMgr(object):
|
||||
'''put commands into a queue at first, then start workers numbers threads to execute this commands'''
|
||||
def submitCmdsAndWait(self, cmds, workers=10):
|
||||
self.submitCmds(cmds)
|
||||
|
||||
lock = threading.Lock()
|
||||
for i in range(workers):
|
||||
worker = workThread(self.inqueue, self.outqueue, self.apiClient, self.db)
|
||||
worker = workThread(self.inqueue, self.outqueue, self.apiClient, self.db, lock)
|
||||
worker.start()
|
||||
|
||||
return self.waitForComplete(workers)
|
||||
|
||||
@ -114,7 +114,7 @@ class cloudConnection(object):
|
||||
i = i + 1
|
||||
|
||||
if self.logging is not None:
|
||||
self.logging.debug("sending command: " + str(requests))
|
||||
self.logging.debug("sending command: %s %s"%(commandName, str(requests)))
|
||||
result = None
|
||||
if self.auth:
|
||||
result = self.make_request_with_auth(commandName, requests)
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
from cloudstackTestCase import *
|
||||
|
||||
class TestCase1(cloudstackTestCase):
|
||||
|
||||
@unittest.skip("demonstrating skipping")
|
||||
def test_cloudstackapi(self):
|
||||
apiClient = self.testClient.getApiClient()
|
||||
listtmcmd = listTemplates.listTemplatesCmd()
|
||||
|
||||
@ -7,5 +7,18 @@ class TestCase2(cloudstackTestCase):
|
||||
listtmcmd.id = 10
|
||||
listtmcmd.zoneid = 1
|
||||
listtmcmd.templatefilter = "featured"
|
||||
listtmresponse = apiClient.listTemplates(listtmcmd)
|
||||
self.debug(listtmresponse[0].isready)
|
||||
#listtmresponse = apiClient.listTemplates(listtmcmd)
|
||||
#self.debug(listtmresponse[0].isready)
|
||||
|
||||
listhostcmd=listHosts.listHostsCmd()
|
||||
listhostcmd.zoneid=1
|
||||
listhostcmd.type="Routing"
|
||||
|
||||
asyncJobResult=self.testClient.submitCmdsAndWait([listhostcmd],1)
|
||||
listVMresponse = asyncJobResult[0].result
|
||||
self.debug("Total Number of Hosts: " + str(len(listVMresponse)))
|
||||
|
||||
'''
|
||||
for i in listhostresponse:
|
||||
self.debug("id: " + i.id +" pod id: " + i.podid +" host tag: " + i.hosttags)
|
||||
'''
|
||||
@ -25,7 +25,8 @@ class TestCase1(cloudstackTestCase):
|
||||
self.templateId = tmpl.id
|
||||
self.zoneId = tmpl.zoneid
|
||||
break
|
||||
|
||||
|
||||
@unittest.skip("demonstrating skipping")
|
||||
def test_cloudstackapi(self):
|
||||
apiClient = self.testClient.getApiClient()
|
||||
|
||||
@ -73,7 +74,7 @@ class TestCase1(cloudstackTestCase):
|
||||
createvm.zoneid = self.zoneId
|
||||
vmcmds.append(createvm)
|
||||
|
||||
result = self.testClient.submitCmdsAndWait(vmcmds)
|
||||
result = self.testClient.submitCmdsAndWait(vmcmds, 5)
|
||||
for jobstatus in result:
|
||||
if jobstatus.status == 1:
|
||||
self.debug(jobstatus.result.id)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user