Azure Batch 中的作业删除和重新创建会引发批处理错误异常



我正在用Python为Azure Batch编写任务管理器。 运行管理器并将作业添加到指定的 Azure Batch 帐户时,我执行以下操作:

  1. 检查指定的作业 ID 是否已存在
  2. 如果是,请删除作业
  3. 创建作业

不幸的是,我在步骤 2 和 3 之间失败了。这是因为,即使我为指定的作业发出删除命令并检查 Azure 批处理帐户中没有具有相同 ID 的作业,当我尝试再次创建作业时,我也会收到如下所示的批处理错误异常:

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,但找不到一种方法可以准确告诉我作业何时被完全删除。

查询作业是否存在是确定作业是否已从系统中删除的唯一方法。

或者,如果您不需要严格地再次重用相同的作业 ID,则可以发出删除作业,然后创建具有不同 ID 的作业。这将允许作业从关键路径中异步删除。

根据您提供的异常日志信息,我认为这是因为删除作业可能会消耗一定的时间,并且在此期间您无法创建相同的作业 ID。

我建议您在步骤 3 中添加签入以创建作业,确保在创建作业之前未在帐户中找到具有相同 ID 的作业。

您可以参考下面的代码片段来创建作业,因为您没有提供创建作业的代码:

import azure.batch.batch_service_client as batch
import azure.batch.batch_auth as batchauth
import azure.batch.models as batchmodels
credentials = batchauth.SharedKeyCredentials(ACCOUNT_NAME,
ACCOUNT_KEY)
batch_client = batch.BatchServiceClient(
credentials,
base_url=ACCOUNT_URL)

def createJob(jobId):
while (batch_client.job.get(jobId)):
print 'job still exists,can not be created'
else:
# Create Job
job = batchmodels.JobAddParameter(
jobId,
batchmodels.PoolInformation(pool_id='mypool')
)
batch_client.job.add(job)
print 'create success'

希望对您有所帮助。

最新更新