Azure批处理作业调度:任务不会反复运行

时间:2018-01-07 06:39:42

标签: 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似乎没有太多文档和示例。

谢谢!任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

您是正确的,JobSchedule将以指定的时间间隔创建新作业。此外,您无法完成任务"重新运行"一旦完成,每5分钟一次。你可以这样做:

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

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

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令牌环境变量添加到作业管理器任务,并具有仅访问当前作业的权限。如果您需要执行其他操作的权限,例如更改池或启动新作业,则可以通过环境变量传递帐户密钥。两个选项如上所示。

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

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()

答案 1 :(得分:0)

job_spec = batchmodels.JobSpecification(
    pool_info=pool_info,
    job_manager_task=batchmodels.JobManagerTask(
        id="JobManagerTask",
        #specify the command that needs to run recurrently
        command_line="/bin/bash -c \" python3 task.py\""
    ))

将要重复运行的任务作为JobManagerTask添加到JobSpecification中,如上所示。现在,此JobManagerTask将循环运行。