From 07d8820fe093f3d921ff9ce8670128c224f1662e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Sat, 11 Jun 2022 16:23:33 +0800 Subject: [PATCH] - update --- CHANGELOG.md | 2 +- jobs_gorm/model_task.go | 155 +++++++++++++++++++++++--------- jobs_gorm/model_task_ip.go | 15 ++-- jobs_gorm/model_task_log.go | 12 +-- jobs_gorm/model_task_log_run.go | 22 ++--- version.go | 2 +- 6 files changed, 140 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d232c51..6cf3080 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## v1.0.22-23 +## v1.0.22-24 - update diff --git a/jobs_gorm/model_task.go b/jobs_gorm/model_task.go index 5cda198..e22e773 100644 --- a/jobs_gorm/model_task.go +++ b/jobs_gorm/model_task.go @@ -7,71 +7,138 @@ import ( // Task 任务 type Task struct { - Id uint `gorm:"primaryKey" json:"id"` // 记录编号 - Status string `json:"status"` // 状态码 - Params string `json:"params"` // 参数 - ParamsType string `json:"params_type"` // 参数类型 - StatusDesc string `json:"status_desc"` // 状态描述 - Frequency int64 `json:"frequency"` // 频率(秒单位) - Number int64 `json:"number"` // 当前次数 - MaxNumber int64 `json:"max_number"` // 最大次数 - RunId string `json:"run_id"` // 执行编号 - CustomId string `json:"custom_id"` // 自定义编号 - CustomSequence int64 `json:"custom_sequence"` // 自定义顺序 - Type string `json:"type"` // 类型 - CreatedIp string `json:"created_ip"` // 创建外网IP - SpecifyIp string `json:"specify_ip"` // 指定外网IP - UpdatedIp string `json:"updated_ip"` // 更新外网IP - Result string `json:"result"` // 结果 - CreatedAt string `gorm:"type:text" json:"created_at"` // 创建时间 - UpdatedAt string `gorm:"type:text" json:"updated_at"` // 更新时间 - DeletedAt gorm.DeletedAt `gorm:"type:text;index" json:"deleted_at"` // 删除时间 + Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 + Status string `gorm:"index;comment:状态码" json:"status"` // 状态码 + Params string `gorm:"comment:参数" json:"params"` // 参数 + ParamsType string `gorm:"comment:参数类型" json:"params_type"` // 参数类型 + StatusDesc string `gorm:"comment:状态描述" json:"status_desc"` // 状态描述 + Frequency int64 `gorm:"index;comment:频率(秒单位)" json:"frequency"` // 频率(秒单位) + Number int64 `gorm:"comment:当前次数" json:"number"` // 当前次数 + MaxNumber int64 `gorm:"comment:最大次数" json:"max_number"` // 最大次数 + RunId string `gorm:"comment:执行编号" json:"run_id"` // 执行编号 + CustomId string `gorm:"index;comment:自定义编号" json:"custom_id"` // 自定义编号 + CustomSequence int64 `gorm:"comment:自定义顺序" json:"custom_sequence"` // 自定义顺序 + Type string `gorm:"index;comment:类型" json:"type"` // 类型 + CreatedIp string `gorm:"comment:创建外网IP" json:"created_ip"` // 创建外网IP + SpecifyIp string `gorm:"comment:指定外网IP" json:"specify_ip"` // 指定外网IP + UpdatedIp string `gorm:"comment:更新外网IP" json:"updated_ip"` // 更新外网IP + Result string `gorm:"comment:结果" json:"result"` // 结果 + CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间 + UpdatedAt string `gorm:"type:text;comment:更新时间" json:"updated_at"` // 更新时间 + DeletedAt gorm.DeletedAt `gorm:"type:text;index;comment:删除时间" json:"deleted_at"` // 删除时间 } func (m *Task) TableName() string { return "task" } -// TaskTake 查询任务 +// TaskTake 查询单任务 func (jobsGorm *JobsGorm) TaskTake(tx *gorm.DB, customId string) (result Task) { - tx.Where("custom_id = ?", customId).Where("status = ?", jobs_common.TASK_IN).Take(&result) + tx.Where("custom_id = ?", customId).Take(&result) return result } -// TaskCustomIdTake 查询任务 -func (jobsGorm *JobsGorm) TaskCustomIdTake(tx *gorm.DB, Type, customId string) (result Task) { - tx.Where("type = ?", Type).Where("custom_id = ?", customId).Take(&result) +// 查询单任务 +func (jobsGorm *JobsGorm) taskTake(tx *gorm.DB, customId, status string) (result Task) { + tx.Where("custom_id = ?", customId).Where("status = ?", status).Take(&result) return result } -// TaskCustomIdTakeStatus 查询任务 -func (jobsGorm *JobsGorm) TaskCustomIdTakeStatus(tx *gorm.DB, Type, customId, status string) (result Task) { - tx.Where("type = ?", Type).Where("custom_id = ?", customId).Where("status = ?", status).Take(&result) +// TaskTakeIn 查询单任务 - 任务运行 +func (jobsGorm *JobsGorm) TaskTakeIn(tx *gorm.DB, customId string) Task { + return jobsGorm.taskTake(tx, customId, jobs_common.TASK_IN) +} + +// TaskTakeSuccess 查询单任务 - 任务完成 +func (jobsGorm *JobsGorm) TaskTakeSuccess(tx *gorm.DB, customId string) Task { + return jobsGorm.taskTake(tx, customId, jobs_common.TASK_SUCCESS) +} + +// TaskTakeError 查询单任务 - 任务异常 +func (jobsGorm *JobsGorm) TaskTakeError(tx *gorm.DB, customId string) Task { + return jobsGorm.taskTake(tx, customId, jobs_common.TASK_ERROR) +} + +// TaskTakeTimeout 查询单任务 - 任务超时 +func (jobsGorm *JobsGorm) TaskTakeTimeout(tx *gorm.DB, customId string) Task { + return jobsGorm.taskTake(tx, customId, jobs_common.TASK_TIMEOUT) +} + +// TaskTakeWait 查询单任务 - 任务等待 +func (jobsGorm *JobsGorm) TaskTakeWait(tx *gorm.DB, customId string) Task { + return jobsGorm.taskTake(tx, customId, jobs_common.TASK_WAIT) +} + +// TaskTypeTake 查询单任务 +func (jobsGorm *JobsGorm) TaskTypeTake(tx *gorm.DB, customId, Type string) (result Task) { + tx.Where("custom_id = ?", customId).Where("type = ?", Type).Take(&result) + return result +} + +// 查询单任务 +func (jobsGorm *JobsGorm) taskTypeTake(tx *gorm.DB, customId, Type, status string) (result Task) { + tx.Where("custom_id = ?", customId).Where("type = ?", Type).Where("status = ?", status).Take(&result) return result } -// TaskFind 查询任务 -func (jobsGorm *JobsGorm) TaskFind(tx *gorm.DB, frequency int64) (results []Task) { - tx.Table("task").Select("task.*").Where("task.frequency = ?", frequency).Where("task.status = ?", jobs_common.TASK_IN).Where("task_ip.ips = ?", jobsGorm.outsideIp).Order("task.id asc").Joins("left join task_ip on task_ip.task_type = task.type").Find(&results) - return jobsGorm.taskFindCheck(results) +// TaskTypeTakeIn 查询单任务 - 任务运行 +func (jobsGorm *JobsGorm) TaskTypeTakeIn(tx *gorm.DB, customId, Type string) Task { + return jobsGorm.taskTypeTake(tx, customId, Type, jobs_common.TASK_IN) +} + +// TaskTypeTakeSuccess 查询单任务 - 任务完成 +func (jobsGorm *JobsGorm) TaskTypeTakeSuccess(tx *gorm.DB, customId, Type string) Task { + return jobsGorm.taskTypeTake(tx, customId, Type, jobs_common.TASK_SUCCESS) } -// TaskFindAll 查询任务 +// TaskTypeTakeError 查询单任务 - 任务异常 +func (jobsGorm *JobsGorm) TaskTypeTakeError(tx *gorm.DB, customId, Type string) Task { + return jobsGorm.taskTypeTake(tx, customId, Type, jobs_common.TASK_ERROR) +} + +// TaskTypeTakeTimeout 查询单任务 - 任务超时 +func (jobsGorm *JobsGorm) TaskTypeTakeTimeout(tx *gorm.DB, customId, Type string) Task { + return jobsGorm.taskTypeTake(tx, customId, Type, jobs_common.TASK_TIMEOUT) +} + +// TaskTypeTakeWait 查询单任务 - 任务等待 +func (jobsGorm *JobsGorm) TaskTypeTakeWait(tx *gorm.DB, customId, Type string) Task { + return jobsGorm.taskTypeTake(tx, customId, Type, jobs_common.TASK_WAIT) +} + +// TaskFindAll 查询多任务 func (jobsGorm *JobsGorm) TaskFindAll(tx *gorm.DB, frequency int64) (results []Task) { - tx.Where("frequency = ?", frequency).Where("status = ?", jobs_common.TASK_IN).Order("id asc").Find(&results) + tx.Where("frequency = ?", frequency).Order("id asc").Find(&results) return results } -// 检查任务 -func (jobsGorm *JobsGorm) taskFindCheck(lists []Task) (results []Task) { - for _, v := range lists { - if v.SpecifyIp == "" { - results = append(results, v) - } else { - if jobsGorm.outsideIp == v.SpecifyIp { - results = append(results, v) - } - } - } +// 查询多任务 +func (jobsGorm *JobsGorm) taskFindAll(tx *gorm.DB, frequency int64, status string) (results []Task) { + tx.Where("frequency = ?", frequency).Where("status = ?", status).Order("id asc").Find(&results) return results } + +// TaskFindAllIn 查询多任务 - 任务运行 +func (jobsGorm *JobsGorm) TaskFindAllIn(tx *gorm.DB, frequency int64) []Task { + return jobsGorm.taskFindAll(tx, frequency, jobs_common.TASK_IN) +} + +// TaskFindAllSuccess 查询多任务 - 任务完成 +func (jobsGorm *JobsGorm) TaskFindAllSuccess(tx *gorm.DB, frequency int64) []Task { + return jobsGorm.taskFindAll(tx, frequency, jobs_common.TASK_SUCCESS) +} + +// TaskFindAllError 查询多任务 - 任务异常 +func (jobsGorm *JobsGorm) TaskFindAllError(tx *gorm.DB, frequency int64) []Task { + return jobsGorm.taskFindAll(tx, frequency, jobs_common.TASK_ERROR) +} + +// TaskFindAllTimeout 查询多任务 - 任务超时 +func (jobsGorm *JobsGorm) TaskFindAllTimeout(tx *gorm.DB, frequency int64) []Task { + return jobsGorm.taskFindAll(tx, frequency, jobs_common.TASK_TIMEOUT) +} + +// TaskFindAllWait 查询多任务 - 任务等待 +func (jobsGorm *JobsGorm) TaskFindAllWait(tx *gorm.DB, frequency int64) []Task { + return jobsGorm.taskFindAll(tx, frequency, jobs_common.TASK_WAIT) +} diff --git a/jobs_gorm/model_task_ip.go b/jobs_gorm/model_task_ip.go index 93302ae..dde0fde 100644 --- a/jobs_gorm/model_task_ip.go +++ b/jobs_gorm/model_task_ip.go @@ -8,18 +8,23 @@ import ( // TaskIp 任务Ip type TaskIp struct { - Id int64 `gorm:"primaryKey" json:"id"` // 记录编号 - TaskType string `json:"task_type"` // 任务编号 - Ips string `json:"ips"` // 任务IP + Id int64 `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 + TaskType string `gorm:"comment:任务编号" json:"task_type"` // 任务编号 + Ips string `gorm:"comment:任务IP" json:"ips"` // 任务IP } func (m *TaskIp) TableName() string { return "task_ip" } +func (jobsGorm *JobsGorm) taskIpTake(tx *gorm.DB, taskType, ips string) (result TaskIp) { + tx.Where("task_type = ?", taskType).Where("ips = ?", ips).Take(&result) + return result +} + +// TaskIpUpdate 更新ip func (jobsGorm *JobsGorm) TaskIpUpdate(tx *gorm.DB, taskType, ips string) *gorm.DB { - var query TaskIp - tx.Where("task_type = ?", taskType).Where("ips = ?", ips).Take(&query) + query := jobsGorm.taskIpTake(tx, taskType, ips) if query.Id != 0 { return tx } diff --git a/jobs_gorm/model_task_log.go b/jobs_gorm/model_task_log.go index bc48ac2..42cf307 100644 --- a/jobs_gorm/model_task_log.go +++ b/jobs_gorm/model_task_log.go @@ -2,12 +2,12 @@ package jobs_gorm // TaskLog 任务日志模型 type TaskLog struct { - Id uint `gorm:"primaryKey" json:"id"` // 记录编号 - TaskId uint `json:"task_id"` // 任务编号 - StatusCode int `json:"status_code"` // 状态码 - Desc string `json:"desc"` // 结果 - Version string `json:"version"` // 版本 - CreatedAt string `gorm:"type:text" json:"created_at"` // 创建时间 + Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 + TaskId uint `gorm:"comment:任务编号" json:"task_id"` // 任务编号 + StatusCode int `gorm:"comment:状态码" json:"status_code"` // 状态码 + Desc string `gorm:"comment:结果" json:"desc"` // 结果 + Version string `gorm:"comment:版本" json:"version"` // 版本 + CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间 } func (m *TaskLog) TableName() string { diff --git a/jobs_gorm/model_task_log_run.go b/jobs_gorm/model_task_log_run.go index 53d1971..6f40b2c 100644 --- a/jobs_gorm/model_task_log_run.go +++ b/jobs_gorm/model_task_log_run.go @@ -6,17 +6,17 @@ import ( // TaskLogRun 任务执行日志模型 type TaskLogRun struct { - Id uint `gorm:"primaryKey" json:"id"` // 记录编号 - TaskId uint `json:"task_id"` // 任务编号 - RunId string `json:"run_id"` // 执行编号 - OutsideIp string `json:"outside_ip"` // 外网ip - InsideIp string `json:"inside_ip"` // 内网ip - Os string `json:"os"` // 系统类型 - Arch string `json:"arch"` // 系统架构 - Gomaxprocs int `json:"gomaxprocs"` // CPU核数 - GoVersion string `json:"go_version"` // GO版本 - MacAddrs string `json:"mac_addrs"` // Mac地址 - CreatedAt string `gorm:"type:text" json:"created_at"` // 创建时间 + Id uint `gorm:"primaryKey;comment:记录编号" json:"id"` // 记录编号 + TaskId uint `gorm:"comment:任务编号" json:"task_id"` // 任务编号 + RunId string `gorm:"comment:执行编号" json:"run_id"` // 执行编号 + OutsideIp string `gorm:"comment:外网ip" json:"outside_ip"` // 外网ip + InsideIp string `gorm:"comment:内网ip" json:"inside_ip"` // 内网ip + Os string `gorm:"comment:系统类型" json:"os"` // 系统类型 + Arch string `gorm:"comment:系统架构" json:"arch"` // 系统架构 + Gomaxprocs int `gorm:"comment:CPU核数" json:"gomaxprocs"` // CPU核数 + GoVersion string `gorm:"comment:GO版本" json:"go_version"` // GO版本 + MacAddrs string `gorm:"comment:Mac地址" json:"mac_addrs"` // Mac地址 + CreatedAt string `gorm:"type:text;comment:创建时间" json:"created_at"` // 创建时间 } func (m *TaskLogRun) TableName() string { diff --git a/version.go b/version.go index 6d49e6f..4c5c65e 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package gojobs -const Version = "1.0.23" +const Version = "1.0.24"