You can use Task states to call API operations of integrated services and complete complex workflows. This topic describes Task states and provides related usage examples of Task states. The usage examples can help you use Task states to call API operations of integrated services.
Overview
Task states are used to call API operations of integrated services to complete a job. You can use task states to invoke functions, API operations of Alibaba Cloud services, or third-party services over a common protocol such as HTTP or HTTPS. In the flow definition language (FDL), you can mark the current state as Task to indicate a Task state.
The following table describes the parameters involved in Task states.
Parameter | Type | Required | Description | Example |
Name | string | Yes | The name of the state. | my-state-name |
Description | string | No | The description of the state. | describe it here |
Type | string | Yes | The type of the state. | Task |
Action | string | Yes | The action that is to be executed in the current task. The action is usually the name of an API operation of the service, or the name of an action of the integrated service. The format of the value: <Service name>:<API operation name>. | FC:InvokeFunction |
TaskMode | enum | No | The mode in which the task is invoked. CloudFlow provides three modes. For more information, see Service integration modes.
| WaitForCustomCallback |
InputConstructor | map[string]any | No | The input constructor. | See the InputConstructor section of the Inputs and outputs topic. |
Parameters | map[string]any | No | The specifications for the parameters that are requested to invoke. The specifications correspond to the action. The values of the parameters can be constants or follow the JSONPath format. | For more information about the specifications, see Overview. |
Timeout | string | No | The timeout period of the invocation. Unit: seconds. Valid values: 0 - 604800. A value of 0 indicates that the task never times out. | 30 |
You can use an expression to dynamically construct the parameter based on the context. | Timeout.$: $Input.timeout | |||
Retry | map[string]any | No | The error retry policy. | See Error handling. |
Catch | map[string]any | No | The error capture policy. | See Error handling. |
OutputConstructor | map[string]any | No | The output constructor. | For more information, see the OutputConstructor section of the Inputs and outputs topic. |
Next | string | No | The next state that is executed after the current state is complete. If the value of the End parameter is true, leave this parameter empty. | my-next-state |
End | bool | No | Specifies whether the state is the terminal state of the current scope. | true |
Sample requests
Invoke Function Compute
You can declare that a state
is of the Task
type and specify the information about the function to be invoked in the Parameters
section based on the specifications for invoked Function Compute parameters. For more information about the parameter specifications, see Step 2: Configure invocation parameters. Functions can be invoked in optimized integration mode or in Alibaba Cloud OpenAPI.
Type: StateMachine
Name: MyWorkflow
SpecVersion: v1
StartAt: an example of synchronous function invocation
States:
- Name: an example of synchronous function invocation
Type: Task
TaskMode: RequestComplete
Action: FC:InvokeFunction
Parameters:
resourceArn: acs:fc:::functions/myFunction1/LATEST
invocationType: Sync
body:
key: name
Next: an example of asynchronous function invocation
- Type: Task
Name: an example of asynchronous function invocation
Action: FC:InvokeFunction
TaskMode: RequestComplete
Parameters:
invocationType: Async
resourceArn: acs:fc:{region}:{accountID}:functions/myFunction/LATEST
body:
key: name
Next: SucceededExit
- Type: Pass
Name: SucceededExit
End: true
Call an API operation of an Alibaba Cloud service
Call the corresponding API operations based on the Alibaba Cloud OpenAPI specifications. The following sample code shows how to perform the ECS:DescribeInstances action.
Type: StateMachine
SpecVersion: v1
Name: MyWorkflow
StartAt: DescribeInstances
States:
- Type: Task
Name: DescribeInstances
Action: ECS:DescribeInstances
TaskMode: RequestComplete
Parameters:
RegionId: cn-hangzhou
VpcId: vpc-bp11y195luy47h8****
VSwitchId: vsw-bp1wb297ekw7xyh****
End: true
Invoke a service that is not provided by Alibaba Cloud and configure task timeout and catch
In the following example, an HTTP service is invoked by using the WaitForCustomCallback integration mode. The timeout period of the task is 1 hour. If the task fails to complete within 1 hour, the workflow throws an error, which is caught by the rules specified in the Catch field.
Type: StateMachine
Name: MyWorkflow
SpecVersion: v1
StartAt: SubmitTask
States:
- Type: Task
Name: SubmitTask
Action: HTTP:Request
TaskMode: RequestComplete
Timeout.$: $Input.timeout
Parameters:
method: POST
url: https://*****
body:
taskName: fly
headers:
Content-Type: multipart/form-data
Catch:
- Errors:
- FnF.TaskTimeout
Description: 'Error capture rule #1'
Next: NotifyOnTaskTimeout
OutputConstructor:
ErrorCode: TaskTimeout
ErrorMessage: Task timed out.
End: true
- Type: Task
Name: NotifyOnTaskTimeout
Action: HTTP:Request
TaskMode: RequestComplete
Parameters:
method: POST
url: https://******
body:
taskName: fly
errorCode.$: $Input.ErrorCode
errorMessage.$: $Input.ErrorMessage
headers:
Content-Type: multipart/form-data
End: true
The task timeout period is obtained from the execution input. Example of an execution input:
{
"timeout": 10
}
If the task fails to complete within the specified time, a timeout error is caught. The workflow proceeds to the NotifyOnTaskTimeout node and sends a notification that the task has timed out.