-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathplugins.go
More file actions
181 lines (140 loc) · 4.93 KB
/
Copy pathplugins.go
File metadata and controls
181 lines (140 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package floxy
import (
"context"
"fmt"
"log/slog"
"sort"
"sync"
)
// Plugin represents a lifecycle hook system for workflows
type Plugin interface {
// Name returns unique plugin identifier
Name() string
// Priority determines execution order (higher = earlier)
Priority() Priority
// Lifecycle hooks
OnWorkflowStart(ctx context.Context, instance *WorkflowInstance) error
OnWorkflowComplete(ctx context.Context, instance *WorkflowInstance) error
OnWorkflowFailed(ctx context.Context, instance *WorkflowInstance) error
OnStepStart(ctx context.Context, instance *WorkflowInstance, step *WorkflowStep) error
OnStepComplete(ctx context.Context, instance *WorkflowInstance, step *WorkflowStep) error
OnStepFailed(ctx context.Context, instance *WorkflowInstance, step *WorkflowStep, err error) error
OnRollbackStepChain(ctx context.Context, instanceID int64, stepName string, depth int) error
}
// BasePlugin provides default no-op implementations
type BasePlugin struct {
name string
priority Priority
}
func NewBasePlugin(name string, priority Priority) BasePlugin {
return BasePlugin{name: name, priority: priority}
}
func (p BasePlugin) Name() string { return p.name }
func (p BasePlugin) Priority() Priority { return p.priority }
func (p BasePlugin) OnWorkflowStart(context.Context, *WorkflowInstance) error {
return nil
}
func (p BasePlugin) OnWorkflowComplete(context.Context, *WorkflowInstance) error {
return nil
}
func (p BasePlugin) OnWorkflowFailed(context.Context, *WorkflowInstance) error {
return nil
}
func (p BasePlugin) OnStepStart(context.Context, *WorkflowInstance, *WorkflowStep) error { return nil }
func (p BasePlugin) OnStepComplete(context.Context, *WorkflowInstance, *WorkflowStep) error {
return nil
}
func (p BasePlugin) OnStepFailed(context.Context, *WorkflowInstance, *WorkflowStep, error) error {
return nil
}
func (p BasePlugin) OnRollbackStepChain(context.Context, int64, string, int) error {
return nil
}
// PluginManager manages plugin lifecycle
type PluginManager struct {
plugins []Plugin
mu sync.RWMutex
}
func NewPluginManager() *PluginManager {
return &PluginManager{
plugins: make([]Plugin, 0),
}
}
func (pm *PluginManager) Register(plugin Plugin) {
pm.mu.Lock()
defer pm.mu.Unlock()
pm.plugins = append(pm.plugins, plugin)
sort.Slice(pm.plugins, func(i, j int) bool {
return pm.plugins[i].Priority() < pm.plugins[j].Priority()
})
}
func (pm *PluginManager) ExecuteWorkflowStart(ctx context.Context, instance *WorkflowInstance) error {
pm.mu.RLock()
defer pm.mu.RUnlock()
for _, plugin := range pm.plugins {
if err := plugin.OnWorkflowStart(ctx, instance); err != nil {
return fmt.Errorf("plugin %s failed: %w", plugin.Name(), err)
}
}
return nil
}
func (pm *PluginManager) ExecuteWorkflowComplete(ctx context.Context, instance *WorkflowInstance) error {
pm.mu.RLock()
defer pm.mu.RUnlock()
for _, plugin := range pm.plugins {
if err := plugin.OnWorkflowComplete(ctx, instance); err != nil {
slog.Error("[floxy] plugin error on workflow complete", "plugin", plugin.Name(), "error", err)
}
}
return nil
}
func (pm *PluginManager) ExecuteWorkflowFailed(ctx context.Context, instance *WorkflowInstance) error {
pm.mu.RLock()
defer pm.mu.RUnlock()
for _, plugin := range pm.plugins {
if err := plugin.OnWorkflowFailed(ctx, instance); err != nil {
slog.Error("[floxy] plugin error on workflow failed", "plugin", plugin.Name(), "error", err)
}
}
return nil
}
func (pm *PluginManager) ExecuteStepStart(ctx context.Context, instance *WorkflowInstance, step *WorkflowStep) error {
pm.mu.RLock()
defer pm.mu.RUnlock()
for _, plugin := range pm.plugins {
if err := plugin.OnStepStart(ctx, instance, step); err != nil {
return fmt.Errorf("plugin %s failed: %w", plugin.Name(), err)
}
}
return nil
}
func (pm *PluginManager) ExecuteStepComplete(ctx context.Context, instance *WorkflowInstance, step *WorkflowStep) error {
pm.mu.RLock()
defer pm.mu.RUnlock()
for _, plugin := range pm.plugins {
if err := plugin.OnStepComplete(ctx, instance, step); err != nil {
slog.Error("[floxy] plugin error on step complete", "plugin", plugin.Name(), "error", err)
}
}
return nil
}
func (pm *PluginManager) ExecuteStepFailed(ctx context.Context, instance *WorkflowInstance, step *WorkflowStep, err error) error {
pm.mu.RLock()
defer pm.mu.RUnlock()
for _, plugin := range pm.plugins {
if pluginErr := plugin.OnStepFailed(ctx, instance, step, err); pluginErr != nil {
slog.Error("[floxy] plugin error on step failed", "plugin", plugin.Name(), "error", err)
}
}
return nil
}
func (pm *PluginManager) ExecuteRollbackStepChain(ctx context.Context, instanceID int64, stepName string, depth int) error {
pm.mu.RLock()
defer pm.mu.RUnlock()
for _, plugin := range pm.plugins {
if err := plugin.OnRollbackStepChain(ctx, instanceID, stepName, depth); err != nil {
slog.Error("[floxy] plugin error on rollback step chain", "plugin", plugin.Name(), "error", err)
}
}
return nil
}