本文介紹如何經過工做流 ASW 編排雲函數,快速完成訂單數據的處理。數據庫
建立工做流程須要首先建立一個狀態機,經過編排狀態機的不一樣元件,改變狀態機結構,從而實現用戶自定義的功能集合。json
簡單三步便可完成:建立雲函數 → 建立工做流 → 運行狀態機api
'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}; };
參考【建立 GetOrder 函數】的方式,建立 ProcessOrder 函數,代碼以下:less
'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 }; }
參考【建立 GetOrder 函數】的方式,建立 GoodsSold 函數,代碼以下:async
'use strict'; exports.main_handler = async (event, context) => { console.log("this is goodsSold function"); //一些寫入數據庫或者圖表展現操做 console.log(event); return "GoodsSold success"; };
參考【建立 GetOrder 函數】的方式,建立 Income 函數,代碼以下:函數
'use strict'; exports.main_handler = async (event, context) => { console.log("this is income function"); //一些寫入數據庫或者圖表展現操做 console.log(event); return "Income success"; };
參考【建立 GetOrder 函數】的方式,建立 SalesReturn 函數,代碼以下:this
'use strict'; exports.main_handler = async (event, context) => { console.log("this is salesReturn function"); //一些寫入數據庫或者圖表展現操做 console.log(event); return "SalesReturn success"; };
{ "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 } } }
使用已有角色須要先建立角色,並給角色受權相關策略,操做詳情參考 運行角色。3d
狀態機建立完成後,您能夠在登陸後的主頁面查看建立好的狀態機。code
{"comment": "invoke workflow"}
上述步驟介紹了一個訂單數據處理場景的基本工做流流程。server
真實的業務場景中每一個 Task 節點會涉及配置一些相關參數信息,例如作參數傳遞、異常重試和錯誤捕獲處理等,更多詳情參考 狀態機語言。
ASW 目前處於公測階段,公測階段免費提供服務。歡迎提出產品改進建議,反饋採納後便可得到騰訊萌新短鵝公仔!
當即申請公測,咱們將會在 3 個工做日內完成審批,並經過短信和站內信通知您,感謝您的支持。
當即體驗騰訊雲 Serverless Demo,領取 Serverless 新用戶禮包 👉 serverless/start
歡迎訪問:Serverless 中文網!