diff --git a/src/common/conf/options.go b/src/common/conf/options.go index 40dddb73aee18ce6c2b7c7f96bbaf07665395bfb..7df4028a08ba7d73b0d9dcc1d6ee906028fb99ed 100644 --- a/src/common/conf/options.go +++ b/src/common/conf/options.go @@ -62,4 +62,5 @@ const ( LocalDateTimeFormat string = "2006-01-02 15:04:05" FinalHeartBeatUnixKey = "FinalHeartBeatUnix" AutoExecHistory = "AutoExecHistory" + TaskExecLog = "TaskExecLog" ) diff --git a/src/service/task_history.go b/src/service/task_history.go index c1c1b1e7ba5cd55ffb3f289669bd6ac0bcdad197..953f85dc3462902818e451520b3ec3990fad04d9 100644 --- a/src/service/task_history.go +++ b/src/service/task_history.go @@ -1,8 +1,8 @@ package service import ( + "encoding/json" "fmt" - json "github.com/json-iterator/go" "github.com/pkg/errors" "github.com/spf13/cast" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" @@ -12,6 +12,7 @@ import ( "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" "go.uber.org/zap" + "strconv" "time" ) @@ -125,37 +126,74 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo err = resp.DbConnectError.WithError(err) return } - - //查询 - _, err = db.Table("task_history").Alias("th").Where("id = ?", id). - Select("th.id,th.task_id,th.exec_desc,(select tm.task_name from task_manage tm where th.task_id = tm.id and tm.is_delete = 0) as task_name," + - "th.exec_start_time,th.exec_end_time,th.state,th.exec_log").Get(&taskExecLogRes) + redis, err := client.GetRedisClient() if err != nil { - err = resp.DbSelectError.WithError(err) + conf.Logger.Error("redis err", zap.Error(err)) + err = resp.RedisConnectError.ErrorDetail(err) return } - if taskExecLogRes.State == 0 { - redis, err1 := client.GetRedisClient() + //判断key值是否存在 + t1, err := redis.HExists(conf.TaskExecLog, cast.ToString(id)) + if err != nil { + conf.Logger.Error("redis TaskExecLog HExists err", zap.Error(err)) + err = resp.RedisExecError.ErrorDetail(err) + } + if t1 { + //获取key + value, err1 := redis.HGet(conf.TaskExecLog, cast.ToString(id)) if err1 != nil { - conf.Logger.Error("redis err", zap.Error(err1)) - err = resp.RedisConnectError.ErrorDetail(err1) + conf.Logger.Error("redis TaskExecLog HGet err", zap.Error(err1)) + err = resp.RedisExecError.ErrorDetail(err1) + return + } + //解析key + err = json.Unmarshal([]byte(value), &taskExecLogRes) + if err != nil { + conf.Logger.Error("redis value Unmarshal err", zap.Error(err)) + err = resp.FAIL.WithError(err) + return + } + } else { + + //查询 + _, err = db.Table("task_history").Alias("th").Where("id = ?", id). + Select("th.id,th.task_id,th.exec_desc,(select tm.task_name from task_manage tm where th.task_id = tm.id and tm.is_delete = 0) as task_name," + + "th.exec_start_time,th.exec_end_time,th.state,th.exec_log").Get(&taskExecLogRes) + if err != nil { + err = resp.DbSelectError.WithError(err) + return + } + taskExecLog, errMarshal := json.Marshal(taskExecLogRes) + if errMarshal != nil { + conf.Logger.Error("Marshal taskExecLog err", zap.Error(errMarshal)) + err = resp.FAIL.WithError(errMarshal) + return + } + // 写缓存 + err = redis.HSet(conf.TaskExecLog, strconv.Itoa(id), fmt.Sprintf("%s", taskExecLog)) + if err != nil { + conf.Logger.Error("Failed to execute history cache err", zap.Error(err)) + err = resp.FAIL.WithError(err) return } + } + + if taskExecLogRes.State == 0 { //判断key值是否存在 - b1, err2 := redis.HExists(conf.AutoExecHistory, cast.ToString(id)) - if err2 != nil { - conf.Logger.Error("redis HExists err", zap.Error(err2)) - err = resp.RedisExecError.ErrorDetail(err2) + b1, err1 := redis.HExists(conf.AutoExecHistory, cast.ToString(id)) + if err1 != nil { + conf.Logger.Error("redis HExists err", zap.Error(err1)) + err = resp.RedisExecError.ErrorDetail(err1) } if !b1 { return } //获取key - value, err3 := redis.HGet(conf.AutoExecHistory, cast.ToString(id)) - if err3 != nil { - conf.Logger.Error("redis HGet err", zap.Error(err3)) - err = resp.RedisExecError.ErrorDetail(err3) + value, err2 := redis.HGet(conf.AutoExecHistory, cast.ToString(id)) + if err2 != nil { + conf.Logger.Error("redis HGet err", zap.Error(err2)) + err = resp.RedisExecError.ErrorDetail(err2) return } //解析key @@ -166,7 +204,6 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo err = resp.FAIL.WithError(err) return } - conf.Logger.Info("redis HGet Log", zap.String("log", execCache.ExecLog)) taskExecLogRes.ExecLog = execCache.ExecLog taskExecLogRes.ExecEndTime = execCache.ExecEndTime } diff --git a/src/service/task_manage.go b/src/service/task_manage.go index 5cd840d761fa87cc644ddc55c424661a1a06de8e..fa20fd501b908454babfbe6d63d0d5a32ffb305a 100644 --- a/src/service/task_manage.go +++ b/src/service/task_manage.go @@ -456,7 +456,6 @@ func ExecAnsible(id, taskId int, value string) { conf.Logger.Error("Modify Execution Status", zap.Error(err)) //return } - //redis.HDel(conf.AutoExecHistory, strconv.Itoa(id)) } else { //任务执行失败 err = UpdateExecHistory(request.UpdateExecHistory{ @@ -468,6 +467,7 @@ func ExecAnsible(id, taskId int, value string) { conf.Logger.Error("Modify Execution Status err", zap.Error(err)) //return } - //redis.HDel(conf.AutoExecHistory, strconv.Itoa(id)) } + redis.HDel(conf.AutoExecHistory, strconv.Itoa(id)) + redis.HDel(conf.TaskExecLog, strconv.Itoa(id)) }