temp
This commit is contained in:
@@ -610,3 +610,121 @@ func (sm *SagaManager) Shutdown(ctx context.Context) error {
|
||||
sm.logger.Info("Saga manager service shutdown")
|
||||
return nil
|
||||
}
|
||||
|
||||
// ==================== Saga构建器 ====================
|
||||
|
||||
// StepBuilder Saga步骤构建器
|
||||
type StepBuilder struct {
|
||||
name string
|
||||
action func(ctx context.Context, data interface{}) error
|
||||
compensate func(ctx context.Context, data interface{}) error
|
||||
timeout time.Duration
|
||||
maxRetries int
|
||||
}
|
||||
|
||||
// Step 创建步骤构建器
|
||||
func Step(name string) *StepBuilder {
|
||||
return &StepBuilder{
|
||||
name: name,
|
||||
timeout: 30 * time.Second,
|
||||
maxRetries: 3,
|
||||
}
|
||||
}
|
||||
|
||||
// Action 设置正向操作
|
||||
func (sb *StepBuilder) Action(action func(ctx context.Context, data interface{}) error) *StepBuilder {
|
||||
sb.action = action
|
||||
return sb
|
||||
}
|
||||
|
||||
// Compensate 设置补偿操作
|
||||
func (sb *StepBuilder) Compensate(compensate func(ctx context.Context, data interface{}) error) *StepBuilder {
|
||||
sb.compensate = compensate
|
||||
return sb
|
||||
}
|
||||
|
||||
// Timeout 设置超时时间
|
||||
func (sb *StepBuilder) Timeout(timeout time.Duration) *StepBuilder {
|
||||
sb.timeout = timeout
|
||||
return sb
|
||||
}
|
||||
|
||||
// MaxRetries 设置最大重试次数
|
||||
func (sb *StepBuilder) MaxRetries(maxRetries int) *StepBuilder {
|
||||
sb.maxRetries = maxRetries
|
||||
return sb
|
||||
}
|
||||
|
||||
// Build 构建Saga步骤
|
||||
func (sb *StepBuilder) Build() *SagaStep {
|
||||
return &SagaStep{
|
||||
Name: sb.name,
|
||||
Action: sb.action,
|
||||
Compensate: sb.compensate,
|
||||
Status: StepPending,
|
||||
MaxRetries: sb.maxRetries,
|
||||
Timeout: sb.timeout,
|
||||
}
|
||||
}
|
||||
|
||||
// SagaBuilder Saga构建器
|
||||
type SagaBuilder struct {
|
||||
manager *SagaManager
|
||||
saga *Saga
|
||||
steps []*SagaStep
|
||||
}
|
||||
|
||||
// NewSagaBuilder 创建Saga构建器
|
||||
func NewSagaBuilder(manager *SagaManager, id, name string) *SagaBuilder {
|
||||
saga := manager.CreateSaga(id, name)
|
||||
return &SagaBuilder{
|
||||
manager: manager,
|
||||
saga: saga,
|
||||
steps: make([]*SagaStep, 0),
|
||||
}
|
||||
}
|
||||
|
||||
// AddStep 添加步骤
|
||||
func (sb *SagaBuilder) AddStep(step *SagaStep) *SagaBuilder {
|
||||
sb.steps = append(sb.steps, step)
|
||||
sb.saga.AddStepWithConfig(step.Name, step.Action, step.Compensate, step.MaxRetries, step.Timeout)
|
||||
return sb
|
||||
}
|
||||
|
||||
// AddSteps 批量添加步骤
|
||||
func (sb *SagaBuilder) AddSteps(steps ...*SagaStep) *SagaBuilder {
|
||||
for _, step := range steps {
|
||||
sb.AddStep(step)
|
||||
}
|
||||
return sb
|
||||
}
|
||||
|
||||
// Execute 执行Saga
|
||||
func (sb *SagaBuilder) Execute(ctx context.Context, data interface{}) error {
|
||||
return sb.saga.Execute(ctx, data)
|
||||
}
|
||||
|
||||
// GetSaga 获取Saga实例
|
||||
func (sb *SagaBuilder) GetSaga() *Saga {
|
||||
return sb.saga
|
||||
}
|
||||
|
||||
// 便捷函数
|
||||
|
||||
// CreateSaga 快速创建Saga
|
||||
func CreateSaga(manager *SagaManager, name string) *SagaBuilder {
|
||||
id := fmt.Sprintf("%s_%d", name, time.Now().Unix())
|
||||
return NewSagaBuilder(manager, id, name)
|
||||
}
|
||||
|
||||
// ExecuteSaga 快速执行Saga
|
||||
func ExecuteSaga(manager *SagaManager, name string, steps []*SagaStep, data interface{}, logger *zap.Logger) error {
|
||||
saga := CreateSaga(manager, name)
|
||||
saga.AddSteps(steps...)
|
||||
|
||||
logger.Info("开始执行Saga",
|
||||
zap.String("saga_name", name),
|
||||
zap.Int("steps_count", len(steps)))
|
||||
|
||||
return saga.Execute(context.Background(), data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user