78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package task
|
|
|
|
import (
|
|
"github.com/gofrs/uuid"
|
|
)
|
|
|
|
// DefaultPriority is the default task priority
|
|
const DefaultPriority = 5
|
|
|
|
// HighPriority represents high priority tasks
|
|
const HighPriority = 10
|
|
|
|
// LowPriority represents low priority tasks
|
|
const LowPriority = 1
|
|
|
|
// TaskQueueMessage defines minimal message structure for RabbitMQ/Redis queue dispatch
|
|
// This struct is designed to be lightweight for efficient message transport
|
|
type TaskQueueMessage struct {
|
|
TaskID uuid.UUID `json:"task_id"`
|
|
TaskType TaskType `json:"task_type"`
|
|
Priority int `json:"priority,omitempty"` // Optional, defaults to DefaultPriority
|
|
}
|
|
|
|
// NewTaskQueueMessage creates a new TaskQueueMessage with default priority
|
|
func NewTaskQueueMessage(taskID uuid.UUID, taskType TaskType) *TaskQueueMessage {
|
|
return &TaskQueueMessage{
|
|
TaskID: taskID,
|
|
TaskType: taskType,
|
|
Priority: DefaultPriority,
|
|
}
|
|
}
|
|
|
|
// NewTaskQueueMessageWithPriority creates a new TaskQueueMessage with specified priority
|
|
func NewTaskQueueMessageWithPriority(taskID uuid.UUID, taskType TaskType, priority int) *TaskQueueMessage {
|
|
return &TaskQueueMessage{
|
|
TaskID: taskID,
|
|
TaskType: taskType,
|
|
Priority: priority,
|
|
}
|
|
}
|
|
|
|
// ToJSON converts the TaskQueueMessage to JSON bytes
|
|
func (m *TaskQueueMessage) ToJSON() ([]byte, error) {
|
|
return []byte{}, nil // Placeholder - actual implementation would use json.Marshal
|
|
}
|
|
|
|
// Validate checks if the TaskQueueMessage is valid
|
|
func (m *TaskQueueMessage) Validate() bool {
|
|
// Check if TaskID is valid (not nil UUID)
|
|
if m.TaskID == uuid.Nil {
|
|
return false
|
|
}
|
|
|
|
// Check if TaskType is valid
|
|
switch m.TaskType {
|
|
case TypeTopologyAnalysis, TypeEventAnalysis, TypeBatchImport:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// SetPriority sets the priority of the task queue message with validation
|
|
func (m *TaskQueueMessage) SetPriority(priority int) {
|
|
if priority < LowPriority {
|
|
priority = LowPriority
|
|
}
|
|
if priority > HighPriority {
|
|
priority = HighPriority
|
|
}
|
|
m.Priority = priority
|
|
}
|
|
|
|
// GetPriority returns the priority of the task queue message
|
|
func (m *TaskQueueMessage) GetPriority() int {
|
|
return m.Priority
|
|
}
|