|
|
@ -16,13 +16,6 @@ import (
|
|
|
|
|
|
|
|
|
|
|
|
// ConfigJobsGorm 配置
|
|
|
|
// ConfigJobsGorm 配置
|
|
|
|
type ConfigJobsGorm struct {
|
|
|
|
type ConfigJobsGorm struct {
|
|
|
|
MainService int // 主要服务
|
|
|
|
|
|
|
|
Db *gorm.DB // 数据库
|
|
|
|
|
|
|
|
Redis *dorm.RedisClient // 缓存数据库服务
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// JobsGorm Gorm数据库驱动
|
|
|
|
|
|
|
|
type JobsGorm struct {
|
|
|
|
|
|
|
|
runVersion string // 运行版本
|
|
|
|
runVersion string // 运行版本
|
|
|
|
os string // 系统类型
|
|
|
|
os string // 系统类型
|
|
|
|
arch string // 系统架构
|
|
|
|
arch string // 系统架构
|
|
|
@ -30,42 +23,55 @@ type JobsGorm struct {
|
|
|
|
version string // GO版本
|
|
|
|
version string // GO版本
|
|
|
|
macAddrS string // Mac地址
|
|
|
|
macAddrS string // Mac地址
|
|
|
|
insideIp string // 内网ip
|
|
|
|
insideIp string // 内网ip
|
|
|
|
outsideIp string // 外网ip
|
|
|
|
OutsideIp string // 外网ip
|
|
|
|
mainService int // 主要服务
|
|
|
|
MainService int // 主要服务
|
|
|
|
|
|
|
|
Db *gorm.DB // 数据库
|
|
|
|
|
|
|
|
Redis *dorm.RedisClient // 缓存数据库服务
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// JobsGorm Gorm数据库驱动
|
|
|
|
|
|
|
|
type JobsGorm struct {
|
|
|
|
db *gorm.DB // 数据库
|
|
|
|
db *gorm.DB // 数据库
|
|
|
|
redis *dorm.RedisClient // 缓存数据库服务
|
|
|
|
redis *dorm.RedisClient // 缓存数据库服务
|
|
|
|
|
|
|
|
config *ConfigJobsGorm // 配置
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewJobsGorm 初始化
|
|
|
|
// NewJobsGorm 初始化
|
|
|
|
func NewJobsGorm(config *ConfigJobsGorm) *JobsGorm {
|
|
|
|
func NewJobsGorm(config *ConfigJobsGorm) (*JobsGorm, error) {
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
c := &JobsGorm{config: config}
|
|
|
|
j = &JobsGorm{}
|
|
|
|
|
|
|
|
)
|
|
|
|
c.config.runVersion = go_library.Version()
|
|
|
|
|
|
|
|
c.config.os = runtime.GOOS
|
|
|
|
|
|
|
|
c.config.arch = runtime.GOARCH
|
|
|
|
|
|
|
|
c.config.maxProCs = runtime.GOMAXPROCS(0)
|
|
|
|
|
|
|
|
c.config.version = runtime.Version()
|
|
|
|
|
|
|
|
c.config.macAddrS = goarray.TurnString(goip.GetMacAddr())
|
|
|
|
|
|
|
|
c.config.insideIp = goip.GetInsideIp()
|
|
|
|
|
|
|
|
|
|
|
|
j.runVersion = go_library.Version()
|
|
|
|
if c.config.OutsideIp == "" {
|
|
|
|
j.os = runtime.GOOS
|
|
|
|
return nil, errors.New("需要配置当前的IP")
|
|
|
|
j.arch = runtime.GOARCH
|
|
|
|
}
|
|
|
|
j.maxProCs = runtime.GOMAXPROCS(0)
|
|
|
|
|
|
|
|
j.version = runtime.Version()
|
|
|
|
if c.config.Db == nil {
|
|
|
|
j.macAddrS = goarray.TurnString(goip.GetMacAddr())
|
|
|
|
return nil, errors.New("需要配置数据库驱动")
|
|
|
|
j.insideIp = goip.GetInsideIp()
|
|
|
|
}
|
|
|
|
j.outsideIp = goip.GetOutsideIp()
|
|
|
|
|
|
|
|
j.mainService = config.MainService
|
|
|
|
if c.config.Redis == nil {
|
|
|
|
j.db = config.Db
|
|
|
|
return nil, errors.New("需要配置缓存驱动")
|
|
|
|
j.redis = config.Redis
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err := j.db.AutoMigrate(
|
|
|
|
err := c.db.AutoMigrate(
|
|
|
|
&jobs_gorm_model.Task{},
|
|
|
|
&jobs_gorm_model.Task{},
|
|
|
|
&jobs_gorm_model.TaskLog{},
|
|
|
|
&jobs_gorm_model.TaskLog{},
|
|
|
|
&jobs_gorm_model.TaskLogRun{},
|
|
|
|
&jobs_gorm_model.TaskLogRun{},
|
|
|
|
&jobs_gorm_model.TaskIp{},
|
|
|
|
&jobs_gorm_model.TaskIp{},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.New(fmt.Sprintf("创建任务模型失败:%v\n", err)))
|
|
|
|
return nil, errors.New(fmt.Sprintf("创建任务模型失败:%v\n", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return j
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (j *JobsGorm) GetDb() *gorm.DB {
|
|
|
|
func (j *JobsGorm) GetDb() *gorm.DB {
|
|
|
@ -83,7 +89,7 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) {
|
|
|
|
TaskId: info.Id,
|
|
|
|
TaskId: info.Id,
|
|
|
|
StatusCode: status,
|
|
|
|
StatusCode: status,
|
|
|
|
Desc: desc,
|
|
|
|
Desc: desc,
|
|
|
|
Version: j.runVersion,
|
|
|
|
Version: j.config.runVersion,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if statusCreate.RowsAffected == 0 {
|
|
|
|
if statusCreate.RowsAffected == 0 {
|
|
|
|
log.Println("statusCreate", statusCreate.Error)
|
|
|
|
log.Println("statusCreate", statusCreate.Error)
|
|
|
@ -108,7 +114,7 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) {
|
|
|
|
StatusDesc: "执行成功",
|
|
|
|
StatusDesc: "执行成功",
|
|
|
|
Number: info.Number + 1,
|
|
|
|
Number: info.Number + 1,
|
|
|
|
RunId: gouuid.GetUuId(),
|
|
|
|
RunId: gouuid.GetUuId(),
|
|
|
|
UpdatedIp: j.outsideIp,
|
|
|
|
UpdatedIp: j.config.OutsideIp,
|
|
|
|
Result: desc,
|
|
|
|
Result: desc,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if statusEdit.RowsAffected == 0 {
|
|
|
|
if statusEdit.RowsAffected == 0 {
|
|
|
@ -123,7 +129,7 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) {
|
|
|
|
Status: TASK_SUCCESS,
|
|
|
|
Status: TASK_SUCCESS,
|
|
|
|
StatusDesc: "结束执行",
|
|
|
|
StatusDesc: "结束执行",
|
|
|
|
Number: info.Number + 1,
|
|
|
|
Number: info.Number + 1,
|
|
|
|
UpdatedIp: j.outsideIp,
|
|
|
|
UpdatedIp: j.config.OutsideIp,
|
|
|
|
Result: desc,
|
|
|
|
Result: desc,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if statusEdit.RowsAffected == 0 {
|
|
|
|
if statusEdit.RowsAffected == 0 {
|
|
|
@ -138,7 +144,7 @@ func (j *JobsGorm) Run(info jobs_gorm_model.Task, status int, desc string) {
|
|
|
|
StatusDesc: "执行失败",
|
|
|
|
StatusDesc: "执行失败",
|
|
|
|
Number: info.Number + 1,
|
|
|
|
Number: info.Number + 1,
|
|
|
|
RunId: gouuid.GetUuId(),
|
|
|
|
RunId: gouuid.GetUuId(),
|
|
|
|
UpdatedIp: j.outsideIp,
|
|
|
|
UpdatedIp: j.config.OutsideIp,
|
|
|
|
Result: desc,
|
|
|
|
Result: desc,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if statusEdit.RowsAffected == 0 {
|
|
|
|
if statusEdit.RowsAffected == 0 {
|
|
|
@ -165,13 +171,13 @@ func (j *JobsGorm) RunAddLog(id uint, runId string) *gorm.DB {
|
|
|
|
return j.db.Create(&jobs_gorm_model.TaskLogRun{
|
|
|
|
return j.db.Create(&jobs_gorm_model.TaskLogRun{
|
|
|
|
TaskId: id,
|
|
|
|
TaskId: id,
|
|
|
|
RunId: runId,
|
|
|
|
RunId: runId,
|
|
|
|
InsideIp: j.insideIp,
|
|
|
|
InsideIp: j.config.insideIp,
|
|
|
|
OutsideIp: j.outsideIp,
|
|
|
|
OutsideIp: j.config.OutsideIp,
|
|
|
|
Os: j.os,
|
|
|
|
Os: j.config.os,
|
|
|
|
Arch: j.arch,
|
|
|
|
Arch: j.config.arch,
|
|
|
|
Gomaxprocs: j.maxProCs,
|
|
|
|
Gomaxprocs: j.config.maxProCs,
|
|
|
|
GoVersion: j.version,
|
|
|
|
GoVersion: j.config.version,
|
|
|
|
MacAddrs: j.macAddrS,
|
|
|
|
MacAddrs: j.config.macAddrS,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -197,9 +203,9 @@ func (j *JobsGorm) CreateInCustomId(config *ConfigCreateInCustomId) error {
|
|
|
|
CustomId: config.CustomId,
|
|
|
|
CustomId: config.CustomId,
|
|
|
|
CustomSequence: config.CustomSequence,
|
|
|
|
CustomSequence: config.CustomSequence,
|
|
|
|
Type: config.Type,
|
|
|
|
Type: config.Type,
|
|
|
|
CreatedIp: j.outsideIp,
|
|
|
|
CreatedIp: j.config.OutsideIp,
|
|
|
|
SpecifyIp: config.SpecifyIp,
|
|
|
|
SpecifyIp: config.SpecifyIp,
|
|
|
|
UpdatedIp: j.outsideIp,
|
|
|
|
UpdatedIp: j.config.OutsideIp,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if createStatus.RowsAffected == 0 {
|
|
|
|
if createStatus.RowsAffected == 0 {
|
|
|
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
|
|
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
|
|
@ -233,9 +239,9 @@ func (j *JobsGorm) CreateInCustomIdOnly(config *ConfigCreateInCustomIdOnly) erro
|
|
|
|
CustomId: config.CustomId,
|
|
|
|
CustomId: config.CustomId,
|
|
|
|
CustomSequence: config.CustomSequence,
|
|
|
|
CustomSequence: config.CustomSequence,
|
|
|
|
Type: config.Type,
|
|
|
|
Type: config.Type,
|
|
|
|
CreatedIp: j.outsideIp,
|
|
|
|
CreatedIp: j.config.OutsideIp,
|
|
|
|
SpecifyIp: config.SpecifyIp,
|
|
|
|
SpecifyIp: config.SpecifyIp,
|
|
|
|
UpdatedIp: j.outsideIp,
|
|
|
|
UpdatedIp: j.config.OutsideIp,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if createStatus.RowsAffected == 0 {
|
|
|
|
if createStatus.RowsAffected == 0 {
|
|
|
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
|
|
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
|
|
@ -267,9 +273,9 @@ func (j *JobsGorm) CreateInCustomIdMaxNumber(config *ConfigCreateInCustomIdMaxNu
|
|
|
|
CustomId: config.CustomId,
|
|
|
|
CustomId: config.CustomId,
|
|
|
|
CustomSequence: config.CustomSequence,
|
|
|
|
CustomSequence: config.CustomSequence,
|
|
|
|
Type: config.Type,
|
|
|
|
Type: config.Type,
|
|
|
|
CreatedIp: j.outsideIp,
|
|
|
|
CreatedIp: j.config.OutsideIp,
|
|
|
|
SpecifyIp: config.SpecifyIp,
|
|
|
|
SpecifyIp: config.SpecifyIp,
|
|
|
|
UpdatedIp: j.outsideIp,
|
|
|
|
UpdatedIp: j.config.OutsideIp,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if createStatus.RowsAffected == 0 {
|
|
|
|
if createStatus.RowsAffected == 0 {
|
|
|
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
|
|
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
|
|
@ -305,9 +311,9 @@ func (j *JobsGorm) CreateInCustomIdMaxNumberOnly(config *ConfigCreateInCustomIdM
|
|
|
|
CustomId: config.CustomId,
|
|
|
|
CustomId: config.CustomId,
|
|
|
|
CustomSequence: config.CustomSequence,
|
|
|
|
CustomSequence: config.CustomSequence,
|
|
|
|
Type: config.Type,
|
|
|
|
Type: config.Type,
|
|
|
|
CreatedIp: j.outsideIp,
|
|
|
|
CreatedIp: j.config.OutsideIp,
|
|
|
|
SpecifyIp: config.SpecifyIp,
|
|
|
|
SpecifyIp: config.SpecifyIp,
|
|
|
|
UpdatedIp: j.outsideIp,
|
|
|
|
UpdatedIp: j.config.OutsideIp,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if createStatus.RowsAffected == 0 {
|
|
|
|
if createStatus.RowsAffected == 0 {
|
|
|
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
|
|
|
return errors.New(fmt.Sprintf("创建[%s@%s]任务失败:%s", config.CustomId, config.Type, createStatus.Error))
|
|
|
|