Boto3:AWS 批处理中没有 logStreamName

Boto3: no logStreamName from AWS batch

提问人:Joymaker 提问时间:10/23/2023 更新时间:10/23/2023 访问量:10

问:

我正在使用 boto3 来启动和监控 AWS 批处理作业。[本文档][1] 建议,一旦作业运行,对该作业的 describe_jobs 的调用应该为我提供一个 logStreamName 字段作为响应中字典的一部分,一旦作业达到 RUNNING 状态,这应该是真的。但是,一旦作业处于 STARTING 状态,它实际上就会出现 - 但是如果我尝试访问该日志流,我会收到“不存在”错误。这是意料之中的吗?

下面是代码,稍作修改以删除专有信息:

import boto3, time

REGION = "us-west-2"
LOG_GROUP = "/aws/batch/job"
client = boto3.client('batch', region_name=REGION)

def startJob():
    response = client.submit_job(
        jobName='kens_boto3_test',  
        jobQueue='blast-queue', 
        jobDefinition='<PROPRIETARY REFERENCE REMOVED>', 
        containerOverrides={
            'environment': [
                {
                    'name': 'FILENAME_LIST_S3_URL',
                    'value': '',
                },
                {
                    'name': 'FILENAME',
                    'value': 'Seq1.fasta',
                },
            ],
        },
    )
    return response


counter = 0

def getUpdate(job):
    global counter
    why = ""
    logName = ""
    
    response2 = client.describe_jobs(jobs=[job])
    jobInfo = response2["jobs"][0]
    name = jobInfo["jobName"]
    id = jobInfo["jobId"]
    status = jobInfo["status"]
    if "statusReason" in jobInfo:
        why = jobInfo["statusReason"]
    attempts = len(jobInfo["attempts"])
    
    container = jobInfo["container"]
    command = container["command"]
    if "logName" in jobInfo:
        logName = container["logStreamName"]
    counter += 1
    print(f'[{counter}] ', name)
    if counter == 1:
        print("command:", command)
        print("id:", id)
    print("status:", status, " :", why)
    # print("attempts:", attempts)
    print("logName:", logName)
    print("")

    if logName:
        do_log_events(LOG_GROUP, logName)
    return status


next_token = None

def do_log_events(log_group_name, log_stream_name, start_time=None):
    global next_token
    client = boto3.client('logs')

    if start_time is None:
        start_time = 0  # Start from the beginning of the log stream
    if next_token:
        response = client.get_log_events(
            logGroupName=log_group_name,
            logStreamName=log_stream_name,
            nextToken=next_token
        )
    else:
        response = client.get_log_events(
            logGroupName=log_group_name,
            logStreamName=log_stream_name,
            startTime=start_time
        )

    if 'nextForwardToken' in response:
        next_token = response['nextForwardToken']
    events = response['events']
    for event in events:
        print(event['message'])
    print(len(events), "events")
    
    return response


def main():
    response = startJob()
    job = response["jobArn"]
    status = ""
    
    while not status in ["SUCCEEDED", "FAILED"]:
        status = getUpdate(job)
        time.sleep(5)

main()
boto3 amazon-cloudwatchlogs aws-batch

评论


答: 暂无答案