Azure Batch中的作业删除和重新创建会引发BatchErrorException

时间:2017-08-24 10:31:06

标签: python azure azure-batch

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

  1. 检查指定的作业ID是否已存在
  2. 如果是,请删除作业
  3. 创建工作
  4. 不幸的是,我在第2步和第3步之间失败了。这是因为,即使我为指定的作业发出删除命令并检查Azure批处理帐户中没有具有相同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,但找不到能够准确告诉我作业完全删除的方法。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

根据您提供的异常日志信息,我认为是因为删除作业可能会占用一定的时间而且您在此期间无法创建相同的作业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'

希望它对你有所帮助。