You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
5.8 KiB
174 lines
5.8 KiB
2 years ago
|
package gojobs
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"go.dtapp.net/gojobs/jobs_gorm_model"
|
||
|
"go.dtapp.net/gostring"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
// ConfigCreateInCustomId 创建正在运行任务
|
||
|
type ConfigCreateInCustomId struct {
|
||
|
Tx *gorm.DB // 驱动
|
||
|
Params string // 参数
|
||
|
Frequency int64 // 频率(秒单位)
|
||
|
CustomId string // 自定义编号
|
||
|
CustomSequence int64 // 自定义顺序
|
||
|
Type string // 类型
|
||
|
TypeName string // 类型名称
|
||
|
SpecifyIp string // 指定外网IP
|
||
|
CurrentIp string // 当前ip
|
||
|
}
|
||
|
|
||
|
// CreateInCustomId 创建正在运行任务
|
||
2 years ago
|
func (c *Client) CreateInCustomId(config *ConfigCreateInCustomId) error {
|
||
2 years ago
|
if config.CurrentIp == "" {
|
||
2 years ago
|
config.CurrentIp = c.config.outsideIp
|
||
2 years ago
|
}
|
||
|
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||
|
Status: TASK_IN,
|
||
|
Params: config.Params,
|
||
|
StatusDesc: "首次添加任务",
|
||
|
Frequency: config.Frequency,
|
||
|
RunId: gostring.GetUuId(),
|
||
|
CustomId: config.CustomId,
|
||
|
CustomSequence: config.CustomSequence,
|
||
|
Type: config.Type,
|
||
|
TypeName: config.TypeName,
|
||
|
CreatedIp: config.CurrentIp,
|
||
|
SpecifyIp: config.SpecifyIp,
|
||
|
UpdatedIp: config.CurrentIp,
|
||
|
}).Error
|
||
|
if err != nil {
|
||
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// ConfigCreateInCustomIdOnly 创建正在运行唯一任务
|
||
|
type ConfigCreateInCustomIdOnly struct {
|
||
|
Tx *gorm.DB // 驱动
|
||
|
Params string // 参数
|
||
|
Frequency int64 // 频率(秒单位)
|
||
|
CustomId string // 自定义编号
|
||
|
CustomSequence int64 // 自定义顺序
|
||
|
Type string // 类型
|
||
|
TypeName string // 类型名称
|
||
|
SpecifyIp string // 指定外网IP
|
||
|
CurrentIp string // 当前ip
|
||
|
}
|
||
|
|
||
|
// CreateInCustomIdOnly 创建正在运行唯一任务
|
||
2 years ago
|
func (c *Client) CreateInCustomIdOnly(config *ConfigCreateInCustomIdOnly) error {
|
||
|
query := c.TaskTypeTakeIn(config.Tx, config.CustomId, config.Type)
|
||
2 years ago
|
if query.Id != 0 {
|
||
|
return errors.New(fmt.Sprintf("%d:[%s@%s]任务已存在", query.Id, config.CustomId, config.Type))
|
||
|
}
|
||
|
if config.CurrentIp == "" {
|
||
2 years ago
|
config.CurrentIp = c.config.outsideIp
|
||
2 years ago
|
}
|
||
|
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||
|
Status: TASK_IN,
|
||
|
Params: config.Params,
|
||
|
StatusDesc: "首次添加任务",
|
||
|
Frequency: config.Frequency,
|
||
|
RunId: gostring.GetUuId(),
|
||
|
CustomId: config.CustomId,
|
||
|
CustomSequence: config.CustomSequence,
|
||
|
Type: config.Type,
|
||
|
TypeName: config.TypeName,
|
||
|
CreatedIp: config.CurrentIp,
|
||
|
SpecifyIp: config.SpecifyIp,
|
||
|
UpdatedIp: config.CurrentIp,
|
||
|
}).Error
|
||
|
if err != nil {
|
||
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// ConfigCreateInCustomIdMaxNumber 创建正在运行任务并限制数量
|
||
|
type ConfigCreateInCustomIdMaxNumber struct {
|
||
|
Tx *gorm.DB // 驱动
|
||
|
Params string // 参数
|
||
|
Frequency int64 // 频率(秒单位)
|
||
|
MaxNumber int64 // 最大次数
|
||
|
CustomId string // 自定义编号
|
||
|
CustomSequence int64 // 自定义顺序
|
||
|
Type string // 类型
|
||
|
TypeName string // 类型名称
|
||
|
SpecifyIp string // 指定外网IP
|
||
|
CurrentIp string // 当前ip
|
||
|
}
|
||
|
|
||
|
// CreateInCustomIdMaxNumber 创建正在运行任务并限制数量
|
||
2 years ago
|
func (c *Client) CreateInCustomIdMaxNumber(config *ConfigCreateInCustomIdMaxNumber) error {
|
||
2 years ago
|
if config.CurrentIp == "" {
|
||
2 years ago
|
config.CurrentIp = c.config.outsideIp
|
||
2 years ago
|
}
|
||
|
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||
|
Status: TASK_IN,
|
||
|
Params: config.Params,
|
||
|
StatusDesc: "首次添加任务",
|
||
|
Frequency: config.Frequency,
|
||
|
MaxNumber: config.MaxNumber,
|
||
|
RunId: gostring.GetUuId(),
|
||
|
CustomId: config.CustomId,
|
||
|
CustomSequence: config.CustomSequence,
|
||
|
Type: config.Type,
|
||
|
TypeName: config.TypeName,
|
||
|
CreatedIp: config.CurrentIp,
|
||
|
SpecifyIp: config.SpecifyIp,
|
||
|
UpdatedIp: config.CurrentIp,
|
||
|
}).Error
|
||
|
if err != nil {
|
||
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// ConfigCreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
|
||
|
type ConfigCreateInCustomIdMaxNumberOnly struct {
|
||
|
Tx *gorm.DB // 驱动
|
||
|
Params string // 参数
|
||
|
Frequency int64 // 频率(秒单位)
|
||
|
MaxNumber int64 // 最大次数
|
||
|
CustomId string // 自定义编号
|
||
|
CustomSequence int64 // 自定义顺序
|
||
|
Type string // 类型
|
||
|
TypeName string // 类型名称
|
||
|
SpecifyIp string // 指定外网IP
|
||
|
CurrentIp string // 当前ip
|
||
|
}
|
||
|
|
||
|
// CreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
|
||
2 years ago
|
func (c *Client) CreateInCustomIdMaxNumberOnly(config *ConfigCreateInCustomIdMaxNumberOnly) error {
|
||
|
query := c.TaskTypeTakeIn(config.Tx, config.CustomId, config.Type)
|
||
2 years ago
|
if query.Id != 0 {
|
||
|
return errors.New(fmt.Sprintf("%d:[%s@%s]任务已存在", query.Id, config.CustomId, config.Type))
|
||
|
}
|
||
|
if config.CurrentIp == "" {
|
||
2 years ago
|
config.CurrentIp = c.config.outsideIp
|
||
2 years ago
|
}
|
||
|
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||
|
Status: TASK_IN,
|
||
|
Params: config.Params,
|
||
|
StatusDesc: "首次添加任务",
|
||
|
Frequency: config.Frequency,
|
||
|
MaxNumber: config.MaxNumber,
|
||
|
RunId: gostring.GetUuId(),
|
||
|
CustomId: config.CustomId,
|
||
|
CustomSequence: config.CustomSequence,
|
||
|
Type: config.Type,
|
||
|
TypeName: config.TypeName,
|
||
|
CreatedIp: config.CurrentIp,
|
||
|
SpecifyIp: config.SpecifyIp,
|
||
|
UpdatedIp: config.CurrentIp,
|
||
|
}).Error
|
||
|
if err != nil {
|
||
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||
|
}
|
||
|
return nil
|
||
|
}
|