2026-03-12 16:37:06 +08:00
|
|
|
// Package orm define database data struct
|
|
|
|
|
package orm
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AsyncTaskType defines the type of asynchronous task
|
|
|
|
|
type AsyncTaskType string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// AsyncTaskTypeTopologyAnalysis represents topology analysis task
|
|
|
|
|
AsyncTaskTypeTopologyAnalysis AsyncTaskType = "TOPOLOGY_ANALYSIS"
|
|
|
|
|
// AsyncTaskTypePerformanceAnalysis represents performance analysis task
|
|
|
|
|
AsyncTaskTypePerformanceAnalysis AsyncTaskType = "PERFORMANCE_ANALYSIS"
|
|
|
|
|
// AsyncTaskTypeEventAnalysis represents event analysis task
|
|
|
|
|
AsyncTaskTypeEventAnalysis AsyncTaskType = "EVENT_ANALYSIS"
|
|
|
|
|
// AsyncTaskTypeBatchImport represents batch import task
|
|
|
|
|
AsyncTaskTypeBatchImport AsyncTaskType = "BATCH_IMPORT"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AsyncTaskStatus defines the status of asynchronous task
|
|
|
|
|
type AsyncTaskStatus string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// AsyncTaskStatusSubmitted represents task has been submitted to queue
|
|
|
|
|
AsyncTaskStatusSubmitted AsyncTaskStatus = "SUBMITTED"
|
|
|
|
|
// AsyncTaskStatusRunning represents task is currently executing
|
|
|
|
|
AsyncTaskStatusRunning AsyncTaskStatus = "RUNNING"
|
|
|
|
|
// AsyncTaskStatusCompleted represents task completed successfully
|
|
|
|
|
AsyncTaskStatusCompleted AsyncTaskStatus = "COMPLETED"
|
|
|
|
|
// AsyncTaskStatusFailed represents task failed with error
|
|
|
|
|
AsyncTaskStatusFailed AsyncTaskStatus = "FAILED"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AsyncTask defines the core task entity stored in database for task lifecycle tracking
|
|
|
|
|
type AsyncTask struct {
|
2026-04-01 17:15:33 +08:00
|
|
|
TaskID uuid.UUID `gorm:"column:task_id;primaryKey;type:uuid;default:gen_random_uuid()"`
|
|
|
|
|
TaskType AsyncTaskType `gorm:"column:task_type;type:varchar(50);not null;index"`
|
|
|
|
|
Status AsyncTaskStatus `gorm:"column:status;type:varchar(20);not null;index"`
|
|
|
|
|
Params JSONMap `gorm:"column:params;type:jsonb"`
|
|
|
|
|
CreatedAt int64 `gorm:"column:created_at;not null;index"`
|
|
|
|
|
FinishedAt *int64 `gorm:"column:finished_at;index"`
|
|
|
|
|
StartedAt *int64 `gorm:"column:started_at;index"`
|
|
|
|
|
ExecutionTime *int64 `gorm:"column:execution_time"`
|
|
|
|
|
Progress *int `gorm:"column:progress"` // 0-100, nullable
|
|
|
|
|
RetryCount int `gorm:"column:retry_count;default:0"`
|
|
|
|
|
MaxRetryCount int `gorm:"column:max_retry_count;default:3"`
|
|
|
|
|
NextRetryTime *int64 `gorm:"column:next_retry_time;index"`
|
|
|
|
|
RetryDelay int `gorm:"column:retry_delay;default:5000"`
|
|
|
|
|
Priority int `gorm:"column:priority;default:5;index"`
|
|
|
|
|
QueueName string `gorm:"column:queue_name;type:varchar(100);default:'default'"`
|
|
|
|
|
WorkerID *string `gorm:"column:worker_id;type:varchar(50)"`
|
|
|
|
|
FailureReason *string `gorm:"column:failure_reason;type:text"`
|
|
|
|
|
StackTrace *string `gorm:"column:stack_trace;type:text"`
|
|
|
|
|
CreatedBy *string `gorm:"column:created_by;type:varchar(100)"`
|
2026-03-12 16:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName returns the table name for AsyncTask model
|
|
|
|
|
func (a *AsyncTask) TableName() string {
|
|
|
|
|
return "async_task"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetSubmitted marks the task as submitted
|
|
|
|
|
func (a *AsyncTask) SetSubmitted() {
|
|
|
|
|
a.Status = AsyncTaskStatusSubmitted
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetRunning marks the task as running
|
|
|
|
|
func (a *AsyncTask) SetRunning() {
|
|
|
|
|
a.Status = AsyncTaskStatusRunning
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetCompleted marks the task as completed with finished timestamp
|
|
|
|
|
func (a *AsyncTask) SetCompleted(timestamp int64) {
|
|
|
|
|
a.Status = AsyncTaskStatusCompleted
|
|
|
|
|
a.FinishedAt = ×tamp
|
|
|
|
|
a.setProgress(100)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetFailed marks the task as failed with finished timestamp
|
|
|
|
|
func (a *AsyncTask) SetFailed(timestamp int64) {
|
|
|
|
|
a.Status = AsyncTaskStatusFailed
|
|
|
|
|
a.FinishedAt = ×tamp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setProgress updates the task progress (0-100)
|
|
|
|
|
func (a *AsyncTask) setProgress(value int) {
|
|
|
|
|
if value < 0 {
|
|
|
|
|
value = 0
|
|
|
|
|
}
|
|
|
|
|
if value > 100 {
|
|
|
|
|
value = 100
|
|
|
|
|
}
|
|
|
|
|
a.Progress = &value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateProgress updates the task progress with validation
|
|
|
|
|
func (a *AsyncTask) UpdateProgress(value int) {
|
|
|
|
|
a.setProgress(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsCompleted checks if the task is completed
|
|
|
|
|
func (a *AsyncTask) IsCompleted() bool {
|
|
|
|
|
return a.Status == AsyncTaskStatusCompleted
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsRunning checks if the task is running
|
|
|
|
|
func (a *AsyncTask) IsRunning() bool {
|
|
|
|
|
return a.Status == AsyncTaskStatusRunning
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsFailed checks if the task failed
|
|
|
|
|
func (a *AsyncTask) IsFailed() bool {
|
|
|
|
|
return a.Status == AsyncTaskStatusFailed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsValidTaskType checks if the task type is valid
|
|
|
|
|
func IsValidAsyncTaskType(taskType string) bool {
|
|
|
|
|
switch AsyncTaskType(taskType) {
|
|
|
|
|
case AsyncTaskTypeTopologyAnalysis, AsyncTaskTypePerformanceAnalysis,
|
|
|
|
|
AsyncTaskTypeEventAnalysis, AsyncTaskTypeBatchImport:
|
|
|
|
|
return true
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|