139 lines
4.0 KiB
Go
139 lines
4.0 KiB
Go
// Package task provides unified task type definitions and interfaces
|
|
package task
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/gofrs/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// UnifiedTaskType defines all async task types in a single location
|
|
type UnifiedTaskType string
|
|
|
|
const (
|
|
// TaskTypeTopologyAnalysis represents topology analysis task
|
|
TaskTypeTopologyAnalysis UnifiedTaskType = "TOPOLOGY_ANALYSIS"
|
|
// TaskTypePerformanceAnalysis represents performance analysis task
|
|
TaskTypePerformanceAnalysis UnifiedTaskType = "PERFORMANCE_ANALYSIS"
|
|
// TaskTypeEventAnalysis represents event analysis task
|
|
TaskTypeEventAnalysis UnifiedTaskType = "EVENT_ANALYSIS"
|
|
// TaskTypeBatchImport represents batch import task
|
|
TaskTypeBatchImport UnifiedTaskType = "BATCH_IMPORT"
|
|
// TaskTypeTest represents test task for system verification
|
|
TaskTypeTest UnifiedTaskType = "TEST"
|
|
)
|
|
|
|
// UnifiedTaskStatus defines task status constants
|
|
type UnifiedTaskStatus string
|
|
|
|
const (
|
|
// TaskStatusPending represents task waiting to be processed
|
|
TaskStatusPending UnifiedTaskStatus = "PENDING"
|
|
// TaskStatusRunning represents task currently executing
|
|
TaskStatusRunning UnifiedTaskStatus = "RUNNING"
|
|
// TaskStatusCompleted represents task finished successfully
|
|
TaskStatusCompleted UnifiedTaskStatus = "COMPLETED"
|
|
// TaskStatusFailed represents task failed with error
|
|
TaskStatusFailed UnifiedTaskStatus = "FAILED"
|
|
)
|
|
|
|
// TaskParams defines the interface for task-specific parameters
|
|
// All task types must implement this interface to provide their parameter structure
|
|
type TaskParams interface {
|
|
// Validate checks if the parameters are valid for this task type
|
|
Validate() error
|
|
// GetType returns the task type these parameters are for
|
|
GetType() UnifiedTaskType
|
|
// ToMap converts parameters to map for database storage
|
|
ToMap() map[string]interface{}
|
|
// FromMap populates parameters from map (for database retrieval)
|
|
FromMap(params map[string]interface{}) error
|
|
}
|
|
|
|
// UnifiedTask defines the base interface that all tasks must implement
|
|
// This provides a clean abstraction for task execution and management
|
|
type UnifiedTask interface {
|
|
// GetType returns the task type
|
|
GetType() UnifiedTaskType
|
|
|
|
// GetParams returns the task parameters
|
|
GetParams() TaskParams
|
|
|
|
// Execute performs the actual task logic
|
|
Execute(ctx context.Context, taskID uuid.UUID, db *gorm.DB) error
|
|
|
|
// GetName returns a human-readable task name for logging
|
|
GetName() string
|
|
|
|
// Validate checks if the task is valid before execution
|
|
Validate() error
|
|
}
|
|
|
|
// BaseTask provides common functionality for all task implementations
|
|
type BaseTask struct {
|
|
taskType UnifiedTaskType
|
|
params TaskParams
|
|
name string
|
|
}
|
|
|
|
// NewBaseTask creates a new BaseTask instance
|
|
func NewBaseTask(taskType UnifiedTaskType, params TaskParams, name string) *BaseTask {
|
|
return &BaseTask{
|
|
taskType: taskType,
|
|
params: params,
|
|
name: name,
|
|
}
|
|
}
|
|
|
|
// GetType returns the task type
|
|
func (t *BaseTask) GetType() UnifiedTaskType {
|
|
return t.taskType
|
|
}
|
|
|
|
// GetParams returns the task parameters
|
|
func (t *BaseTask) GetParams() TaskParams {
|
|
return t.params
|
|
}
|
|
|
|
// GetName returns the task name
|
|
func (t *BaseTask) GetName() string {
|
|
return t.name
|
|
}
|
|
|
|
// Validate checks if the task is valid
|
|
func (t *BaseTask) Validate() error {
|
|
if t.params == nil {
|
|
return fmt.Errorf("task parameters cannot be nil")
|
|
}
|
|
|
|
if t.taskType != t.params.GetType() {
|
|
return fmt.Errorf("task type mismatch: expected %s, got %s", t.taskType, t.params.GetType())
|
|
}
|
|
|
|
return t.params.Validate()
|
|
}
|
|
|
|
// IsTaskType checks if a task type string is valid
|
|
func IsTaskType(taskType string) bool {
|
|
switch UnifiedTaskType(taskType) {
|
|
case TaskTypeTopologyAnalysis, TaskTypePerformanceAnalysis,
|
|
TaskTypeEventAnalysis, TaskTypeBatchImport, TaskTypeTest:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// GetTaskTypes returns all registered task types
|
|
func GetTaskTypes() []UnifiedTaskType {
|
|
return []UnifiedTaskType{
|
|
TaskTypeTopologyAnalysis,
|
|
TaskTypePerformanceAnalysis,
|
|
TaskTypeEventAnalysis,
|
|
TaskTypeBatchImport,
|
|
TaskTypeTest,
|
|
}
|
|
}
|