go-wind-plugins 提供统一的工作流引擎接口,支持 Temporal、Celery 等。
type Engine interface {
Start(ctx context.Context) error
Stop(ctx context.Context) error
Submit(ctx context.Context, task *Task) (TaskID, error)
GetResult(ctx context.Context, id TaskID) (*Result, error)
Cancel(ctx context.Context, id TaskID) error
}
| 适配器 | 导入路径 | 特点 |
|---|
| Asynq | plugins/workflow/asynq | Redis 队列任务、轻量 |
| Temporal | plugins/workflow/temporal | 长流程编排、状态持久化 |
import asynqPlugin "github.com/tx7do/go-wind-plugins/workflow/asynq"
engine := asynqPlugin.New(
asynqPlugin.WithAddr("localhost:6379"),
asynqPlugin.WithConcurrency(10),
asynqPlugin.WithQueues(
asynqPlugin.Queue{Name: "critical", Priority: 6},
asynqPlugin.Queue{Name: "default", Priority: 3},
asynqPlugin.Queue{Name: "low", Priority: 1},
),
)
type EmailTask struct {
To string
Subject string
Body string
}
func HandleEmailTask(ctx context.Context, task *asynqPlugin.Task) error {
var payload EmailTask
json.Unmarshal(task.Payload, &payload)
return sendEmail(payload.To, payload.Subject, payload.Body)
}
engine.Register("send:email", HandleEmailTask)
payload, _ := json.Marshal(EmailTask{
To: "user@example.com",
Subject: "Welcome",
Body: "Welcome to GoWind!",
})
taskID, _ := engine.Submit(ctx, &workflow.Task{
Type: "send:email",
Payload: payload,
Queue: "default",
Delay: 5 * time.Minute,
MaxRetry: 3,
Timeout: 30 * time.Second,
})
engine.Schedule("0 9 * * *", &workflow.Task{
Type: "daily:report",
Payload: reportPayload,
})
workflow:
asynq:
addr: "localhost:6379"
db: 0
concurrency: 10
queues:
- name: critical
priority: 6
- name: default
priority: 3
- name: low
priority: 1
retry:
max_attempts: 5
max_duration: 24h
monitor:
enabled: true
addr: ":8090"
import temporalPlugin "github.com/tx7do/go-wind-plugins/workflow/temporal"
client, _ := temporalPlugin.New(
temporalPlugin.WithHostPort("localhost:7233"),
temporalPlugin.WithNamespace("default"),
temporalPlugin.WithTaskQueue("my-queue"),
)
func OrderWorkflow(ctx workflow.Context, order Order) error {
var inventoryOK bool
workflow.ExecuteActivity(ctx, DeductInventoryActivity, order).Get(ctx, &inventoryOK)
if !inventoryOK {
return errors.New("inventory deduction failed")
}
var paymentResult PaymentResult
workflow.ExecuteActivity(ctx, PaymentActivity, order).Get(ctx, &paymentResult)
workflow.ExecuteActivity(ctx, SendConfirmationActivity, order).Get(ctx, nil)
return nil
}
worker := temporalPlugin.NewWorker(client, "my-queue")
worker.RegisterWorkflow(OrderWorkflow)
worker.RegisterActivity(DeductInventoryActivity)
worker.RegisterActivity(PaymentActivity)
worker.RegisterActivity(SendConfirmationActivity)
worker.Start(ctx)
workflowID := "order-" + uuid.New().String()
client.ExecuteWorkflow(ctx, temporalPlugin.StartWorkflowOptions{
ID: workflowID,
TaskQueue: "my-queue",
}, OrderWorkflow, order)
workflow:
temporal:
host_port: "localhost:7233"
namespace: "default"
task_queue: "my-queue"
worker:
max_concurrent_activities: 10
max_concurrent_workflows: 5
| 场景 | 推荐 | 理由 |
|---|
| 异步任务 | Asynq | 轻量、Redis 即可 |
| 定时 Cron | Asynq | 内置 Cron 调度 |
| 订单流程 | Temporal | 状态持久化、补偿回滚 |
| 审批流程 | Temporal | 多步骤、长时间运行 |
| 批量处理 | Asynq | 优先级队列 |
| 跨服务工作流 | Temporal | 全局编排 |