-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_manager.go
More file actions
202 lines (168 loc) · 4.78 KB
/
Copy pathtask_manager.go
File metadata and controls
202 lines (168 loc) · 4.78 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package task_engine
import (
"context"
"fmt"
"log/slog"
"sync"
"time"
)
var _ TaskManagerInterface = (*TaskManager)(nil)
// TaskHandle provides access to a running task's completion status and result
type TaskHandle struct {
taskID string
done chan struct{}
err error
mu sync.Mutex
}
func (h *TaskHandle) Done() <-chan struct{} { return h.done }
func (h *TaskHandle) Err() error {
h.mu.Lock()
defer h.mu.Unlock()
return h.err
}
func (h *TaskHandle) TaskID() string { return h.taskID }
// TaskManager implements TaskManagerInterface for managing task execution
type TaskManager struct {
Tasks map[string]*Task
runningTasks map[string]context.CancelFunc
Logger *slog.Logger
mu sync.Mutex
wg sync.WaitGroup
// Global context for cross-task parameter passing. This enables actions
// in different tasks to reference outputs from other tasks.
globalContext *GlobalContext
}
func NewTaskManager(logger *slog.Logger) *TaskManager {
return &TaskManager{
Tasks: make(map[string]*Task),
runningTasks: make(map[string]context.CancelFunc),
Logger: logger,
globalContext: NewGlobalContext(),
}
}
func (tm *TaskManager) AddTask(task *Task) error {
if task == nil {
return fmt.Errorf("task is nil")
}
tm.mu.Lock()
defer tm.mu.Unlock()
// Check for duplicate task IDs
if _, exists := tm.Tasks[task.ID]; exists {
return fmt.Errorf("task ID '%s' already exists", task.ID)
}
task.Logger = tm.Logger.With("taskID", task.ID)
tm.Tasks[task.ID] = task
tm.Logger.Info("Task added", "taskID", task.ID)
return nil
}
func (tm *TaskManager) RunTask(taskID string) (*TaskHandle, error) {
tm.mu.Lock()
defer tm.mu.Unlock()
task, exists := tm.Tasks[taskID]
if !exists {
tm.Logger.Error("Task not found", "taskID", taskID)
return nil, fmt.Errorf("task %q not found", taskID)
}
ctx, cancel := context.WithCancel(context.Background())
tm.runningTasks[taskID] = cancel
gc := tm.globalContext
handle := &TaskHandle{taskID: taskID, done: make(chan struct{})}
tm.wg.Add(1)
go func(gcSnapshot *GlobalContext) {
defer tm.wg.Done()
defer close(handle.done)
defer func() {
tm.mu.Lock()
delete(tm.runningTasks, taskID)
tm.mu.Unlock()
}()
err := task.RunWithContext(ctx, gcSnapshot)
handle.mu.Lock()
handle.err = err
handle.mu.Unlock()
if err != nil {
if ctx.Err() != nil {
tm.Logger.Info("Task canceled", "taskID", taskID, "error", err)
} else {
tm.Logger.Error("Task execution failed", "taskID", taskID, "error", err)
}
} else {
tm.Logger.Info("Task completed", "taskID", taskID)
}
}(gc)
return handle, nil
}
func (tm *TaskManager) StopTask(taskID string) error {
tm.mu.Lock()
cancel, exists := tm.runningTasks[taskID]
if !exists {
tm.mu.Unlock()
return fmt.Errorf("task %q is not running", taskID)
}
delete(tm.runningTasks, taskID)
tm.mu.Unlock()
cancel()
tm.Logger.Info("Task stopped", "taskID", taskID)
return nil
}
func (tm *TaskManager) StopAllTasks() {
tm.mu.Lock()
defer tm.mu.Unlock()
for taskID, cancel := range tm.runningTasks {
cancel()
tm.Logger.Info("Task stopped", "taskID", taskID)
delete(tm.runningTasks, taskID)
}
}
// GetRunningTasks returns a list of currently running task IDs
func (tm *TaskManager) GetRunningTasks() []string {
tm.mu.Lock()
defer tm.mu.Unlock()
taskIDs := make([]string, 0, len(tm.runningTasks))
for taskID := range tm.runningTasks {
taskIDs = append(taskIDs, taskID)
}
return taskIDs
}
// IsTaskRunning checks if a specific task is currently running
func (tm *TaskManager) IsTaskRunning(taskID string) bool {
tm.mu.Lock()
defer tm.mu.Unlock()
_, exists := tm.runningTasks[taskID]
return exists
}
func (tm *TaskManager) WaitForAllTasksToComplete(timeout time.Duration) error {
done := make(chan struct{})
go func() {
tm.wg.Wait()
close(done)
}()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
select {
case <-done:
return nil
case <-ctx.Done():
tm.mu.Lock()
runningCount := len(tm.runningTasks)
tm.mu.Unlock()
return fmt.Errorf("timeout waiting for %d tasks to complete", runningCount)
}
}
// GetGlobalContext returns the global context for parameter resolution.
// Use this to access the shared context that stores outputs from all tasks
// and actions, enabling cross-entity parameter references.
func (tm *TaskManager) GetGlobalContext() *GlobalContext {
tm.mu.Lock()
defer tm.mu.Unlock()
return tm.globalContext
}
// ResetGlobalContext resets the global context, clearing all stored outputs and results.
// Use this when you want to start fresh with parameter passing, such as between
// different workflow executions or test runs.
func (tm *TaskManager) ResetGlobalContext() {
tm.mu.Lock()
defer tm.mu.Unlock()
tm.globalContext = NewGlobalContext()
tm.Logger.Info("Global context reset")
}