定义执行历史和执行队列model, 以及基础的增删改查功能
This commit is contained in:
32
internal/infra/models/schedule.go
Normal file
32
internal/infra/models/schedule.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// PendingTask 是一个待执行任务队列, 里面会储存待执行的Task以及这个Task什么时候执行
|
||||
// 它是一个纯粹的工作队列,任务被认领后即被删除。
|
||||
type PendingTask struct {
|
||||
// 手动填充必须字段以实现硬删除,不内嵌 gorm.Model
|
||||
ID uint `gorm:"primarykey"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
|
||||
// 业务字段
|
||||
|
||||
// TaskID 使用 int 类型以容纳特殊的负数ID,代表系统任务
|
||||
TaskID int `gorm:"index"`
|
||||
|
||||
ExecuteAt time.Time `gorm:"index"` // 任务执行时间
|
||||
TaskExecutionLogID uint `gorm:"unique;not null"` // 对应的执行历史记录ID
|
||||
|
||||
// 关联关系定义
|
||||
// 通过 TaskExecutionLogID 关联到唯一的 TaskExecutionLog 记录
|
||||
// ON DELETE CASCADE 确保如果日志被删除,这个待办任务也会被自动清理
|
||||
TaskExecutionLog TaskExecutionLog `gorm:"foreignKey:TaskExecutionLogID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
|
||||
}
|
||||
|
||||
// TableName 自定义 GORM 使用的数据库表名
|
||||
func (PendingTask) TableName() string {
|
||||
return "pending_tasks"
|
||||
}
|
||||
Reference in New Issue
Block a user