This topic describes task steps and related examples.

Overview

A task step defines the function call information of Function Compute. When a task step is executed, the corresponding function is called.

A task step contains the following attributes:

  • type: Required. The step type. The value task indicates that the step is a task step.
  • name: Required. The step name.
  • resourceArn: Required. The resource identifier, which can be set to a function, MNS queue, or Serverless workflow flow, for example, acs:fc:cn-shanghai:123456789:services/fnf_test/functions/hello. For more information, see Integration overview.
  • pattern: Optional. The execution mode of an integration service. Different integration services support different execution modes. Default value: requestResponse. Valid values:
    • requestResponse: The system synchronously waits until task execution ends and then continues with this step.
    • sync: The system waits until task execution ends after a task is asynchronously submitted, and then continues with the step after it receives the task execution result.
    • waitForCallback: The system pauses this step after a task is asynchronously submitted (such as calling a function), and waits until it receives a callback request or timeout notification of the task.
  • timeoutSeconds: Optional. The timeout period of a task. If the task execution duration exceeds the specified timeout period, the task step times out.
  • end: Optional. Specifies whether to proceed with the subsequent steps after the current step ends.
  • inputMappings: Optional. The input mappings. The input of a task step is used as an function call event (InvokeFunction).
  • outputMappings: Optional. The output mappings. The $local is the result of a function call and must be in JSON object format.
    Note If no output mappings are specified, $local is used as the output of this step by default.
  • retry: a group of retry policies, in which each retry policy has the following attributes:
    • errors: one or more errors. For more information, see the following table.
    • intervalSeconds: the initial interval between retries. The maximum value is 86,400 seconds. Default value: 1 second.
    • maxIntervalSeconds: the maximum interval between retries. Both the maximum value and default value are 86,400 seconds.
    • maxAttempts: the maximum number of retries. Default value: 3.
    • multiplier: the value by which a retry interval is multiplied to make a next retry interval. Default value: 2.
  • catch: a group of catch policies, in which each catch policy has the following attributes:
    • errors: one or more errors. For more information, see the following table.
    • goto: the name of the target step.
      Note The target step must be a step parallel to the current task step.
Table 1. Error definition
Function execution status HTTP code of a Function Compute response Function Compute response Serverless workflow step failure (for retry and catch) Need retry
Not executed 429 ResourceExhausted FC.ResourceExhausted Yes
Not executed 4xx but not 429 ServiceNotFound, FunctionNotFound, or InvalidArgument FC.ServiceNotFound, FC.FunctionNotFound, or FC.InvalidArgument No
Uncertain 500 InternalServerError FC.InternalServerError Yes
Not executed 503 ResourceThrottled FC.ResourceThrottled Yes
Execution successful, with an error code returned 200 A custom error, including errorType errorType Determined based on businesses
Execution failed, with an error code returned 200 No errorType FC.Unknown Yes
Execution successful, with a non-JSON object returned 200 No errorType FC.InvalidOutput No

Examples

  • Simple task steps

    The following sample flow contains a task step.

    • If the input of the flow is {"name": "function flow"}, the output is {"hello": "function flow"}.
    • If no input is specified for the flow or the flow input does not contain the name key, the task step execution fails, causing a flow failure.
    • Flow definition
      version: v1
      type: flow
      steps:
        - type: task
          name: hello
          resourceArn: acs:fc:{region}:{accountID}:services/fnf_test/functions/hello           
      Note You need to replace resourceArn with the resource identifier of the corresponding function under your account. The following content introduces the code of the function:
    • Function definition
      import json
      
      class MyError(Exception):
        pass
      
      def handle(event, context):
        evt = json.loads(event)
        if "name" in evt:
          return {
            "hello": evt["name"]
          }
        else:
          raise MyError("My unhandled exception")          
  • Retry

    The following example shows how to retry a task upon MyError. If no input is specified for the flow or the flow input does not contain the name key, the Serverless workflow service retries to call failed tasks multiple times based on retry policies.

    • It waits 3 seconds after the first error occurs, and then calls the function again.
    • It waits 6 seconds (intervalSeconds x multiplier) after the second error occurs, and then calls the function again.
    • It waits 12 seconds (intervalSeconds x multiplier x multiplier) after the third error occurs, and then calls the function again.
    • If an error occurs after three retries, the number of retries exceeds maxAttempts. Therefore, the task step fails and the flow ends due to failures.
    version: v1
    type: flow
    steps:
      - type: task
        name: hello
        resourceArn: acs:fc:{region}:{accountID}:services/fnf_test/functions/hello
        retry:
          - errors:
            - MyError
            intervalSeconds: 3
            maxAttempts: 3
            multiplier: 2            
  • Error catch

    The following example shows how to catch MyError and then jump to the final step. The error is caught, so the flow ends successfully.

    version: v1
    type: flow
    steps:
      - type: task
        name: hello
        resourceArn: acs:fc:{region}:{accountID}:services/fnf_test/functions/hello
        catch:
          - errors:
            - MyError
            goto: final
      - type: pass
        name: pass1
      - type: pass
        name: final