mirror of
https://github.com/oarkflow/mq.git
synced 2025-10-05 16:06:55 +08:00
feat: add task completion
This commit is contained in:
@@ -1,13 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/oarkflow/jet"
|
||||
|
||||
v2 "github.com/oarkflow/mq/dag/v2"
|
||||
)
|
||||
|
||||
func NodeA(payload json.RawMessage) v2.Result {
|
||||
func Form(ctx context.Context, payload json.RawMessage) v2.Result {
|
||||
var data map[string]any
|
||||
if err := json.Unmarshal(payload, &data); err != nil {
|
||||
return v2.Result{Error: err, Status: v2.StatusFailed}
|
||||
}
|
||||
if templateFile, ok := data["html_content"].(string); ok {
|
||||
parser := jet.NewWithMemory(jet.WithDelims("{{", "}}"))
|
||||
rs, err := parser.ParseTemplate(templateFile, data)
|
||||
if err != nil {
|
||||
return v2.Result{Error: err, Status: v2.StatusFailed}
|
||||
}
|
||||
ctx = context.WithValue(ctx, "Content-Type", "text/html; charset/utf-8")
|
||||
return v2.Result{Data: []byte(rs), Status: v2.StatusCompleted, Ctx: ctx}
|
||||
}
|
||||
return v2.Result{Data: payload, Status: v2.StatusCompleted}
|
||||
}
|
||||
|
||||
func NodeA(ctx context.Context, payload json.RawMessage) v2.Result {
|
||||
var data map[string]any
|
||||
if err := json.Unmarshal(payload, &data); err != nil {
|
||||
return v2.Result{Error: err, Status: v2.StatusFailed}
|
||||
@@ -17,7 +37,7 @@ func NodeA(payload json.RawMessage) v2.Result {
|
||||
return v2.Result{Data: updatedPayload, Status: v2.StatusCompleted}
|
||||
}
|
||||
|
||||
func NodeB(payload json.RawMessage) v2.Result {
|
||||
func NodeB(ctx context.Context, payload json.RawMessage) v2.Result {
|
||||
var data map[string]any
|
||||
if err := json.Unmarshal(payload, &data); err != nil {
|
||||
return v2.Result{Error: err, Status: v2.StatusFailed}
|
||||
@@ -27,7 +47,7 @@ func NodeB(payload json.RawMessage) v2.Result {
|
||||
return v2.Result{Data: updatedPayload, Status: v2.StatusCompleted}
|
||||
}
|
||||
|
||||
func NodeC(payload json.RawMessage) v2.Result {
|
||||
func NodeC(ctx context.Context, payload json.RawMessage) v2.Result {
|
||||
var data map[string]any
|
||||
if err := json.Unmarshal(payload, &data); err != nil {
|
||||
return v2.Result{Error: err, Status: v2.StatusFailed}
|
||||
@@ -37,10 +57,20 @@ func NodeC(payload json.RawMessage) v2.Result {
|
||||
return v2.Result{Data: updatedPayload, Status: v2.StatusCompleted}
|
||||
}
|
||||
|
||||
func Result(payload json.RawMessage) v2.Result {
|
||||
func Result(ctx context.Context, payload json.RawMessage) v2.Result {
|
||||
var data map[string]any
|
||||
json.Unmarshal(payload, &data)
|
||||
|
||||
if err := json.Unmarshal(payload, &data); err != nil {
|
||||
return v2.Result{Error: err, Status: v2.StatusFailed}
|
||||
}
|
||||
if templateFile, ok := data["html_content"].(string); ok {
|
||||
parser := jet.NewWithMemory(jet.WithDelims("{{", "}}"))
|
||||
rs, err := parser.ParseTemplate(templateFile, data)
|
||||
if err != nil {
|
||||
return v2.Result{Error: err, Status: v2.StatusFailed}
|
||||
}
|
||||
ctx = context.WithValue(ctx, "Content-Type", "text/html; charset/utf-8")
|
||||
return v2.Result{Data: []byte(rs), Status: v2.StatusCompleted, Ctx: ctx}
|
||||
}
|
||||
return v2.Result{Data: payload, Status: v2.StatusCompleted}
|
||||
}
|
||||
|
||||
@@ -50,10 +80,12 @@ func notify(taskID string, result v2.Result) {
|
||||
|
||||
func main() {
|
||||
dag := v2.NewDAG(notify)
|
||||
// dag.AddNode("Form", Form)
|
||||
dag.AddNode("NodeA", NodeA)
|
||||
dag.AddNode("NodeB", NodeB)
|
||||
dag.AddNode("NodeC", NodeC)
|
||||
dag.AddNode("Result", Result)
|
||||
// dag.AddEdge("Form", "NodeA")
|
||||
dag.AddEdge("NodeA", "NodeB")
|
||||
dag.AddEdge("NodeB", "NodeC")
|
||||
dag.AddEdge("NodeC", "Result")
|
||||
|
Reference in New Issue
Block a user