本文介绍如何通过工作流 ASW 编排云函数,快速完成订单数据的处理。
创建工作流程需要首先创建一个状态机,通过编排状态机的不同元件,改变状态机结构,从而实现用户自定义的功能集合。
简单三步即可完成:创建云函数 → 创建工作流 → 运行状态机
Step 1:创建云函数
a. 创建 GetOrder 函数
'use strict';
exports.main_handler = async (event, context) => {
console.log("this is get order function");
# 您可以通过api请求获取真实的订单数据,样例中数据方便模拟工作流执行
var orderlist = [
{
"orderId":"202012200001",
"goodsId":"1004",
"goodsName":"橘子#1004",
"unit":"件",
"specific":"5斤一箱",
"linePrice":100,
"salePrice":90,
"costPrice":80,
"number":30,
"isVoucher":1,
"voucherPrice":2,
"voucherId":"3dr55678hj",
"isDiscount":1,
"discountPrice":3,
"carriage": 8,
"receiver":"susu",
"phone":"18633567898",
"address":"深圳市南山区腾讯大厦20楼",
"createTime":"2020-12-20 10:00:00",
"payTime":"2020-12-20 11:00:00",
"payMethod":1,
"payOrder":"202012201100003940",
"orderStatus":3,
"deliveryTime":"2020-12-21 11:00:00",
"finishTime":"2020-12-25 11:00:00",
"deliveryOrder":"ZT12345789d786",
"isReturn":1,
"returnId":"2020122600012",
"returnNumber":2,
},
{
"orderId":"202012200001",
"goodsId":"2001",
"goodsName":"梨子#2001",
"unit":"件",
"specific":"6斤一箱",
"linePrice":150,
"salePrice":120,
"costPrice":90,
"number":20,
"isVoucher":1,
"voucherPrice":3,
"voucherId":"3dr55678hj",
"isDiscount":1,
"discountPrice":5,
"carriage": 0,
"receiver":"susu",
"phone":"18633567898",
"address":"深圳市南山区腾讯大厦20楼",
"createTime":"2020-12-20 10:00:00",
"payTime":"2020-12-20 11:00:00",
"payMethod":1,
"payOrder":"202012201100003940",
"orderStatus":3,
"deliveryTime":"2020-12-21 11:00:00",
"finishTime":"2020-12-25 11:00:00",
"deliveryOrder":"ZT12345789d786",
"isReturn":0,
"returnId":"",
"returnNumber":0,
},
{
"orderId":"202012200001",
"goodsId":"3005",
"goodsName":"香蕉#3005",
"unit":"件",
"specific":"10斤一箱",
"linePrice":180,
"salePrice":150,
"costPrice":98,
"number":6,
"isVoucher":1,
"voucherPrice":8,
"voucherId":"3dr55678hj",
"isDiscount":1,
"discountPrice":20,
"carriage": 0,
"receiver":"susu",
"phone":"18633567898",
"address":"深圳市南山区腾讯大厦20楼",
"createTime":"2020-12-20 10:00:00",
"payTime":"2020-12-20 11:00:00",
"payMethod":1,
"payOrder":"202012201100003940",
"orderStatus":3,
"deliveryTime":"2020-12-21 11:00:00",
"finishTime":"2020-12-25 11:00:00",
"deliveryOrder":"ZT12345789d786",
"isReturn":1,
"returnId":"2020122600013",
"returnNumber":3,
}
];
return {"orderList":orderlist};
};
b. 创建 ProcessOrder 函数
参考【创建 GetOrder 函数】的方式,创建 ProcessOrder 函数,代码如下:
'use strict';
exports.main_handler = async (event, context) => {
console.log("this is processOrder function");
var order = event;
#数据处理
var income = order["salePrice"]-order["costPrice"];
var goodsInfo = {"goodsId":order["goodId"],"goodsName":order["goodsName"],"number":order["number"]};
var incomeInfo = {"goodsId":order["goodId"],"goodsName":order["goodsName"],"number":order["number"],"income":income};
return {
"goodsInfo":goodsInfo,
"incomeInfo":incomeInfo,
"salesInfo":salesInfo
};
}
c. 创建 GoodsSold 函数
参考【创建 GetOrder 函数】的方式,创建 GoodsSold 函数,代码如下:
'use strict';
exports.main_handler = async (event, context) => {
console.log("this is goodsSold function");
//一些写入数据库或者图表展示操作
console.log(event);
return "GoodsSold success";
};
d. 创建 Income 函数
参考【创建 GetOrder 函数】的方式,创建 Income 函数,代码如下:
'use strict';
exports.main_handler = async (event, context) => {
console.log("this is income function");
//一些写入数据库或者图表展示操作
console.log(event);
return "Income success";
};
e. 创建 SalesReturn 函数
参考【创建 GetOrder 函数】的方式,创建 SalesReturn 函数,代码如下:
'use strict';
exports.main_handler = async (event, context) => {
console.log("this is salesReturn function");
//一些写入数据库或者图表展示操作
console.log(event);
return "SalesReturn success";
};
Step 2:创建工作流
{
"Comment": "订单处理",
"StartAt": "GetOrder",
"States": {
"GetOrder": {
"Type": "Task",
"Comment": "拉取数据",
"Resource": "qrn:qcs:asw:ap-guangzhou:12345678:sdk:json:qcloud:scf:Invoke/GetOrder",
"Next": "MapProcess"
},
"MapProcess": {
"Type": "Map",
"ItemsPath": "$.orderList",
"MaxConcurrency": 6,
"Iterator": {
"StartAt": "ProcessOrder",
"States": {
"ProcessOrder": {
"Type": "Task",
"Resource": "qrn:qcs:asw:ap-guangzhou:12345678:sdk:json:qcloud:scf:Invoke/ProcessOrder",
"Next": "ParallelDataProcess"
},
"ParallelDataProcess": {
"Type": "Parallel",
"End": true,
"Branches": [
{
"StartAt": "GoodsSold",
"States": {
"GoodsSold": {
"InputPath": "$.goodsInfo",
"Type": "Task",
"Resource": "qrn:qcs:asw:ap-guangzhou:12345678:sdk:json:qcloud:scf:Invoke/GoodsSold",
"End": true
}
}
},
{
"StartAt": "Income",
"States": {
"Income": {
"InputPath": "$.incomeInfo",
"Type": "Task",
"Resource": "qrn:qcs:asw:ap-guangzhou:12345678:sdk:json:qcloud:scf:Invoke/Income",
"End": true
}
}
},
{
"StartAt": "SalesReturn",
"States": {
"SalesReturn": {
"InputPath": "$.salesInfo",
"Type": "Task",
"Resource": "qrn:qcs:asw:ap-guangzhou:12345678:sdk:json:qcloud:scf:Invoke/SalesReturn",
"End": true
}
}
}
]
}
}
},
"End": true
}
}
}
使用已有角色需要先创建角色,并给角色授权相关策略,操作详情参考 运行角色。
Step 3:运行状态机
状态机创建完成后,您可以在登录后的主页面查看创建好的状态机。
{"comment": "invoke workflow"}
上述步骤介绍了一个订单数据处理场景的基本工作流流程。
真实的业务场景中每个 Task 节点会涉及配置一些相关参数信息,例如做参数传递、异常重试和错误捕获处理等,更多详情参考 状态机语言。
ASW 目前处于公测阶段,公测阶段免费提供服务。欢迎提出产品改进建议,反馈采纳后即可获得腾讯萌新短鹅公仔!
反馈入口
您可通过下方链接或点击文末「阅读原文」免费申请公测,我们将在 3 个工作日内完成审批,并通过短信和站内信通知您,感谢您的支持。
申请链接:https://cloud.tencent.com/product/asw?ADTAG=outer.wechat
立即体验腾讯云 Serverless Demo,获取 Serverless 新用户礼包,请在 PC 端访问: serverless.cloud.tencent.com/start?c=wx
欢迎进入千人 QQ 群 (537539545) 交流!
点击「阅读原文」进入 Serverless 中文网,体验更多 Serverless 应用的最佳实践!
本文分享自 ServerlessCloudNative 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!