Product SiteDocumentation Site

4.5. Asynchronous Commands

Asynchronous commands were introduced in CloudStack 2.x. Commands are designated as asynchronous when they can potentially take a long period of time to complete such as creating a snapshot or disk volume. They differ from synchronous commands by the following:

4.5.1. Job Status

The key to using an asynchronous command is the job ID that is returned immediately once the command has been executed. With the job ID, you can periodically check the job status by making calls to queryAsyncJobResult command. The command will return three possible job status integer values:
  • 0 - Job is still in progress. Continue to periodically poll for any status changes.
  • 1 - Job has successfully completed. The job will return any successful response values associated with command that was originally executed.
  • 2 - Job has failed to complete. Please check the "jobresultcode" tag for failure reason code and "jobresult" for the failure reason.

4.5.2. Example

The following shows an example of using an asynchronous command. Assume the API command:
command=deployVirtualMachine&zoneId=1&serviceOfferingId=1&diskOfferingId=1&templateId=1
CloudStack will immediately return a job ID and any other additional data.
         <deployvirtualmachineresponse> 
              <jobid>1</jobid>
             <id>100</id>
         </deployvirtualmachineresponse>
Using the job ID, you can periodically poll for the results by using the queryAsyncJobResult command.
command=queryAsyncJobResult&jobId=1
Three possible results could come from this query.
Job is still pending:
         <queryasyncjobresult> 
              <jobid>1</jobid>
              <jobstatus>0</jobstatus>
              <jobprocstatus>1</jobprocstatus>
         </queryasyncjobresult>
Job has succeeded:
            <queryasyncjobresultresponse cloud-stack-version="3.0.1.6">
                  <jobid>1</jobid>
                  <jobstatus>1</jobstatus>
                  <jobprocstatus>0</jobprocstatus>
                 <jobresultcode>0</jobresultcode>
                  <jobresulttype>object</jobresulttype>
                  <jobresult>
                    <virtualmachine>
                    <id>450</id>
                    <name>i-2-450-VM</name>
                    <displayname>i-2-450-VM</displayname>
                    <account>admin</account>
                    <domainid>1</domainid>
                    <domain>ROOT</domain>
                    <created>2011-03-10T18:20:25-0800</created>
                    <state>Running</state>
                    <haenable>false</haenable>
                    <zoneid>1</zoneid>
                    <zonename>San Jose 1</zonename>
                    <hostid>2</hostid>
                    <hostname>905-13.sjc.lab.vmops.com</hostname>
                    <templateid>1</templateid>
                    <templatename>CentOS 5.3 64bit LAMP</templatename>
                    <templatedisplaytext>CentOS 5.3 64bit LAMP</templatedisplaytext>
                    <passwordenabled>false</passwordenabled>
                    <serviceofferingid>1</serviceofferingid>
                    <serviceofferingname>Small Instance</serviceofferingname>
                    <cpunumber>1</cpunumber>
                    <cpuspeed>500</cpuspeed>
                    <memory>512</memory>
                    <guestosid>12</guestosid>
                    <rootdeviceid>0</rootdeviceid>
                    <rootdevicetype>NetworkFilesystem</rootdevicetype>
                    <nic>
                      <id>561</id>
                      <networkid>205</networkid>
                      <netmask>255.255.255.0</netmask>
                      <gateway>10.1.1.1</gateway>
                      <ipaddress>10.1.1.225</ipaddress>
                      <isolationuri>vlan://295</isolationuri>
                      <broadcasturi>vlan://295</broadcasturi>
                      <traffictype>Guest</traffictype>
                      <type>Virtual</type>
                      <isdefault>true</isdefault>
                    </nic>
                    <hypervisor>XenServer</hypervisor>
                   </virtualmachine>
                 </jobresult>
            </queryasyncjobresultresponse>
Job has failed:
            <queryasyncjobresult>
                  <jobid>1</jobid> 
                  <jobstatus>2</jobstatus> 
                  <jobprocstatus>0</jobprocstatus>
                  <jobresultcode>551</jobresultcode>
                  <jobresulttype>text</jobresulttype>
                  <jobresult>Unable to deploy virtual machine id = 100 due to not enough capacity</jobresult> 
            </queryasyncjobresult>