-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterfaces.go
More file actions
271 lines (211 loc) · 10.6 KB
/
Copy pathinterfaces.go
File metadata and controls
271 lines (211 loc) · 10.6 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package background
import (
"context"
"time"
"digital.vasic.models"
)
// TaskExecutor defines the interface for task execution
type TaskExecutor interface {
// Execute runs the task with context and progress reporting
Execute(ctx context.Context, task *models.BackgroundTask, reporter ProgressReporter) error
// CanPause returns whether this task type supports pause/resume
CanPause() bool
// Pause saves checkpoint for later resume
Pause(ctx context.Context, task *models.BackgroundTask) ([]byte, error)
// Resume restores from checkpoint
Resume(ctx context.Context, task *models.BackgroundTask, checkpoint []byte) error
// Cancel handles graceful cancellation
Cancel(ctx context.Context, task *models.BackgroundTask) error
// GetResourceRequirements returns resource needs for this executor
GetResourceRequirements() ResourceRequirements
}
// ProgressReporter allows tasks to report progress
type ProgressReporter interface {
// ReportProgress reports task progress (0-100 percentage)
ReportProgress(percent float64, message string) error
// ReportHeartbeat sends a heartbeat to indicate the task is still alive
ReportHeartbeat() error
// ReportCheckpoint saves a checkpoint for pause/resume capability
ReportCheckpoint(data []byte) error
// ReportMetrics reports custom metrics from the task
ReportMetrics(metrics map[string]interface{}) error
// ReportLog reports a log entry from the task
ReportLog(level, message string, fields map[string]interface{}) error
}
// TaskQueue defines the queue interface for task management
type TaskQueue interface {
// Enqueue adds a task to the queue
Enqueue(ctx context.Context, task *models.BackgroundTask) error
// Dequeue atomically retrieves and claims a task from the queue
Dequeue(ctx context.Context, workerID string, requirements ResourceRequirements) (*models.BackgroundTask, error)
// Peek returns tasks without claiming them
Peek(ctx context.Context, count int) ([]*models.BackgroundTask, error)
// Requeue returns a task to the queue with optional delay
Requeue(ctx context.Context, taskID string, delay time.Duration) error
// MoveToDeadLetter moves a failed task to dead-letter queue
MoveToDeadLetter(ctx context.Context, taskID string, reason string) error
// GetPendingCount returns the number of pending tasks
GetPendingCount(ctx context.Context) (int64, error)
// GetRunningCount returns the number of running tasks
GetRunningCount(ctx context.Context) (int64, error)
// GetQueueDepth returns counts by priority
GetQueueDepth(ctx context.Context) (map[models.TaskPriority]int64, error)
}
// TaskWaiter provides synchronous waiting for task completion
type TaskWaiter interface {
// WaitForCompletion blocks until the task completes, fails, or times out
// Returns the final task state and any error
// progressCallback is called with progress updates (can be nil)
WaitForCompletion(ctx context.Context, taskID string, timeout time.Duration, progressCallback func(progress float64, message string)) (*models.BackgroundTask, error)
// WaitForCompletionWithOutput waits and returns both task state and captured output
WaitForCompletionWithOutput(ctx context.Context, taskID string, timeout time.Duration) (*models.BackgroundTask, []byte, error)
}
// WaitResult contains the result of waiting for a task
type WaitResult struct {
Task *models.BackgroundTask
Output []byte
Duration time.Duration
Error error
}
// TaskRepository handles database operations for tasks
type TaskRepository interface {
// CRUD operations
Create(ctx context.Context, task *models.BackgroundTask) error
GetByID(ctx context.Context, id string) (*models.BackgroundTask, error)
Update(ctx context.Context, task *models.BackgroundTask) error
Delete(ctx context.Context, id string) error
// Status operations
UpdateStatus(ctx context.Context, id string, status models.TaskStatus) error
UpdateProgress(ctx context.Context, id string, progress float64, message string) error
UpdateHeartbeat(ctx context.Context, id string) error
SaveCheckpoint(ctx context.Context, id string, checkpoint []byte) error
// Query operations
GetByStatus(ctx context.Context, status models.TaskStatus, limit, offset int) ([]*models.BackgroundTask, error)
GetPendingTasks(ctx context.Context, limit int) ([]*models.BackgroundTask, error)
GetStaleTasks(ctx context.Context, threshold time.Duration) ([]*models.BackgroundTask, error)
GetByWorkerID(ctx context.Context, workerID string) ([]*models.BackgroundTask, error)
CountByStatus(ctx context.Context) (map[models.TaskStatus]int64, error)
// Dequeue with atomic update
Dequeue(ctx context.Context, workerID string, maxCPUCores, maxMemoryMB int) (*models.BackgroundTask, error)
// Resource snapshots
SaveResourceSnapshot(ctx context.Context, snapshot *models.ResourceSnapshot) error
GetResourceSnapshots(ctx context.Context, taskID string, limit int) ([]*models.ResourceSnapshot, error)
// Execution history
LogEvent(ctx context.Context, taskID, eventType string, data map[string]interface{}, workerID *string) error
GetTaskHistory(ctx context.Context, taskID string, limit int) ([]*models.TaskExecutionHistory, error)
// Dead letter queue
MoveToDeadLetter(ctx context.Context, taskID, reason string) error
}
// ResourceRequirements specifies resource needs for a task
type ResourceRequirements struct {
CPUCores int `json:"cpu_cores"`
MemoryMB int `json:"memory_mb"`
DiskMB int `json:"disk_mb"`
GPUCount int `json:"gpu_count"`
Priority models.TaskPriority `json:"priority"`
}
// SystemResources represents available system resources
type SystemResources struct {
TotalCPUCores int `json:"total_cpu_cores"`
AvailableCPUCores float64 `json:"available_cpu_cores"`
TotalMemoryMB int64 `json:"total_memory_mb"`
AvailableMemoryMB int64 `json:"available_memory_mb"`
CPULoadPercent float64 `json:"cpu_load_percent"`
MemoryUsedPercent float64 `json:"memory_used_percent"`
DiskUsedPercent float64 `json:"disk_used_percent"`
LoadAvg1 float64 `json:"load_avg_1"`
LoadAvg5 float64 `json:"load_avg_5"`
LoadAvg15 float64 `json:"load_avg_15"`
}
// ResourceMonitor tracks system and process resources
type ResourceMonitor interface {
// GetSystemResources returns current system resource usage
GetSystemResources() (*SystemResources, error)
// GetProcessResources returns resource usage for a specific process
GetProcessResources(pid int) (*models.ResourceSnapshot, error)
// StartMonitoring begins periodic monitoring of a process
StartMonitoring(taskID string, pid int, interval time.Duration) error
// StopMonitoring stops monitoring a process
StopMonitoring(taskID string) error
// GetLatestSnapshot returns the most recent snapshot for a task
GetLatestSnapshot(taskID string) (*models.ResourceSnapshot, error)
// IsResourceAvailable checks if system has enough resources
IsResourceAvailable(requirements ResourceRequirements) bool
}
// StuckDetector identifies stuck processes
type StuckDetector interface {
// IsStuck determines if a task is stuck based on various criteria
IsStuck(ctx context.Context, task *models.BackgroundTask, snapshots []*models.ResourceSnapshot) (bool, string)
// GetStuckThreshold returns the stuck detection threshold for a task type
GetStuckThreshold(taskType string) time.Duration
// SetThreshold sets a custom threshold for a task type
SetThreshold(taskType string, threshold time.Duration)
}
// NotificationService handles task notifications
type NotificationService interface {
// NotifyTaskEvent sends notifications for a task event
NotifyTaskEvent(ctx context.Context, task *models.BackgroundTask, event string, data map[string]interface{}) error
// RegisterSSEClient registers a client for SSE notifications
RegisterSSEClient(ctx context.Context, taskID string, client chan<- []byte) error
// UnregisterSSEClient removes an SSE client
UnregisterSSEClient(ctx context.Context, taskID string, client chan<- []byte) error
// RegisterWebSocketClient registers a WebSocket client
RegisterWebSocketClient(ctx context.Context, taskID string, client WebSocketClient) error
// BroadcastToTask broadcasts a message to all clients watching a task
BroadcastToTask(ctx context.Context, taskID string, message []byte) error
}
// WebSocketClient interface for WebSocket connections
type WebSocketClient interface {
// Send sends data to the client
Send(data []byte) error
// Close closes the connection
Close() error
// ID returns the client identifier
ID() string
}
// WorkerPool manages background task workers
type WorkerPool interface {
// Start initializes and starts the worker pool
Start(ctx context.Context) error
// Stop gracefully stops the worker pool
Stop(gracePeriod time.Duration) error
// RegisterExecutor registers a task executor for a task type
RegisterExecutor(taskType string, executor TaskExecutor)
// GetWorkerCount returns the current number of workers
GetWorkerCount() int
// GetActiveTaskCount returns the number of currently executing tasks
GetActiveTaskCount() int
// GetWorkerStatus returns status information for all workers
GetWorkerStatus() []WorkerStatus
// Scale manually adjusts the worker count
Scale(targetCount int) error
}
// WorkerStatus represents the status of a worker
type WorkerStatus struct {
ID string `json:"id"`
Status string `json:"status"` // idle, busy, stopping, stopped
CurrentTask *models.BackgroundTask `json:"current_task,omitempty"`
StartedAt time.Time `json:"started_at"`
LastActivity time.Time `json:"last_activity"`
TasksCompleted int64 `json:"tasks_completed"`
TasksFailed int64 `json:"tasks_failed"`
AvgTaskDuration time.Duration `json:"avg_task_duration"`
}
// TaskEvent represents a task lifecycle event
type TaskEvent struct {
TaskID string `json:"task_id"`
EventType string `json:"event_type"`
Timestamp time.Time `json:"timestamp"`
Data map[string]interface{} `json:"data,omitempty"`
WorkerID *string `json:"worker_id,omitempty"`
}
// ExecutionResult represents the result of task execution
type ExecutionResult struct {
TaskID string `json:"task_id"`
Status models.TaskStatus `json:"status"`
Output []byte `json:"output,omitempty"`
Error string `json:"error,omitempty"`
Duration time.Duration `json:"duration"`
RetryCount int `json:"retry_count"`
ResourceMetrics *models.ResourceSnapshot `json:"resource_metrics,omitempty"`
}