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.
gojobs/run.go

121 lines
3.6 KiB

2 years ago
package gojobs
import (
"context"
2 years ago
"go.dtapp.net/gojobs/jobs_gorm_model"
2 years ago
"go.dtapp.net/gotime"
2 years ago
"go.dtapp.net/gotrace_id"
2 years ago
)
// Run 运行
func (c *Client) Run(ctx context.Context, info jobs_gorm_model.Task, status int, result string) {
2 years ago
runId := gotrace_id.GetTraceIdContext(ctx)
if runId == "" {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[gojobs.Run]%s", "上下文没有跟踪编号")
return
}
2 years ago
// 请求函数记录
2 years ago
err := c.db.gormClient.Db.Create(&jobs_gorm_model.TaskLog{
2 years ago
TaskId: info.Id,
StatusCode: status,
2 years ago
Desc: result,
Version: c.config.sdkVersion,
2 years ago
}).Error
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[gojobs.Run.Create]%s", err.Error())
2 years ago
}
if status == 0 {
2 years ago
err = c.EditTask(c.db.gormClient.Db, info.Id).
2 years ago
Select("run_id", "result", "next_run_time").
2 years ago
Updates(jobs_gorm_model.Task{
2 years ago
RunId: runId,
2 years ago
Result: result,
NextRunTime: gotime.Current().AfterSeconds(info.Frequency).Time,
2 years ago
}).Error
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[gojobs.Run.0.EditTask]%s", err.Error())
2 years ago
}
return
}
// 任务
if status == CodeSuccess {
// 执行成功
2 years ago
err = c.EditTask(c.db.gormClient.Db, info.Id).
2 years ago
Select("status_desc", "number", "run_id", "updated_ip", "result", "next_run_time").
2 years ago
Updates(jobs_gorm_model.Task{
2 years ago
StatusDesc: "执行成功",
Number: info.Number + 1,
2 years ago
RunId: runId,
UpdatedIp: c.config.systemOutsideIp,
2 years ago
Result: result,
NextRunTime: gotime.Current().AfterSeconds(info.Frequency).Time,
2 years ago
}).Error
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[gojobs.Run.CodeSuccess.EditTask]%s", err.Error())
2 years ago
}
}
if status == CodeEnd {
// 执行成功、提前结束
2 years ago
err = c.EditTask(c.db.gormClient.Db, info.Id).
2 years ago
Select("status", "status_desc", "number", "updated_ip", "result", "next_run_time").
2 years ago
Updates(jobs_gorm_model.Task{
2 years ago
Status: TASK_SUCCESS,
StatusDesc: "结束执行",
Number: info.Number + 1,
UpdatedIp: c.config.systemOutsideIp,
2 years ago
Result: result,
NextRunTime: gotime.Current().Time,
2 years ago
}).Error
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[gojobs.Run.CodeEnd.EditTask]%s", err.Error())
2 years ago
}
}
if status == CodeError {
// 执行失败
2 years ago
err = c.EditTask(c.db.gormClient.Db, info.Id).
2 years ago
Select("status_desc", "number", "run_id", "updated_ip", "result", "next_run_time").
2 years ago
Updates(jobs_gorm_model.Task{
2 years ago
StatusDesc: "执行失败",
Number: info.Number + 1,
2 years ago
RunId: runId,
UpdatedIp: c.config.systemOutsideIp,
2 years ago
Result: result,
NextRunTime: gotime.Current().AfterSeconds(info.Frequency).Time,
2 years ago
}).Error
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[gojobs.Run.CodeError.EditTask]%s", err.Error())
2 years ago
}
}
if info.MaxNumber != 0 {
if info.Number+1 >= info.MaxNumber {
// 关闭执行
2 years ago
err = c.EditTask(c.db.gormClient.Db, info.Id).
2 years ago
Select("status").
Updates(jobs_gorm_model.Task{
Status: TASK_TIMEOUT,
}).Error
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[gojobs.Run.TASK_TIMEOUT.EditTask]%s", err.Error())
2 years ago
}
}
}
}
// RunAddLog 任务执行日志
func (c *Client) RunAddLog(ctx context.Context, id uint, runId string) error {
2 years ago
return c.db.gormClient.Db.Create(&jobs_gorm_model.TaskLogRun{
2 years ago
TaskId: id,
RunId: runId,
InsideIp: c.config.systemInsideIp,
OutsideIp: c.config.systemOutsideIp,
Os: c.config.systemOs,
Arch: c.config.systemArch,
Gomaxprocs: c.config.systemCpuQuantity,
GoVersion: c.config.goVersion,
SdkVersion: c.config.sdkVersion,
MacAddrs: c.config.systemMacAddrS,
2 years ago
}).Error
}