2026-03-12 16:37:06 +08:00
|
|
|
// Package orm define database data struct
|
|
|
|
|
package orm
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AsyncTaskResult stores computation results, separate from AsyncTask model for flexibility
|
|
|
|
|
type AsyncTaskResult struct {
|
2026-04-01 17:15:33 +08:00
|
|
|
TaskID uuid.UUID `gorm:"column:task_id;primaryKey;type:uuid"`
|
|
|
|
|
Result JSONMap `gorm:"column:result;type:jsonb"`
|
|
|
|
|
ErrorCode *int `gorm:"column:error_code"`
|
2026-03-12 16:37:06 +08:00
|
|
|
ErrorMessage *string `gorm:"column:error_message;type:text"`
|
2026-04-01 17:15:33 +08:00
|
|
|
ErrorDetail JSONMap `gorm:"column:error_detail;type:jsonb"`
|
|
|
|
|
ExecutionTime int64 `gorm:"column:execution_time;not null;default:0"`
|
|
|
|
|
MemoryUsage *int64 `gorm:"column:memory_usage"`
|
|
|
|
|
CPUUsage *float64 `gorm:"column:cpu_usage"`
|
|
|
|
|
RetryCount int `gorm:"column:retry_count;default:0"`
|
|
|
|
|
CompletedAt int64 `gorm:"column:completed_at;not null"`
|
2026-03-12 16:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TableName returns the table name for AsyncTaskResult model
|
|
|
|
|
func (a *AsyncTaskResult) TableName() string {
|
|
|
|
|
return "async_task_result"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetSuccess sets the result for successful task execution
|
|
|
|
|
func (a *AsyncTaskResult) SetSuccess(result JSONMap) {
|
|
|
|
|
a.Result = result
|
|
|
|
|
a.ErrorCode = nil
|
|
|
|
|
a.ErrorMessage = nil
|
|
|
|
|
a.ErrorDetail = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetError sets the error information for failed task execution
|
|
|
|
|
func (a *AsyncTaskResult) SetError(code int, message string, detail JSONMap) {
|
|
|
|
|
a.Result = nil
|
|
|
|
|
a.ErrorCode = &code
|
|
|
|
|
a.ErrorMessage = &message
|
|
|
|
|
a.ErrorDetail = detail
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HasError checks if the task result contains an error
|
|
|
|
|
func (a *AsyncTaskResult) HasError() bool {
|
|
|
|
|
return a.ErrorCode != nil || a.ErrorMessage != nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetErrorCode returns the error code or 0 if no error
|
|
|
|
|
func (a *AsyncTaskResult) GetErrorCode() int {
|
|
|
|
|
if a.ErrorCode == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return *a.ErrorCode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetErrorMessage returns the error message or empty string if no error
|
|
|
|
|
func (a *AsyncTaskResult) GetErrorMessage() string {
|
|
|
|
|
if a.ErrorMessage == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return *a.ErrorMessage
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsSuccess checks if the task execution was successful
|
|
|
|
|
func (a *AsyncTaskResult) IsSuccess() bool {
|
|
|
|
|
return !a.HasError()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear clears all result data
|
|
|
|
|
func (a *AsyncTaskResult) Clear() {
|
|
|
|
|
a.Result = nil
|
|
|
|
|
a.ErrorCode = nil
|
|
|
|
|
a.ErrorMessage = nil
|
|
|
|
|
a.ErrorDetail = nil
|
|
|
|
|
}
|