75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package task
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"modelRT/constants"
|
|
|
|
"github.com/gofrs/uuid"
|
|
)
|
|
|
|
// 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 constants.TaskPriorityDefault
|
|
TraceID string `json:"trace_id,omitempty"` // propagated from the originating HTTP request
|
|
SpanID string `json:"span_id,omitempty"` // spanID of the step that enqueued this message
|
|
}
|
|
|
|
// NewTaskQueueMessage creates a new TaskQueueMessage with default priority
|
|
func NewTaskQueueMessage(taskID uuid.UUID, taskType TaskType) *TaskQueueMessage {
|
|
return &TaskQueueMessage{
|
|
TaskID: taskID,
|
|
TaskType: taskType,
|
|
Priority: constants.TaskPriorityDefault,
|
|
}
|
|
}
|
|
|
|
// 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 TaskQueueMessage to JSON bytes
|
|
func (m *TaskQueueMessage) ToJSON() ([]byte, error) {
|
|
return json.Marshal(m)
|
|
}
|
|
|
|
// Validate checks if 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, TypeTest:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// SetPriority sets priority of task queue message with validation
|
|
func (m *TaskQueueMessage) SetPriority(priority int) {
|
|
if priority < constants.TaskPriorityLow {
|
|
priority = constants.TaskPriorityLow
|
|
}
|
|
if priority > constants.TaskPriorityHigh {
|
|
priority = constants.TaskPriorityHigh
|
|
}
|
|
m.Priority = priority
|
|
}
|
|
|
|
// GetPriority returns priority of task queue message
|
|
func (m *TaskQueueMessage) GetPriority() int {
|
|
return m.Priority
|
|
}
|