mirror of
https://github.com/oarkflow/mq.git
synced 2025-10-06 16:36:53 +08:00
init: publisher
This commit is contained in:
6
dag.go
6
dag.go
@@ -103,12 +103,12 @@ func (dag *DAG) TaskCallback(ctx context.Context, task *Task) error {
|
||||
}
|
||||
|
||||
func (dag *DAG) AddNode(queue string, handler Handler, firstNode ...bool) {
|
||||
consumer := NewConsumer(dag.brokerAddr)
|
||||
consumer.RegisterHandler(queue, handler)
|
||||
con := NewConsumer(dag.brokerAddr)
|
||||
con.RegisterHandler(queue, handler)
|
||||
dag.broker.NewQueue(queue)
|
||||
n := &node{
|
||||
queue: queue,
|
||||
consumer: consumer,
|
||||
consumer: con,
|
||||
handler: handler,
|
||||
}
|
||||
if len(firstNode) > 0 && firstNode[0] {
|
||||
|
@@ -4,8 +4,9 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/oarkflow/mq"
|
||||
"time"
|
||||
|
||||
"github.com/oarkflow/mq"
|
||||
)
|
||||
|
||||
func handleNode1(_ context.Context, task mq.Task) mq.Result {
|
||||
@@ -40,15 +41,25 @@ func handleNode2(_ context.Context, task mq.Task) mq.Result {
|
||||
}
|
||||
|
||||
func handleNode3(_ context.Context, task mq.Task) mq.Result {
|
||||
result := `{"field": "node3", "item": %s}`
|
||||
fmt.Printf("Processing task at node3: %s\n", string(task.Payload))
|
||||
return mq.Result{Status: "completed", Payload: json.RawMessage(fmt.Sprintf(result, string(task.Payload)))}
|
||||
var data map[string]any
|
||||
err := json.Unmarshal(task.Payload, &data)
|
||||
if err != nil {
|
||||
return mq.Result{Error: err}
|
||||
}
|
||||
data["item"] = "Item processed in node3"
|
||||
bt, _ := json.Marshal(data)
|
||||
return mq.Result{Status: "completed", Payload: bt}
|
||||
}
|
||||
|
||||
func handleNode4(_ context.Context, task mq.Task) mq.Result {
|
||||
result := `{"field": "node4", "item": %s}`
|
||||
fmt.Printf("Processing task at node4: %s\n", string(task.Payload))
|
||||
return mq.Result{Status: "completed", Payload: json.RawMessage(fmt.Sprintf(result, string(task.Payload)))}
|
||||
var data map[string]any
|
||||
err := json.Unmarshal(task.Payload, &data)
|
||||
if err != nil {
|
||||
return mq.Result{Error: err}
|
||||
}
|
||||
data["item"] = "An Item processed in node4"
|
||||
bt, _ := json.Marshal(data)
|
||||
return mq.Result{Status: "completed", Payload: bt}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
Reference in New Issue
Block a user