0

AWS Step Function

My problem is to how to sendTaskSuccess or sendTaskFailuer to Activity which are running under the state machine in AWS .

My Actual intent is to Notify the specific activities which belongs to particular State machine execution.

I successfully send notification to all waiting activities by activityARN. But my actual need is to send notification to specific activity which belong to particular state machine execution .

Example . StateMachine - SM1 There two execution on going for SM1-- SM1E1, SM1E2 . In that case I want to sendTaskSuccess to activity which belongs to SM1E1 .

follwoing code i used . But it send notification to all activities

        GetActivityTaskResult getActivityTaskResult = client.getActivityTask(new GetActivityTaskRequest()
                .withActivityArn("arn detail"));

        if (getActivityTaskResult.getTaskToken() != null) {
            try {
                JsonNode json = Jackson.jsonNodeOf(getActivityTaskResult.getInput());

                    String outputResult = patientRegistrationActivity.setStatus(json.get("patientId").textValue());
                    System.out.println("outputResult " + outputResult);
                    SendTaskSuccessRequest sendTaskRequest = new SendTaskSuccessRequest().withOutput(outputResult)
                            .withTaskToken(getActivityTaskResult.getTaskToken());
                    client.sendTaskSuccess(sendTaskRequest);


            } catch (Exception e) {
                client.sendTaskFailure(
                        new SendTaskFailureRequest().withTaskToken(getActivityTaskResult.getTaskToken()));
            }
Nilay Tiwari
  • 492
  • 5
  • 16
  • I answered a similar question in here https://stackoverflow.com/questions/55979328/aws-getactivitytask-beloging-from-the-same-state-machine-execution/58440488#58440488 – VenVig Oct 17 '19 at 20:46

2 Answers2

0

As far as I know you have no control over which task token is returned. You may get one for SM1E1 or SM1E2 and you cannot tell by looking at the task token. GetActivityTask returns "input" so based on that you may be able to tell which execution you are dealing with but if you get a token you are not interested in, I don't think there's a way to put it back so you won't be able to get it again with GetActivityTask later. I guess you could store it in a database somewhere for use later.

One idea you can try is to use the new callback integration pattern. You can specify the Payload parameter in the state definition to include the task token like this token.$: "$$.Task.Token" and then use GetExecutionHistory to find the TaskScheduled state of the execution you are interested in and retrieve the parameters.Payload.token value and then use that with sendTaskSuccess.

Here's a snippet of my serverless.yml file that describes the state

WaitForUserInput: #Wait for the user to do something
    Type: Task
    Resource: arn:aws:states:::lambda:invoke.waitForTaskToken
    Parameters:
        FunctionName:
        Fn::GetAtt: [WaitForUserInputLambdaFunction, Arn]
        Payload:
        token.$: "$$.Task.Token"
        executionArn.$: "$$.Execution.Id"
    Next: DoSomethingElse
Ilya Y
  • 236
  • 2
  • 6
  • I didn’t get it completely . Can you share some example snnipet here. Just to add on what i did yet to get that . But that not worked . I get input json and based on the json attribute I put if conditions but that also not make any impact . – Nilay Tiwari Sep 07 '19 at 11:30
0

I did a POC to check and below is the solution .

if token is consumed by getActivityTaskResult.getTaskToken() and if your conditions not satisfied by request input then you can use below line to avoid token consumption .awsStepFunctionClient.sendTaskHeartbeat(new SendTaskHeartbeatRequest().withTaskToken(taskToken))

Nilay Tiwari
  • 492
  • 5
  • 16