简体   繁体   English

步骤 function lambda 通常在不同的时间间隔执行不同的任务

[英]Step function lambda that executes different tasks generically and on different intervals

So I am writing a Lambda that is part of a step function, this lambda is getting a map of task name and on what interval to execute it, for example -("task1",5) --> task1 will be executed every 5 minutes. So I am writing a Lambda that is part of a step function, this lambda is getting a map of task name and on what interval to execute it, for example -("task1",5) --> task1 will be executed every 5分钟。 This lambda function is within a loop condition in the step machine and could potentially be looping for 24 hours.此 lambda function 处于步进机的循环条件内,并且可能循环 24 小时。 Upon each iteration of the step function loop, it waits for 5 minutes.在步骤 function 循环的每次迭代中,它等待 5 分钟。 This means that 5 minutes is my execution iteration.这意味着 5 分钟是我的执行迭代。 In such case we understand that "task1" should actually be executed on every iteration.在这种情况下,我们知道“task1”实际上应该在每次迭代中执行。 In my below example I also have ("task2",60) - this task should be executed every 12 iterations.在下面的示例中,我也有 ("task2",60) - 此任务应每 12 次迭代执行一次。 My Lambda code at the moment looks like:目前我的 Lambda 代码如下所示:

protected Map<String, Object> customHandleLambdaRequest(Map<String, Object> inputMap, Context context) throws Exception {

    Map<String, Object> tasks = (Map<String, Object>) inputMap.get("tasks");
    if (tasks.isEmpty()) {
        return inputMap;
    }

    int tick = (int) inputMap.get("tick");
    tick = tick + 5;
    inputMap.put("tick", tick);

    if (tasks.containsKey("task1") || isEvenlyDividable((int) tasks.get("task1"),tick)) {
        //do something
    }

    if (tasks.containsKey("task2") || isEvenlyDividable((int) tasks.get("task2"),tick)) {
        //do something
    }

    return inputMap;
}

The thing is, I keep on adding many tasks and end up having a code that looks like one big "if" (ugly).问题是,我不断添加许多任务,最终得到一个看起来像一个大“如果”(丑陋)的代码。 My question for the stack overflow code wizards are:我对堆栈溢出代码向导的问题是:

  1. how can I make this code more generic and not have all these if conditions?我怎样才能使这段代码更通用,并且没有所有这些 if 条件?
  2. I would like to adhere to my interval time accurately and executing all the tasks might take a few seconds which in turn will cause my next iteration to be a few seconds late, would you suggest that I sample the first iteration time and keep that as my five minutes compass?我想准确地遵守我的间隔时间,执行所有任务可能需要几秒钟,这反过来会导致我的下一次迭代延迟几秒钟,你是否建议我对第一次迭代时间进行采样并将其作为我的五分钟指南针?

Make additional lambdas.制作额外的 lambda。

I know that sounds contrite, but it is the best practice accepted behavior.我知道这听起来很遗憾,但这是公认的最佳做法。

Each lambda should be a self contained task only called when needed.每个 lambda 应该是一个独立的任务,仅在需要时调用。 If you have a lot of different scenarios, then make additional lambdas and put a choice Task in your State Machine to determine which lambda to call.如果您有很多不同的场景,则制作额外的 lambda 表达式并在您的 State 机器中选择 Task 以确定要调用哪个 lambda。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM