modelRT/task/queue_message.go

75 lines
2.1 KiB
Go
Raw Normal View History

2026-03-12 16:37:06 +08:00
package task
import (
2026-03-20 15:00:04 +08:00
"encoding/json"
"modelRT/constants"
2026-03-12 16:37:06 +08:00
"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
2026-03-12 16:37:06 +08:00
}
// 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,
2026-03-12 16:37:06 +08:00
}
}
// 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
2026-03-12 16:37:06 +08:00
func (m *TaskQueueMessage) ToJSON() ([]byte, error) {
2026-03-20 15:00:04 +08:00
return json.Marshal(m)
2026-03-12 16:37:06 +08:00
}
// Validate checks if TaskQueueMessage is valid
2026-03-12 16:37:06 +08:00
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:
2026-03-12 16:37:06 +08:00
return true
default:
return false
}
}
// SetPriority sets priority of task queue message with validation
2026-03-12 16:37:06 +08:00
func (m *TaskQueueMessage) SetPriority(priority int) {
if priority < constants.TaskPriorityLow {
priority = constants.TaskPriorityLow
2026-03-12 16:37:06 +08:00
}
if priority > constants.TaskPriorityHigh {
priority = constants.TaskPriorityHigh
2026-03-12 16:37:06 +08:00
}
m.Priority = priority
}
// GetPriority returns priority of task queue message
2026-03-12 16:37:06 +08:00
func (m *TaskQueueMessage) GetPriority() int {
return m.Priority
}