CloudFlow uses Task states to invoke various services. You can control the transition of service invocation states based on service integration modes. This topic describes the common service integration modes: RequestComplete, WaitForSystemCallback, and WaitForCustomCallback.
Precautions
You can use the RequestComplete, WaitForSystemCallback, and WaitForCustomCallback service integration modes to integrate services. However, not all invoked services support these integration modes. For information about the integration modes supported by different services, see Integration modes.
RequestComplete
If you invoke a service of the Task state, the RequestComplete integration mode is used by default. After you send a request to a service, the system waits for the service to return a response before the system proceeds with subsequent tasks.
The following example shows how to use the RequestComplete integration mode to invoke a function of Function Compute. When CloudFlow executes this workflow, CloudFlow invokes the specified function and waits for the result of the function processing. As the invocation is synchronous, the state machine receives the function processing result after the invocation ends.
Type: StateMachine
Name: myWorkFlow
SpecVersion: v1
StartAt: InvokeFunction
States:
- Type: Task
Name: an example of function invocation.
Action: FC:InvokeFunction
TaskMode: RequestComplete
Parameters:
resourceArn: acs:fc:{regionId}:{accountId}:functions/dataji/LATEST
invocationType: Sync
body: xxxx
End: true
WaitForSystemCallback
For specific task-type integration services and asynchronous invocations in Function Compute, you can use the WaitForSystemCallback integration mode. This way, workflows cannot transit to next states until tasks are complete.
The following example shows how to use the WaitForSystemCallback integration mode to invoke a function of Function Compute.
Type: StateMachine
Name: myWorkFlow
SpecVersion: v1
StartAt: InvokeFunction
States:
- Type: Task
Name: an example of function invocation.
Action: FC:InvokeFunction
TaskMode: WaitForSystemCallback
Parameters:
resourceArn: acs:fc:{regionId}:{accountId}:functions/dataji/LATEST
invocationType: Async
body: xxxx
End: true
In the WaitForSystemCallback integration mode, workflows cannot transit to next states until tasks are complete.
If tasks that use the WaitForSystemCallback integration mode are terminated, CloudFlow cannot cancel the tasks. You may be charged unexpected fees for the integration service. To prevent unexpected fees, you must manually cancel the integration tasks. CloudFlow can automatically terminate an integration task in the following cases:
The execution of the workflow is stopped.
Another branch in the Parallel state fails due to a caught error.
An iteration of the Map state fails and the corresponding error is not caught.
In this case, the workflow engine tries its best to cancel the task. For example, if WaitForSystemCallback integration mode is used in a workflow and CloudFlow invokes an asynchronous task that contains child flows or functions of Function Compute, you can call the StopExecution API operation of CloudFlow to stop the workflow. CloudFlow stops the child flows and then the asynchronous task-related functions in the workflow. However, the workflow engine may not be able to cancel the task for the following reasons:
Your execution role has no the permissions to call the corresponding API operation.
The service is temporarily interrupted.
For information about the Alibaba Cloud services that support the WaitForSystemCallback integration mode, see Alibaba Cloud services that support normal integration.
WaitForCustomCallback
The WaitForCustomCallback integration mode is a callback mode that contains a task token. In this mode, you can suspend a workflow and control the execution of the workflow based on the logic that you defined in the integration service. For example, if a user approval is required during the flow execution, the integration invocation initiates the approval flow of an external system. After the approval flow is completed, the system calls back the workflow and returns the approval result. Then, the workflow continues to execute subsequent steps. After an integration invocation that uses the WaitForCustomCallback integration mode is initiated, the current task is automatically suspended. The workflow does not advance until the integrated service calls the ReportTaskSucceeded or ReportTaskFailed API operation to report the execution result of the task.
Type: StateMachine
Name: myWorkFlow
SpecVersion: v1
StartAt: InvokeFunction
States:
- Type: Task
Name: an example of function invocation.
Action: FC:InvokeFunction
TaskMode: WaitForCustomCallback
Parameters:
resourceArn: acs:fc:{regionId}:{accountId}:functions/dataji/LATEST
invocationType: Sync
body: xxxx
End: true
The following figure shows how the WaitForCustomCallback integration mode works. Compared with polling, a callback effectively reduces the delay and unnecessary pressure on the server. In addition, the combination of callbacks and queues can orchestrate tasks in other services than Function Compute and extend the orchestration scope of CloudFlow to all types of computing resources.
Example: service integration workflow that uses the WaitForCustomCallback integration mode
CloudFlow is integrated with an external microservice
This section describes how CloudFlow is integrated with an external microservice to perform credit checks. CloudFlow sends a message that contains a task token to Simple Message Queue (formerly MNS). The external microservice is integrated with Simple Message Queue (formerly MNS), obtains the message from the message queue of Simple Message Queue, and then returns the result and the original task token. Then, CloudFlow continues to execute subsequent flows. The following figure shows the complete workflow.
Example:
Type: StateMachine
Name: myWorkFlow
SpecVersion: v1
StartAt: InvokeFunction
States:
- Type: Task
Name: an example of function invocation.
Action: FC:InvokeFunction
TaskMode: WaitForCustomCallback
Parameters:
resourceArn: acs:fc:{regionId}:{accountId}:functions/dataji/LATEST
invocationType: Async
body:
payload.$: $Input
taskToken.$: $Context.Current.TaskToken
End: true
When you use the WaitForCustomCallback integration mode, you can use the $Context.Current.TaskToken
expression to access the task token in the Parameters
field of the States section. The initial $Context
specifies the path to the context object and obtains the task token of the current task.
Then, the external microservice calls the ReportTaskSucceeded or ReportTaskFailed API operation to send the token and task execution status. The workflow transits to the next state.
CloudFlow is integrated with a self-managed service
The following figure shows how to use the Simple Message Queue (formerly MNS) service and API callbacks to integrate CloudFlow with a self-managed service. This extends the usage of CloudFlow. Simple Message Queue (formerly MNS) can be any intermediate storage service.