[ovirt-users] Tracking asynchronous tasks results in Python SDK

Juan Hernández jhernand at redhat.com
Tue Mar 8 09:15:31 UTC 2016


On 03/08/2016 09:45 AM, Sandro Bonazzola wrote:
> 
> 
> On Sat, Mar 5, 2016 at 8:29 PM, James Michels
> <karma.sometimes.hurts at gmail.com
> <mailto:karma.sometimes.hurts at gmail.com>> wrote:
> 
>     Greetings
> 
>     How can I track asynchronous tasks with python sdk? For instance, I
>     want to copy a disk so I use:
> 
>        dcdisk = api.disks.get(id='...')
>        act =
>     params.Action(storage_domain=api.storagedomains.get(name='...'))
>        action = dcdisk.copy(act)
> 
>     I get a params.Action object. Now how can I know which ID has the
>     new copied disk?
> 

In general you can track async tasks using the "job" contained in the
returned action. For example:

  action = dcdisk.copy(act)
  job = action.get_job()
  if job is not None:
    job_id = job.get_id()
    while True:
      job = api.jobs.get(id=job_id)
      if job is not None and job.get_status().get_state() != 'FINISHED':
        break
      time.sleep(5)

However, this isn't reliable, as actions may not return jobs, that
depends on how they are implemented in the backend, and that is outside
of the control of the API. So I don't recommend this approach.

But in your particular case this won't help, as it will tell you when
the disk is created, but not what is the id of the disk. To be able to
get the id of the disk I recommend that you set an unique alias when
creating it:

  copy_alias = "copy-of-%s" % dcdisk.get_id()
  action = params.Action(
    disk=params.Disk(
      alias=copy_alias,
    ),
    ...
  )
  dcdisk.copy(action)

Then, later, you can search that disk by alias:

  copy_disk = api.disks.list(query="alias=%s" % copy_alias)

A bug was opened recently related to this:

  [RFE] Add support to search VMs by disk id
  https://bugzilla.redhat.com/1315345

That bug is about searching for the VMs that have a disk attached. You
may want to open another bug requesting that the identifier of the
copied disk is returned directly by the "copy" operation.

-- 
Dirección Comercial: C/Jose Bardasano Baos, 9, Edif. Gorbea 3, planta
3ºD, 28016 Madrid, Spain
Inscrita en el Reg. Mercantil de Madrid – C.I.F. B82657941 - Red Hat S.L.



More information about the Users mailing list