2026-03-12 16:37:06 +08:00
|
|
|
package task
|
|
|
|
|
|
|
|
|
|
import (
|
2026-03-20 15:00:04 +08:00
|
|
|
"encoding/json"
|
|
|
|
|
|
2026-04-22 17:20:26 +08:00
|
|
|
"modelRT/constants"
|
|
|
|
|
|
2026-03-12 16:37:06 +08:00
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TaskQueueMessage defines minimal message structure for RabbitMQ/Redis queue dispatch
|
|
|
|
|
type TaskQueueMessage struct {
|
2026-04-27 17:55:38 +08:00
|
|
|
TaskID uuid.UUID `json:"task_id"`
|
|
|
|
|
TaskType TaskType `json:"task_type"`
|
|
|
|
|
Priority int `json:"priority,omitempty"`
|
|
|
|
|
TraceCarrier map[string]string `json:"trace_carrier,omitempty"`
|
|
|
|
|
Params map[string]any `json:"params,omitempty"`
|
2026-03-12 16:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewTaskQueueMessage(taskID uuid.UUID, taskType TaskType) *TaskQueueMessage {
|
|
|
|
|
return &TaskQueueMessage{
|
|
|
|
|
TaskID: taskID,
|
|
|
|
|
TaskType: taskType,
|
2026-04-22 17:20:26 +08:00
|
|
|
Priority: constants.TaskPriorityDefault,
|
2026-03-12 16:37:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewTaskQueueMessageWithPriority(taskID uuid.UUID, taskType TaskType, priority int) *TaskQueueMessage {
|
|
|
|
|
return &TaskQueueMessage{
|
|
|
|
|
TaskID: taskID,
|
|
|
|
|
TaskType: taskType,
|
|
|
|
|
Priority: priority,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *TaskQueueMessage) Validate() bool {
|
|
|
|
|
if m.TaskID == uuid.Nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
switch m.TaskType {
|
2026-04-22 17:20:26 +08:00
|
|
|
case TypeTopologyAnalysis, TypeEventAnalysis, TypeBatchImport, TypeTest:
|
2026-03-12 16:37:06 +08:00
|
|
|
return true
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *TaskQueueMessage) SetPriority(priority int) {
|
2026-04-22 17:20:26 +08:00
|
|
|
if priority < constants.TaskPriorityLow {
|
|
|
|
|
priority = constants.TaskPriorityLow
|
2026-03-12 16:37:06 +08:00
|
|
|
}
|
2026-04-22 17:20:26 +08:00
|
|
|
if priority > constants.TaskPriorityHigh {
|
|
|
|
|
priority = constants.TaskPriorityHigh
|
2026-03-12 16:37:06 +08:00
|
|
|
}
|
|
|
|
|
m.Priority = priority
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *TaskQueueMessage) GetPriority() int {
|
|
|
|
|
return m.Priority
|
|
|
|
|
}
|