Serverless workflow provides the service integration feature to simplify the interaction between users and cloud services. In this topic, the Message Service (MNS) queues are used with callback to orchestrate tasks that do not involve functions in Function Compute.
Overview
Serverless workflow not only allows you to orchestrate functions that are deployed in Function Compute in Function as a Service (FaaS) mode into flows, but also allows you to orchestrate other computing tasks into flows. The topic Perform callbacks on asynchronous tasks under Best Practices describes how to use functions in Function Compute to send messages to MNS queues. In custom environments, after a task executor (worker) receives a message, it notifies Serverless workflow of the task execution result based on the callback. This topic describes how to use MNS queues, a new feature of Serverless workflow. MNS queues further simplify the orchestration of custom task types. Serverless workflow allows you to directly send messages to MNS queues. In this way, you do not need to develop, test, and maintain the function that is deployed in Function Compute for sending the messages, improving the availability and reducing the latency. Compared with sending messages to MNS topics by using functions in Function Compute, using the integrated MNS service to send messages to specified MNS queues has the following benefits:
- You do not need to develop a function in Function Compute to send messages. This reduces the cost of development, testing, and maintenance.
- The message delivery delay is reduced, a remote access process is eliminated, and the cold start of Function Compute is avoided.
- Service dependency is removed and fault tolerance is improved.
Serverless workflow will support more cloud services in the future to make it easier to orchestrate flows that consist of different types of tasks.
Service integration
In the following figure, the three serial tasks are sent by Serverless workflow to the specified MNS queue in sequence. After the messages are sent, Serverless workflow waits for the callback in this step. You can call the ReceiveMessage
operation of MNS to pull messages in the worker in a custom environment, such as
an Elastic Compute Service (ECS) instance, a container, or a server in an on-premises
data center. After the worker receives the messages, it executes the corresponding
task based on the message content. After the task ends, the worker calls the ReportTaskSucceeded/Failed
operation of Serverless workflow. Serverless workflow continues the step after receiving the task result. After the worker reports the
success result, the message is deleted from the MNS queue.

Procedure
Perform the following step to use this feature:
Step 1: Prepare for using this feature
- In the MNS console, create an MNS queue. For more information, see Create a queue.
- Serverless workflow assumes the Create execution roles (the role of the RAM user) that you specify in the flow to send messages to the MNS
queue in your Alibaba Cloud account. Therefore, you must add MNS SendMessage policies for the role of the RAM user. The following example shows a fine-grained policy. If you do not need the fine-grained
policy, you can log on to the Serverless workflow console, and add
AliyunMNSFullAccess
in System Policy to Flow RAM Role.
{
"Statement": [
{
"Effect": "Allow",
"Action": [
"mns:SendMessage"
],
"Resource": [
"acs:mns:$region:$account_id:/queues/$queue_name/messages"
]
}
],
"Version": "1"
}
Step 2: Define a flow
The following code in Flow Definition Language (FDL) defines a task step that can send messages to the MNS queue named fnf-demo and wait for the callback.
version: v1
type: flow
steps:
- type: task
name: Task_1
resourceArn: acs:mns:::/queues/fnf-demo/messages # This task step sends messages to the MNS queue fnf-demo that is under the same account in the same region.
pattern: waitForCallback # The task step suspends after the message is sent to the MNS queue and waits until it receives the callback.
inputMappings:
- target: task_token
source: $context.task.token # Serverless Workflow queries the task token from the context object.
- target: key
source: value
serviceParams: # The service integration parameters.
MessageBody: $ # The mapped input is used as the body of the message you want to send.
Priority: 1 # The priority of the MNS queue.
Step 3: Define a worker
The following Python 2.7 code simulates a worker that executes a task. It can run
in any environment that can access Serverless workflow and MNS. The worker calls the MNS ReceiveMessage
operation for long polling. When it enters a task step with an MNS configuration,
Serverless workflow sends a message to the fnf-demo
queue. After the worker executes the task, it calls back the ReportTaskSucceeded/Failed
operation of Serverless workflow. After Serverless workflow receives the task execution result, it continues the current task step. The worker
deletes the message from the queue.
Step 4: Execute the flow and view the result
In the Serverless workflow console, execute the flow and run the worker. The result shows that the flow is successful.
