mirror of
https://github.com/oarkflow/mq.git
synced 2025-10-06 00:16:49 +08:00
feat: add task completion
This commit is contained in:
@@ -52,6 +52,7 @@ type DAG struct {
|
|||||||
taskManager storage.IMap[string, *TaskManager]
|
taskManager storage.IMap[string, *TaskManager]
|
||||||
finalResult func(taskID string, result Result)
|
finalResult func(taskID string, result Result)
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
Error error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDAG(finalResultCallback func(taskID string, result Result)) *DAG {
|
func NewDAG(finalResultCallback func(taskID string, result Result)) *DAG {
|
||||||
@@ -62,14 +63,22 @@ func NewDAG(finalResultCallback func(taskID string, result Result)) *DAG {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tm *DAG) AddNode(nodeID string, handler func(payload json.RawMessage) Result) {
|
func (tm *DAG) AddNode(nodeID string, handler func(payload json.RawMessage) Result) *DAG {
|
||||||
|
if tm.Error != nil {
|
||||||
|
return tm
|
||||||
|
}
|
||||||
tm.nodes.Set(nodeID, &Node{ID: nodeID, Handler: handler})
|
tm.nodes.Set(nodeID, &Node{ID: nodeID, Handler: handler})
|
||||||
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tm *DAG) AddEdge(from string, targets ...string) error {
|
func (tm *DAG) AddEdge(from string, targets ...string) *DAG {
|
||||||
|
if tm.Error != nil {
|
||||||
|
return tm
|
||||||
|
}
|
||||||
node, ok := tm.nodes.Get(from)
|
node, ok := tm.nodes.Get(from)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("node not found %s", from)
|
tm.Error = fmt.Errorf("node not found %s", from)
|
||||||
|
return tm
|
||||||
}
|
}
|
||||||
for _, target := range targets {
|
for _, target := range targets {
|
||||||
if targetNode, ok := tm.nodes.Get(target); ok {
|
if targetNode, ok := tm.nodes.Get(target); ok {
|
||||||
@@ -77,7 +86,7 @@ func (tm *DAG) AddEdge(from string, targets ...string) error {
|
|||||||
node.Edges = append(node.Edges, edge)
|
node.Edges = append(node.Edges, edge)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tm *DAG) GetNextNodes(key string) ([]*Node, error) {
|
func (tm *DAG) GetNextNodes(key string) ([]*Node, error) {
|
||||||
@@ -116,7 +125,6 @@ func (tm *DAG) formHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
taskID := mq.NewID()
|
taskID := mq.NewID()
|
||||||
manager := NewTaskManager(tm)
|
manager := NewTaskManager(tm)
|
||||||
tm.taskManager.Set(taskID, manager)
|
tm.taskManager.Set(taskID, manager)
|
||||||
go manager.Run()
|
|
||||||
payload := fmt.Sprintf(`{"email": "%s", "age": "%s", "gender": "%s"}`, email, age, gender)
|
payload := fmt.Sprintf(`{"email": "%s", "age": "%s", "gender": "%s"}`, email, age, gender)
|
||||||
manager.Trigger(taskID, "NodeA", json.RawMessage(payload))
|
manager.Trigger(taskID, "NodeA", json.RawMessage(payload))
|
||||||
http.Redirect(w, r, "/result?taskID="+taskID, http.StatusFound)
|
http.Redirect(w, r, "/result?taskID="+taskID, http.StatusFound)
|
||||||
@@ -143,9 +151,6 @@ func (tm *DAG) taskStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tm *DAG) Start(addr string) {
|
func (tm *DAG) Start(addr string) {
|
||||||
tm.AddEdge("NodeA", "NodeB")
|
|
||||||
tm.AddEdge("NodeB", "NodeC")
|
|
||||||
tm.AddEdge("NodeC", "Result")
|
|
||||||
http.HandleFunc("/form", tm.formHandler)
|
http.HandleFunc("/form", tm.formHandler)
|
||||||
http.HandleFunc("/result", tm.resultHandler)
|
http.HandleFunc("/result", tm.resultHandler)
|
||||||
http.HandleFunc("/task-result", tm.taskStatusHandler)
|
http.HandleFunc("/task-result", tm.taskStatusHandler)
|
||||||
|
@@ -16,7 +16,6 @@ type TaskState struct {
|
|||||||
Timestamp time.Time
|
Timestamp time.Time
|
||||||
Result Result
|
Result Result
|
||||||
targetResults storage.IMap[string, Result]
|
targetResults storage.IMap[string, Result]
|
||||||
my sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type nodeResult struct {
|
type nodeResult struct {
|
||||||
@@ -46,6 +45,7 @@ func NewTaskManager(dag *DAG) *TaskManager {
|
|||||||
resultQueue: make(chan nodeResult, 100),
|
resultQueue: make(chan nodeResult, 100),
|
||||||
dag: dag,
|
dag: dag,
|
||||||
}
|
}
|
||||||
|
go tm.Run()
|
||||||
go tm.WaitForResult()
|
go tm.WaitForResult()
|
||||||
return tm
|
return tm
|
||||||
}
|
}
|
||||||
|
@@ -54,5 +54,11 @@ func main() {
|
|||||||
dag.AddNode("NodeB", NodeB)
|
dag.AddNode("NodeB", NodeB)
|
||||||
dag.AddNode("NodeC", NodeC)
|
dag.AddNode("NodeC", NodeC)
|
||||||
dag.AddNode("Result", Result)
|
dag.AddNode("Result", Result)
|
||||||
|
dag.AddEdge("NodeA", "NodeB")
|
||||||
|
dag.AddEdge("NodeB", "NodeC")
|
||||||
|
dag.AddEdge("NodeC", "Result")
|
||||||
|
if dag.Error != nil {
|
||||||
|
panic(dag.Error)
|
||||||
|
}
|
||||||
dag.Start(":8080")
|
dag.Start(":8080")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user