From c86027a4d3f5c29f72aabe427990796e529519e9 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:42:17 +0800 Subject: [PATCH] - update --- CHANGELOG.md | 2 +- jobs_gorm/jobs_gorm.go | 146 +++++++++++++++++++++++++++++++++++++++++ version.go | 2 +- 3 files changed, 148 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cf3080..c929dee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## v1.0.22-24 +## v1.0.22-25 - update diff --git a/jobs_gorm/jobs_gorm.go b/jobs_gorm/jobs_gorm.go index a5443aa..d6cd0b5 100644 --- a/jobs_gorm/jobs_gorm.go +++ b/jobs_gorm/jobs_gorm.go @@ -1,13 +1,18 @@ package jobs_gorm import ( + "errors" + "fmt" "go.dtapp.net/goarray" "go.dtapp.net/goip" + "go.dtapp.net/gojobs/jobs_common" "go.dtapp.net/goredis" + "go.dtapp.net/gouuid" "gorm.io/gorm" "runtime" ) +// JobsGorm 任务 type JobsGorm struct { runVersion string // 运行版本 os string // 系统类型 @@ -22,6 +27,7 @@ type JobsGorm struct { Redis *goredis.Client // 缓存数据库服务 } +// NewGorm 任务 func NewGorm(jobsGorm JobsGorm, mainService int, runVersion string) *JobsGorm { jobsGorm.runVersion = runVersion jobsGorm.os = runtime.GOOS @@ -34,3 +40,143 @@ func NewGorm(jobsGorm JobsGorm, mainService int, runVersion string) *JobsGorm { jobsGorm.mainService = mainService return &jobsGorm } + +// ConfigCreateInCustomId 创建正在运行任务 +type ConfigCreateInCustomId struct { + Tx *gorm.DB // 驱动 + Params string // 参数 + Frequency int64 // 频率(秒单位) + CustomId string // 自定义编号 + CustomSequence int64 // 自定义顺序 + Type string // 类型 + SpecifyIp string // 指定外网IP +} + +// CreateInCustomId 创建正在运行任务 +func (jobsGorm *JobsGorm) CreateInCustomId(config *ConfigCreateInCustomId) error { + createStatus := config.Tx.Create(&Task{ + Status: jobs_common.TASK_IN, + Params: config.Params, + StatusDesc: "首次添加任务", + Frequency: config.Frequency, + RunId: gouuid.GetUuId(), + CustomId: config.CustomId, + CustomSequence: config.CustomSequence, + Type: config.Type, + CreatedIp: jobsGorm.outsideIp, + SpecifyIp: config.SpecifyIp, + UpdatedIp: jobsGorm.outsideIp, + }) + if createStatus.RowsAffected == 0 { + return errors.New(fmt.Sprintf("创建任务失败:%s", createStatus.Error)) + } + return nil +} + +// ConfigCreateInCustomIdOnly 创建正在运行唯一任务 +type ConfigCreateInCustomIdOnly struct { + Tx *gorm.DB // 驱动 + Params string // 参数 + Frequency int64 // 频率(秒单位) + CustomId string // 自定义编号 + CustomSequence int64 // 自定义顺序 + Type string // 类型 + SpecifyIp string // 指定外网IP +} + +// CreateInCustomIdOnly 创建正在运行唯一任务 +func (jobsGorm *JobsGorm) CreateInCustomIdOnly(config *ConfigCreateInCustomIdOnly) error { + query := jobsGorm.TaskTypeTakeIn(config.Tx, config.CustomId, config.Type) + if query.Id != 0 { + return errors.New("任务已存在") + } + createStatus := config.Tx.Create(&Task{ + Status: jobs_common.TASK_IN, + Params: config.Params, + StatusDesc: "首次添加任务", + Frequency: config.Frequency, + RunId: gouuid.GetUuId(), + CustomId: config.CustomId, + CustomSequence: config.CustomSequence, + Type: config.Type, + CreatedIp: jobsGorm.outsideIp, + SpecifyIp: config.SpecifyIp, + UpdatedIp: jobsGorm.outsideIp, + }) + if createStatus.RowsAffected == 0 { + return errors.New(fmt.Sprintf("创建任务失败:%s", createStatus.Error)) + } + return nil +} + +// ConfigCreateInCustomIdMaxNumber 创建正在运行任务并限制数量 +type ConfigCreateInCustomIdMaxNumber struct { + Tx *gorm.DB // 驱动 + Params string // 参数 + Frequency int64 // 频率(秒单位) + MaxNumber int64 // 最大次数 + CustomId string // 自定义编号 + CustomSequence int64 // 自定义顺序 + Type string // 类型 + SpecifyIp string // 指定外网IP +} + +// CreateInCustomIdMaxNumber 创建正在运行任务并限制数量 +func (jobsGorm *JobsGorm) CreateInCustomIdMaxNumber(config *ConfigCreateInCustomIdMaxNumber) error { + createStatus := config.Tx.Create(&Task{ + Status: jobs_common.TASK_IN, + Params: config.Params, + StatusDesc: "首次添加任务", + Frequency: config.Frequency, + MaxNumber: config.MaxNumber, + RunId: gouuid.GetUuId(), + CustomId: config.CustomId, + CustomSequence: config.CustomSequence, + Type: config.Type, + CreatedIp: jobsGorm.outsideIp, + SpecifyIp: config.SpecifyIp, + UpdatedIp: jobsGorm.outsideIp, + }) + if createStatus.RowsAffected == 0 { + return errors.New(fmt.Sprintf("创建任务失败:%s", createStatus.Error)) + } + return nil +} + +// ConfigCreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量 +type ConfigCreateInCustomIdMaxNumberOnly struct { + Tx *gorm.DB // 驱动 + Params string // 参数 + Frequency int64 // 频率(秒单位) + MaxNumber int64 // 最大次数 + CustomId string // 自定义编号 + CustomSequence int64 // 自定义顺序 + Type string // 类型 + SpecifyIp string // 指定外网IP +} + +// CreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量 +func (jobsGorm *JobsGorm) CreateInCustomIdMaxNumberOnly(config *ConfigCreateInCustomIdMaxNumberOnly) error { + query := jobsGorm.TaskTypeTakeIn(config.Tx, config.CustomId, config.Type) + if query.Id != 0 { + return errors.New("任务已存在") + } + createStatus := config.Tx.Create(&Task{ + Status: jobs_common.TASK_IN, + Params: config.Params, + StatusDesc: "首次添加任务", + Frequency: config.Frequency, + MaxNumber: config.MaxNumber, + RunId: gouuid.GetUuId(), + CustomId: config.CustomId, + CustomSequence: config.CustomSequence, + Type: config.Type, + CreatedIp: jobsGorm.outsideIp, + SpecifyIp: config.SpecifyIp, + UpdatedIp: jobsGorm.outsideIp, + }) + if createStatus.RowsAffected == 0 { + return errors.New(fmt.Sprintf("创建任务失败:%s", createStatus.Error)) + } + return nil +} diff --git a/version.go b/version.go index 4c5c65e..2f295f7 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package gojobs -const Version = "1.0.24" +const Version = "1.0.25"