python - Azure批量作业调度: Task doesn't run recurrently

标签 python azure job-scheduling azure-batch

我的目标是安排一个 Azure 批处理任务从添加之日起每 5 分钟运行一次,并且我使用 Python SDK 来创建/管理我的 Azure 资源。我尝试创建一个 Job-Schedule它会在指定的Pool下自动创建一个新的Job。

    job_spec = batch.models.JobSpecification(
        pool_info=batch.models.PoolInformation(pool_id=pool_id)
    )
    schedule = batch.models.Schedule(
        start_window=datetime.timedelta(hours=1),
        recurrence_interval=datetime.timedelta(minutes=5)
    )
    setup = batch.models.JobScheduleAddParameter(
        'python_test_schedule',
        schedule,
        job_spec
    )
    batch_client.job_schedule.add(setup)

我所做的就是向这个新作业添加一个任务。但该任务似乎在添加后仅运行一次(就像普通任务一样)。我还需要做些什么才能使任务定期运行吗? JobSchedule 的文档和示例似乎也不是很多。

谢谢!如有任何帮助,我们将不胜感激。

最佳答案

您是正确的,JobSchedule 将在指定的时间间隔创建一个新作业。此外,一旦任务完成,您就不能每 5 分钟“重新运行”一次任务。您可以执行以下任一操作:

  • 有一个运行循环的任务,每 5 分钟执行一次相同的操作。
  • 使用作业管理器每 5 分钟添加一个新任务(执行相同的操作)。

我可能会推荐第二个选项,因为它具有更大的灵 active 来监控任务和工作的进度并采取相应的行动。 创建作业的示例客户端可能看起来有点像这样:

job_manager = models.JobManagerTask(
    id='job_manager',
    command_line="/bin/bash -c 'python ./job_manager.py'",
    environment_settings=[
        mdoels.EnvironmentSettings('AZ_BATCH_KEY', AZ_BATCH_KEY)],
    resource_files=[
        models.ResourceFile(blob_sas="https://url/to/job_manager.py", file_name="job_manager.py")],
    authentication_token_settings=models.AuthenticationTokenSettings(
        access=[models.AccessScope.job]),
    kill_job_on_completion=True,  # This will mark the job as complete once the Job Manager has finished.
    run_exclusive=False)  # Whether the job manager needs a dedicated VM - this will depend on the nature of the other tasks running on the VM.


new_job = models.JobAddParameter(
    id='my_job',
    job_manager_task=job_manager,
    pool_info=models.PoolInformation(pool_id='my_pool'))

batch_client.job.add(new_job)

现在我们需要一个脚本作为计算节点上的作业管理器运行。在本例中,我将使用 Python,因此您需要将 StartTask 添加到池中(或将 JobPrepTask 添加到作业中)来安装 azure-batch Python 包。

此外,作业管理器任务需要能够针对 Batch API 进行身份验证。根据作业管理器将执行的事件范围,有两种方法可以执行此操作。如果您只需要添加任务,则可以使用authentication_token_settings属性,该属性会将AAD token 环境变量添加到作业管理器任务,并具有仅访问当前作业的权限。如果您需要执行其他操作的权限,例如更改池或开始新作业,您可以通过环境变量传递帐户 key 。上面显示了两个选项。

您在作业管理器任务上运行的脚本可能如下所示:

import os
import time

from azure.batch import BatchServiceClient
from azure.batch.batch_auth import SharedKeyCredentials
from azure.batch import models

# Batch account credentials
AZ_BATCH_ACCOUNT = os.environ['AZ_BATCH_ACCOUNT_NAME']
AZ_BATCH_KEY = os.environ['AZ_BATCH_KEY']
AZ_BATCH_ENDPOINT = os.environ['AZ_BATCH_ENDPOINT']

# If you're using the authentication_token_settings for authentication
# you can use the AAD token in the environment variable AZ_BATCH_AUTHENTICATION_TOKEN.


def main():
    # Batch Client
    creds = SharedKeyCredentials(AZ_BATCH_ACCOUNT, AZ_BATCH_KEY)
    batch_client = BatchServiceClient(creds, base_url=AZ_BATCH_ENDPOINT)

    # You can set up the conditions under which your Job Manager will continue to add tasks here.
    # It could be a timeout, max number of tasks, or you could monitor tasks to act on task status
    condition = True
    task_id = 0
    task_params = {
        "command_line": "/bin/bash -c 'echo hello world'",
        # Any other task parameters go here.
    }

    while condition:
        new_task = models.TaskAddParameter(id=task_id, **task_params)
        batch_client.task.add(AZ_JOB, new_task)
        task_id += 1
        # Perform any additional log here - for example:
        # - Check the status of the tasks, e.g. stdout, exit code etc
        # - Process any output files for the tasks
        # - Delete any completed tasks
        # - Error handling for tasks that have failed
        time.sleep(300)  # Wait for 5 minutes (300 seconds)

    # Job Manager task has completed - it will now exit and the job will be marked as complete.

if __name__ == '__main__':
    main()

关于python - Azure批量作业调度: Task doesn't run recurrently,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48134928/

相关文章:

azure - Terraform 中的本地人

azure - 将音轨添加到现有 Assets (通过 Azure 媒体服务)

job-scheduling - AMPL 中的护士排类模型制定

python - 清理 Twitter 数据 pandas python

python - matplotlib : How to display graphs or figures with pylab

python : How to "merge" two class

azure - 如何在Azure门户中登录到不同租户(非默认)?

python - jupyter 按顺序打印图和数据框,for 循环

c# - Quartz.net 中的 'DisallowConcurrentExecution' 到底是什么意思

apache-spark - 失败的数据 block 作业如何从原来的位置继续下去?