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.
180 lines
6.2 KiB
180 lines
6.2 KiB
2 years ago
|
package gojobs
|
||
|
|
||
|
import (
|
||
2 years ago
|
"context"
|
||
2 years ago
|
"errors"
|
||
|
"fmt"
|
||
2 years ago
|
"github.com/dtapps/go-library/utils/gojobs/jobs_gorm_model"
|
||
2 years ago
|
"github.com/dtapps/go-library/utils/gostring"
|
||
2 years ago
|
"github.com/dtapps/go-library/utils/gotime"
|
||
2 years ago
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
// ConfigCreateInCustomId 创建正在运行任务
|
||
|
type ConfigCreateInCustomId struct {
|
||
|
Tx *gorm.DB // 驱动
|
||
|
Params string // 参数
|
||
|
Frequency int64 // 频率(秒单位)
|
||
|
CustomId string // 自定义编号
|
||
|
CustomSequence int64 // 自定义顺序
|
||
|
Type string // 类型
|
||
2 years ago
|
TypeName string // 类型名称
|
||
2 years ago
|
SpecifyIp string // 指定外网IP
|
||
2 years ago
|
CurrentIp string // 当前外网IP
|
||
2 years ago
|
}
|
||
|
|
||
|
// CreateInCustomId 创建正在运行任务
|
||
2 years ago
|
func (c *Client) CreateInCustomId(ctx context.Context, config *ConfigCreateInCustomId) error {
|
||
2 years ago
|
if config.CurrentIp == "" {
|
||
2 years ago
|
config.CurrentIp = c.config.systemOutsideIp
|
||
2 years ago
|
}
|
||
2 years ago
|
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||
2 years ago
|
Status: TASK_IN,
|
||
|
Params: config.Params,
|
||
|
StatusDesc: "首次添加任务",
|
||
|
Frequency: config.Frequency,
|
||
2 years ago
|
RunId: gostring.GetUuId(),
|
||
2 years ago
|
CustomId: config.CustomId,
|
||
|
CustomSequence: config.CustomSequence,
|
||
|
Type: config.Type,
|
||
2 years ago
|
TypeName: config.TypeName,
|
||
2 years ago
|
CreatedIp: config.CurrentIp,
|
||
2 years ago
|
SpecifyIp: config.SpecifyIp,
|
||
2 years ago
|
UpdatedIp: config.CurrentIp,
|
||
2 years ago
|
NextRunTime: gotime.Current().AfterSeconds(config.Frequency).Time,
|
||
2 years ago
|
}).Error
|
||
|
if err != nil {
|
||
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||
2 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// ConfigCreateInCustomIdOnly 创建正在运行唯一任务
|
||
|
type ConfigCreateInCustomIdOnly struct {
|
||
|
Tx *gorm.DB // 驱动
|
||
|
Params string // 参数
|
||
|
Frequency int64 // 频率(秒单位)
|
||
|
CustomId string // 自定义编号
|
||
|
CustomSequence int64 // 自定义顺序
|
||
|
Type string // 类型
|
||
2 years ago
|
TypeName string // 类型名称
|
||
2 years ago
|
SpecifyIp string // 指定外网IP
|
||
2 years ago
|
CurrentIp string // 当前外网IP
|
||
2 years ago
|
}
|
||
|
|
||
|
// CreateInCustomIdOnly 创建正在运行唯一任务
|
||
2 years ago
|
func (c *Client) CreateInCustomIdOnly(ctx context.Context, config *ConfigCreateInCustomIdOnly) error {
|
||
|
query := c.TaskTypeTakeIn(ctx, config.Tx, config.CustomId, config.Type)
|
||
2 years ago
|
if query.Id != 0 {
|
||
2 years ago
|
return TaskIsExist
|
||
2 years ago
|
}
|
||
2 years ago
|
if config.CurrentIp == "" {
|
||
2 years ago
|
config.CurrentIp = c.config.systemOutsideIp
|
||
2 years ago
|
}
|
||
2 years ago
|
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||
2 years ago
|
Status: TASK_IN,
|
||
|
Params: config.Params,
|
||
|
StatusDesc: "首次添加任务",
|
||
|
Frequency: config.Frequency,
|
||
2 years ago
|
RunId: gostring.GetUuId(),
|
||
2 years ago
|
CustomId: config.CustomId,
|
||
|
CustomSequence: config.CustomSequence,
|
||
|
Type: config.Type,
|
||
2 years ago
|
TypeName: config.TypeName,
|
||
2 years ago
|
CreatedIp: config.CurrentIp,
|
||
2 years ago
|
SpecifyIp: config.SpecifyIp,
|
||
2 years ago
|
UpdatedIp: config.CurrentIp,
|
||
2 years ago
|
NextRunTime: gotime.Current().AfterSeconds(config.Frequency).Time,
|
||
2 years ago
|
}).Error
|
||
|
if err != nil {
|
||
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||
2 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// ConfigCreateInCustomIdMaxNumber 创建正在运行任务并限制数量
|
||
|
type ConfigCreateInCustomIdMaxNumber struct {
|
||
|
Tx *gorm.DB // 驱动
|
||
|
Params string // 参数
|
||
|
Frequency int64 // 频率(秒单位)
|
||
|
MaxNumber int64 // 最大次数
|
||
|
CustomId string // 自定义编号
|
||
|
CustomSequence int64 // 自定义顺序
|
||
|
Type string // 类型
|
||
2 years ago
|
TypeName string // 类型名称
|
||
2 years ago
|
SpecifyIp string // 指定外网IP
|
||
2 years ago
|
CurrentIp string // 当前外网IP
|
||
2 years ago
|
}
|
||
|
|
||
|
// CreateInCustomIdMaxNumber 创建正在运行任务并限制数量
|
||
2 years ago
|
func (c *Client) CreateInCustomIdMaxNumber(ctx context.Context, config *ConfigCreateInCustomIdMaxNumber) error {
|
||
2 years ago
|
if config.CurrentIp == "" {
|
||
2 years ago
|
config.CurrentIp = c.config.systemOutsideIp
|
||
2 years ago
|
}
|
||
2 years ago
|
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||
2 years ago
|
Status: TASK_IN,
|
||
|
Params: config.Params,
|
||
|
StatusDesc: "首次添加任务",
|
||
|
Frequency: config.Frequency,
|
||
|
MaxNumber: config.MaxNumber,
|
||
2 years ago
|
RunId: gostring.GetUuId(),
|
||
2 years ago
|
CustomId: config.CustomId,
|
||
|
CustomSequence: config.CustomSequence,
|
||
|
Type: config.Type,
|
||
2 years ago
|
TypeName: config.TypeName,
|
||
2 years ago
|
CreatedIp: config.CurrentIp,
|
||
2 years ago
|
SpecifyIp: config.SpecifyIp,
|
||
2 years ago
|
UpdatedIp: config.CurrentIp,
|
||
2 years ago
|
NextRunTime: gotime.Current().AfterSeconds(config.Frequency).Time,
|
||
2 years ago
|
}).Error
|
||
|
if err != nil {
|
||
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||
2 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// ConfigCreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
|
||
|
type ConfigCreateInCustomIdMaxNumberOnly struct {
|
||
|
Tx *gorm.DB // 驱动
|
||
|
Params string // 参数
|
||
|
Frequency int64 // 频率(秒单位)
|
||
|
MaxNumber int64 // 最大次数
|
||
|
CustomId string // 自定义编号
|
||
|
CustomSequence int64 // 自定义顺序
|
||
|
Type string // 类型
|
||
2 years ago
|
TypeName string // 类型名称
|
||
2 years ago
|
SpecifyIp string // 指定外网IP
|
||
2 years ago
|
CurrentIp string // 当前外网IP
|
||
2 years ago
|
}
|
||
|
|
||
|
// CreateInCustomIdMaxNumberOnly 创建正在运行唯一任务并限制数量
|
||
2 years ago
|
func (c *Client) CreateInCustomIdMaxNumberOnly(ctx context.Context, config *ConfigCreateInCustomIdMaxNumberOnly) error {
|
||
|
query := c.TaskTypeTakeIn(ctx, config.Tx, config.CustomId, config.Type)
|
||
2 years ago
|
if query.Id != 0 {
|
||
2 years ago
|
return TaskIsExist
|
||
2 years ago
|
}
|
||
2 years ago
|
if config.CurrentIp == "" {
|
||
2 years ago
|
config.CurrentIp = c.config.systemOutsideIp
|
||
2 years ago
|
}
|
||
2 years ago
|
err := config.Tx.Create(&jobs_gorm_model.Task{
|
||
2 years ago
|
Status: TASK_IN,
|
||
|
Params: config.Params,
|
||
|
StatusDesc: "首次添加任务",
|
||
|
Frequency: config.Frequency,
|
||
|
MaxNumber: config.MaxNumber,
|
||
2 years ago
|
RunId: gostring.GetUuId(),
|
||
2 years ago
|
CustomId: config.CustomId,
|
||
|
CustomSequence: config.CustomSequence,
|
||
|
Type: config.Type,
|
||
2 years ago
|
TypeName: config.TypeName,
|
||
2 years ago
|
CreatedIp: config.CurrentIp,
|
||
2 years ago
|
SpecifyIp: config.SpecifyIp,
|
||
2 years ago
|
UpdatedIp: config.CurrentIp,
|
||
2 years ago
|
NextRunTime: gotime.Current().AfterSeconds(config.Frequency).Time,
|
||
2 years ago
|
}).Error
|
||
|
if err != nil {
|
||
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, err.Error()))
|
||
2 years ago
|
}
|
||
|
return nil
|
||
|
}
|