'Poll Google Cloud Pubsub for the GCB build status.'

@classmethod
def __listen_gcb_build_status(cls, name, subscription, tag, gcb_project, gcb_service_account, logfile):

    start_time = datetime.datetime.now()
    time_elapsed = (datetime.datetime.now() - start_time).seconds
    completed = False
    try:
        while ((not completed) and (time_elapsed < GCB_BUILD_STATUS_TIMEOUT)):
            pulled = subscription.pull()
            for (ack_id, message) in pulled:
                comp_name = ''
                if (name == 'spinnaker-monitoring'):
                    comp_name = 'monitoring-daemon'
                else:
                    comp_name = name
                payload = json.loads(message.data)
                repo_name = payload['source']['repoSource']['repoName']
                tag_name = payload['source']['repoSource']['tagName']
                if ((repo_name == comp_name) and (tag_name == tag)):
                    subscription.acknowledge([ack_id])
                    status = payload['status']
                    print 'Received    status:    {}    for    building    tag    {}    of    {}'.format(status, tag_name, comp_name)
                    if (status in ['SUCCESS', 'FAILURE']):
                        completed = True
                        build_id = payload['id']
                        print 'Retrieving    logs    for    build_id:    {}'.format(build_id)
                        get_log_cmd = 'gcloud    container    builds    log    --project    {project}    --account    {account}    {id}'.format(project=gcb_project, account=gcb_service_account, id=build_id)
                        build_log = run_quick(get_log_cmd, echo=False).stdout.strip()
                        with open(logfile, 'a') as log:
                            log.write('Fetching    GCB    build    logs    with:    {}\n---\n'.format(get_log_cmd))
                            log.write(build_log)
                            log.write('\n---\nFinished    fetching    GCB    build    logs')
                            if (status == 'FAILURE'):
                                raise Exception('Triggered    GCB    build    for    {name}    failed.'.format(name=comp_name))
            time_elapsed = (datetime.datetime.now() - start_time).seconds
        if (time_elapsed >= GCB_BUILD_STATUS_TIMEOUT):
            raise Exception('GCB    triggered    build    for    {}    timed    out'.format(name))
    finally:
        subscription.delete()
