【问题标题】:Job deletion and recreation in Azure Batch raises BatchErrorExceptionAzure Batch 中的作业删除和重新创建引发 BatchErrorException
【发布时间】:2017-08-24 10:31:06
【问题描述】:

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

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

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

【问题讨论】:

    标签: python azure azure-batch


    【解决方案1】:

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

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

    【讨论】:

    • 我想我必须在 Azure API 上打开一个错误/功能请求。
    【解决方案2】:

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

    我建议您可以在步骤3中添加check来创建作业,确保在创建之前没有在帐户中找到具有相同id的作业。

    由于您没有提供创建工作的代码,您可以参考下面代码的sn-p来创建工作:

    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'
    

    希望对你有帮助。

    【讨论】:

    • 不幸的是,这个解决方案和我的一样。它仍然依赖于命令batch_client.job.get(jobId),它只是检查一个作业是否在任务中,而不是它的整体存在(因此在尝试将它再次添加到同一任务时出现异常)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多