如何在特定的“完成”状态下获得工作

我正在尝试确定各种工作状态。Bigquery 提供了我知道的三种状态:DONE、PENDING 和 RUNNING。但是,我正在尝试根据以下内容获取状态:


完毕

待办的

成功的

错误

取消

跑步

我将如何以不太“昂贵”的方式执行此操作,因为我正在以一种“长轮询”方式迭代大约 100 个结果,大约每十秒一次。目前我正在做类似的事情:


jobs = [job for job in self.bq_client.list_jobs(project=PROJECT_ID]

if state is not None:

    jobs = [job for job in jobs if job.state == state]

如果状态为 DONE、RUNNING 或 PENDING 之一,则上述方法有效。但是我将如何覆盖其他州?


有只小跳蛙
浏览 197回答 1
1回答

www说

状态跟踪作业进度,如果你需要成功/失败,你要考虑的信息errorResult中的响应。对于成功的工作,这将是None,对于取消的工作,您将得到{u'reason': u'stopped', u'message': u'Job execution was cancelled: User requested cancellation'}。我用来测试这个的代码:from google.cloud import bigqueryclient = bigquery.Client()project = "[PROJECT-ID]"states = ["RUNNING", "PENDING", "SUCCESSFUL", "CANCELLED", "FAILED"]def returnState(job):  if job.state == "DONE":    if job.error_result is None:      return "SUCCESSFUL"    elif job.error_result['reason'] == u'stopped':      return "CANCELLED"    else:      return "FAILED"  else:    return job.statejobs = [job for job in client.list_jobs(project=project, max_results=10)]for state in states:  matching_jobs = [job for job in jobs if returnState(job) == state]  for job in matching_jobs:    print "Job ID: {0}, State: {1}, Error Result: {2}".format(job.job_id, state, job.error_result)这将打印出如下内容:$ python bq-status.pyJob ID: bquijob_..., State: SUCCESSFUL, Error Result: NoneJob ID: bquijob_..., State: SUCCESSFUL, Error Result: NoneJob ID: job_..., State: SUCCESSFUL, Error Result: NoneJob ID: job_..., State: SUCCESSFUL, Error Result: NoneJob ID: job_..., State: SUCCESSFUL, Error Result: NoneJob ID: job_..., State: SUCCESSFUL, Error Result: NoneJob ID: scheduled_query_..., State: SUCCESSFUL, Error Result: NoneJob ID: bquijob_..., State: SUCCESSFUL, Error Result: NoneJob ID: bquijob_..., State: CANCELLED, Error Result: {u'reason': u'stopped', u'message': u'Job execution was cancelled: User requested cancellation'}Job ID: bquijob_..., State: FAILED, Error Result: {u'reason': u'invalidQuery', u'message': u'Syntax error: Illegal input character "\\\\" at [2:18]', u'location': u'query'}请记住,负载的工作可能是成功的,但允许一些maxBadRecords,这样errorResult将不为空,等等。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java