3.5.5.2 Python

Track Job Status

To track job status in Python, we can repeatedly query the API using the job ID returned in each response. To keep our script polite, we use the time library to sleep between requests. Since this is functionality that is required for a variety of tasks it is better to define it as a function:

import time
...
def wait_for_job(client, job_id):
        while True:
            time.sleep(1)
            job = client.service.jobGetById(job.id)
            if job.summaryDone:
                if job.jobRunState == 'FAILURE':
                    raise Exception('Job failed: %s' % job.error)
                elif job.jobRunState == 'SUCCESS':
                    if 'resultId' in job:
                        return job.resultId
                    break
                elif job.jobRunState == 'RUNNING':
                    continue
                else:
                    break

A completed job that consists of a create style action also includes the ID of the object that has been created. We check the summaryDone property of the job to determine whether the job is complete, as this provides a more complete view of the status of any spawned child jobs as opposed to the done property. See Section 5.1.4, “Working with Jobs” for more information on this. For these types of activities, it is useful to return this value after the job is complete, so that it is easier to perform subsequent tasks related to the same object. Only return this value if the job was successful and no error was returned.