Files
TaskPool/internal/models/task.go
T

142 lines
6.2 KiB
Go
Raw Normal View History

2025-12-20 09:30:16 +08:00
package models
import (
2026-01-18 17:31:04 +08:00
"github.com/engigu/baihu-panel/internal/constant"
2025-12-20 09:30:16 +08:00
"gorm.io/gorm"
)
2025-12-21 22:19:55 +08:00
// CleanConfig 清理配置结构
type CleanConfig struct {
Type string `json:"type"` // "day" 或 "count"
Keep int `json:"keep"` // 保留天数或条数
}
2025-12-25 17:55:54 +08:00
// RepoConfig 仓库同步配置
type RepoConfig struct {
SourceType string `json:"source_type"` // url 或 git
SourceURL string `json:"source_url"` // 源地址
TargetPath string `json:"target_path"` // 目标路径
Branch string `json:"branch"` // Git 分支
SparsePath string `json:"sparse_path"` // 稀疏检出路径(仅拉取指定目录或文件)
SingleFile bool `json:"single_file"` // 单文件模式(直接下载文件而非 sparse-checkout)
Proxy string `json:"proxy"` // 代理类型: none, ghproxy, mirror, custom
ProxyURL string `json:"proxy_url"` // 自定义代理地址
2026-03-16 10:50:07 +08:00
AuthToken string `json:"auth_token"` // 认证 Token
2026-03-19 11:06:45 +08:00
WhitelistPaths string `json:"whitelist_paths"` // 同步时保留的路径及脚本筛选白名单关键词,逗号或竖线分割
Blacklist string `json:"blacklist"` // 脚本筛选黑名单关键词,竖线分割
Dependence string `json:"dependence"` // 脚本依赖文件关键词,竖线分割
Extensions string `json:"extensions"` // 脚本文件后缀关键词,竖线分割
AutoAddCron bool `json:"auto_add_cron"` // 自动解析脚本注释添加定时任务
RepoSource string `json:"repo_source"` // 仓库来源,如果是选择了这个 ql 导入的仓库,= ql
2025-12-25 17:55:54 +08:00
}
2026-02-07 21:44:09 +08:00
// TaskConfig 任务配置 RepoConfig+TaskConfig=task.config
type TaskConfig struct {
2026-03-06 14:13:37 +08:00
Concurrency int `json:"$task_concurrency"` // 0: disable concurrency, 1: enable concurrency
2026-03-08 20:51:04 +08:00
AllEnvs bool `json:"$task_all_envs"` // 开启则注入全部环境变量
2026-02-07 21:44:09 +08:00
}
2026-02-10 18:43:59 +08:00
// Task 代表一个计划任务
2025-12-20 09:30:16 +08:00
type Task struct {
2026-03-08 20:51:04 +08:00
ID string `json:"id" gorm:"primaryKey;size:20"`
Name string `json:"name" gorm:"size:255;not null"`
2026-03-09 18:10:23 +08:00
Command BigText `json:"command"` // 普通任务的命令
2026-03-08 20:51:04 +08:00
Tags string `json:"tags" gorm:"size:255;default:''"` // 标签,逗号分隔
Type string `json:"type" gorm:"size:20;default:'task'"` // 任务类型: constant.TaskTypeNormal, constant.TaskTypeRepo
TriggerType string `json:"trigger_type" gorm:"size:25;default:'cron'"` // 触发类型: constant.TriggerTypeCron, constant.TriggerTypeBaihuStartup
2026-03-09 18:10:23 +08:00
Config BigText `json:"config"` // 配置 JSON(仓库同步配置等)
2026-03-08 20:51:04 +08:00
Schedule string `json:"schedule" gorm:"size:100"` // cron 表达式
Timeout int `json:"timeout" gorm:"default:30"` // 超时时间(分钟),默认30分钟
WorkDir string `json:"work_dir" gorm:"size:255;default:''"` // 工作目录,为空则使用 scripts 目录
CleanConfig string `json:"clean_config" gorm:"size:255;default:''"` // 清理配置 JSON
2026-03-09 18:10:23 +08:00
Envs BigText `json:"envs"` // 环境变量ID列表,逗号分隔
Languages []map[string]string `json:"languages" gorm:"serializer:json"` // 针对本地任务的语言配置列表
2026-03-08 20:51:04 +08:00
AgentID *string `json:"agent_id" gorm:"size:20;index"` // Agent ID,为空表示本地执行
2026-02-28 14:44:32 +08:00
RetryCount int `json:"retry_count" gorm:"default:0"` // 失败重试次数
RetryInterval int `json:"retry_interval" gorm:"default:0"` // 失败重试间隔(秒)
2026-03-08 20:51:04 +08:00
RandomRange int `json:"random_range" gorm:"default:0"` // 随机延迟范围(秒)
2026-02-28 14:44:32 +08:00
Enabled bool `json:"enabled" gorm:"default:true"`
2026-03-09 18:10:23 +08:00
RunningGo BigText `json:"running_go"` // 正在运行的 go routine id 数组 (JSON)
2026-03-06 14:13:37 +08:00
RuntimeEnvs []string `json:"-" gorm:"-"` // 运行时环境变量(非持久化)
2026-02-28 14:44:32 +08:00
LastRun *LocalTime `json:"last_run"`
NextRun *LocalTime `json:"next_run"`
2026-03-19 11:06:45 +08:00
SourceID string `json:"source_id" gorm:"size:255;index"` // 脚本资源唯一标识(路径 sanitized)
RepoTaskID string `json:"repo_task_id" gorm:"size:20;index"` // 所属的仓库任务 ID
2026-02-28 14:44:32 +08:00
CreatedAt LocalTime `json:"created_at"`
UpdatedAt LocalTime `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
2025-12-20 09:30:16 +08:00
}
func (Task) TableName() string {
return constant.TablePrefix + "tasks"
}
2026-02-07 21:44:09 +08:00
func (t *Task) GetID() string {
2026-03-03 17:25:38 +08:00
return t.ID
2026-02-07 21:44:09 +08:00
}
func (t *Task) GetName() string {
return t.Name
}
func (t *Task) GetCommand() string {
2026-03-09 18:10:23 +08:00
return string(t.Command)
2026-02-07 21:44:09 +08:00
}
func (t *Task) GetTimeout() int {
return t.Timeout
}
2026-02-09 18:28:14 +08:00
func (t *Task) GetWorkDir() string {
return t.WorkDir
}
func (t *Task) GetEnvs() string {
2026-03-09 18:10:23 +08:00
return string(t.Envs)
2026-02-09 18:28:14 +08:00
}
2026-02-13 19:26:20 +08:00
func (t *Task) GetLanguages() []map[string]string {
return t.Languages
2026-02-13 17:27:55 +08:00
}
2026-03-06 14:13:37 +08:00
func (t *Task) GetEnvVars() []string {
return t.RuntimeEnvs
}
2026-02-13 17:27:55 +08:00
func (t *Task) GetUseMise() bool {
2026-03-03 17:25:38 +08:00
return t.AgentID == nil || *t.AgentID == ""
2026-02-13 17:27:55 +08:00
}
func (t *Task) UseMise() bool {
return t.GetUseMise()
}
2026-02-07 21:44:09 +08:00
func (t *Task) GetSchedule() string {
return t.Schedule
}
2026-03-02 18:29:07 +08:00
func (t *Task) GetRandomRange() int {
return t.RandomRange
}
2026-02-10 18:43:59 +08:00
// TaskLog 代表任务执行的日志记录
2025-12-20 09:30:16 +08:00
type TaskLog struct {
2026-03-03 17:25:38 +08:00
ID string `json:"id" gorm:"primaryKey;size:20"`
TaskID string `json:"task_id" gorm:"size:20;index"`
AgentID *string `json:"agent_id" gorm:"size:20;index"` // Agent ID,为空表示本地执行
2026-03-09 18:10:23 +08:00
Command BigText `json:"command"`
Output BigText `json:"-"` // gzip+base64 压缩后的日志
Error BigText `json:"error"` // 额外的系统错误信息
2026-02-10 17:07:11 +08:00
Status string `json:"status" gorm:"size:20;index"` // success, failed
2026-02-10 18:43:59 +08:00
Duration int64 `json:"duration"` // 执行耗时(毫秒)
2026-01-01 14:42:33 +08:00
ExitCode int `json:"exit_code"`
StartTime *LocalTime `json:"start_time"`
EndTime *LocalTime `json:"end_time"`
CreatedAt LocalTime `json:"created_at"`
2025-12-20 09:30:16 +08:00
}
func (TaskLog) TableName() string {
return constant.TablePrefix + "task_logs"
2025-12-21 22:19:55 +08:00
}