我正在用 Python 为 Azure Batch 编写任务管理器。当我运行管理器并将作业添加到指定的 Azure Batch 帐户时,我会:
- 检查指定的job id是否已经存在
- 如果是,删除作业
- 创造工作
不幸的是,我在第 2 步和第 3 步之间失败了。这是因为,即使我为指定的作业发出删除命令并检查 Azure Batch 帐户中是否没有具有相同 ID 的作业,当我收到如下所示的 BatchErrorException尝试再次创建作业:
Exception encountered:
The specified job has been marked for deletion and is being garbage collected.
我用来删除作业的代码如下:
def deleteJob(self, jobId):
print("Delete job [{}]".format(jobId))
self.__batchClient.job.delete(jobId)
# Wait until the job is deleted
# 10 minutes timeout for the operation to succeed
timeout = datetime.timedelta(minutes=10)
timeout_expiration = datetime.datetime.now() + timeout
while True:
try:
# As long as we can retrieve data related to the job, it means it is still deleting
self.__batchClient.job.get(jobId)
except batchmodels.BatchErrorException:
print("Job {jobId} deleted correctly.".format(
jobId = jobId
))
break
time.sleep(2)
if datetime.datetime.now() > timeout_expiration:
raise RuntimeError("ERROR: couldn't delete job [{jobId}] within timeout period of {timeout}.".format(
jobId = jobId
, timeout = timeout
))
我尝试检查 Azure SDK,但找不到可以准确告诉我作业何时被完全删除的方法。