如何在Lambda启动任务中设置间隔。

huangapple go评论98阅读模式
英文:

how to set interval in lambda start job

问题

我有一个Lambda函数,用于停止一个运行中的作业并启动一个不同的Glue作业。它的代码如下:

def lambda_handler(event, context):
    client = boto3.client('glue')
    body = json.loads(event['body'])            
    job_id_to_stop = body['job_id_to_stop']     
    job_to_start_argument = body['job_to_start_argument'] 
    client.batch_stop_job_run(                              # 停止作业运行
            JobName='job-to-stop',    
            JobRunIds=[
                job_id_to_stop                 
            ]
    )
    
    # 在启动作业运行之前等待5秒钟
    import time
    time.sleep(5)
    
    client.start_job_run(                                   # 启动作业运行
            JobName = 'job-to-start',    
            Arguments = {
                '--job_to_start_argument':  job_to_start_argument                       
            }
        )
    return  {
        "statusCode": 200,
        "headers": {
            "Content-Type": "application/json"
        },
        "body": "Success"
    }

Lambda函数在运行后会自动终止,所以你不需要让它无限期运行。然而,你可以使用time.sleep(5)来在停止作业运行和启动作业运行之间添加5秒的延迟。这样,Lambda函数会在停止作业后等待5秒,然后再启动新的作业。

英文:

I have lambda that stops a running job and start a different glue job. It goes like this:

def lambda_handler(event, context):
    client = boto3.client('glue')
    body = json.loads(event['body'])            
    job_id_to_stop = body['job_id_to_stop']     
    job_to_start_argument = body['job_to_start_argument'] 
    client.batch_stop_job_run(                              #STOP JOB RUN
            JobName='job-to-stop',    
            JobRunIds=[
                    job_id_to_stop                 
                ]
    )
    
    client.start_job_run(                                   #START JOB RUN
            JobName = 'job-to-start',    
            Arguments = {
                '--job_to_start_argument':  job_to_start_argument                       
            }
        )
    return  {
        "statusCode": 200,
        "headers": {
            "Content-Type": "application/json"
        },
        "body": "Success"
    }

Now I know that lambda dies after running and I don't need it to run indefinitely. However, I want to add a delay between stop_job_run and start_job_run. So after stopping the first glue job, I want lambda to wait at least 5 seconds before running the start_job_run. How do i do that?

答案1

得分: 2

只需在调用之间添加time.sleep(5),并确保 Lambda 的超时时间足够长,以便提交两个请求并等待 5 秒,例如将超时时间设置为 10 秒。

英文:

Just add a time.sleep(5) in between the calls and ensure the lambda has a timeout that is large enough to submit both requests and wait for 5s, e.g. have the timeout be 10 seconds.

答案2

得分: 2

我会在Lambda函数中谨慎使用显式的sleep():Lambda的计费模型基于执行时间,故意暂停执行N秒会不必要地增加成本。

在这种情况下,我会改用Step Function,编排多个Lambda来停止/启动相应的作业(如果我记得正确,您还可以直接在Step Functions中管理Glue作业,但我不知道它是否适合您的用例)。这也会提供更多的自定义、配置和重用空间。

英文:

I would use an explicit sleep() within a Lambda function with great caution: the billing model for Lambda is based on execution time, and deliberately pausing execution for N seconds would unnecessarily increase the cost.

In this scenario, I would instead use a Step Function, orchestrating multiple Lambdas to stop/start the respective jobs (if I recall correctly you can also manage Glue jobs directly in Step Functions, but I don't know if it fits your use case).<br/>This would also provide more room for customization, configuration, and reuse.

huangapple
  • 本文由 发表于 2023年8月10日 15:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定