[英]Azure Batch Job Scheduling: Task doesn't run recurrently
我的目标是安排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分钟“重新运行”任务。 您可以执行以下任一操作:
我可能会建议使用第二个选项,因为它具有更多的灵活性来监视任务和工作的进度并相应地采取措施。 创建作业的示例客户端可能看起来像这样:
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()
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\""
))
将要循环运行的任务添加为JobSpecification内的JobManagerTask,如上所示。 现在,此JobManagerTask将循环运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.