python-3.xtableau-api

How to add poll/wait into python script for refreshing a tableau workbook extract?


I have written a python script to call the Tableau Rest API's and I have it working properly. It prompts the user to enter their login credentials, select the server, select the site, select the workbook and then finds all the necessary keys and submits an extract refresh request based on the workbook id. The server.workbooks.refresh method kicks off an extract refresh job on Tableau Server and I get the jobID back from the method return.

What I want to do next is build a process in the script to poll the Tableau Server with that jobID looking for the completion time and status, then post a message that the refresh is complete and close the script.

I looked up some youtube videos of how to do polling/waiting in python but I'm not sure how those methods can apply using the tableau server. I was hoping that someone has a snippet of code to do this polling related to a tableau server process?

with server.auth.sign_in(tableau_auth):
    get_sites(server)
    print(selected_site_id)

    #tab_site = selected_site_id

    # Get all projects on site
    get_projects(server)

    if input_resource_type.lower() == 'workbook':
        # Get a list of workbooks
        get_workbooks(server)

    results = server.workbooks.refresh(selected_workbook_id)

    print("refresh results: " + str(results))

I want the python script to keep running until the task associated to the JobID on Tableau Server is complete and has a successful status code.

Currently the code just kicks the refresh extract task off in Server and returns the JobID.


Solution

  • Just to follow-up on how to resolve this for anyone else trying to do this. As jdigital said, this works pretty well... I added some comments into the code so the user is notified as the process is running:

            jobid = results.id
    
            complete_time = None
            counter = 0
            started = False
            sleep_timer = 5
            while complete_time is None:
                extract = server.jobs.get_by_id(jobid)
    
                if str(extract.finish_code) == "0":
                    complete_time = str(extract.completed_at)
                else:
                    if extract.started_at is None:
                        print("refresh has not started")
                    else:
                        if started == False:
                            print("refresh started at " + str(extract.started_at))
                            started = True
    
                        if counter == 0:
                            print("refresh is running....")
                        else:
                            print("refresh is still running.....")
    
                        counter += 1
    
                if complete_time is None:
                    time.sleep(sleep_timer)
    
            refresh_time = counter * sleep_timer
            print("############################################")
            print(" Refreshed the " + str(workbook.name) + " workbook \n")
            print(" Extract refresh started at " + str(extract.started_at) + "\n")
            print(" Extract refresh completed at " + str(complete_time) + "\n")
            print(" Extract took approximately " + str(refresh_time) + " seconds to refresh")
            print("############################################")