From 7a49adf2025c1323230ad33bc46dd4d76fbdcbdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=AD=90=E9=BE=99?= Date: Wed, 19 Jul 2023 17:59:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A7=E8=A1=8Cansible=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=AD=98=E5=82=A8=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/conf/options.go | 1 + src/service/task_history.go | 77 +++++++++++++++++++++++++++---------- src/service/task_manage.go | 4 +- 3 files changed, 60 insertions(+), 22 deletions(-) diff --git a/src/common/conf/options.go b/src/common/conf/options.go index 40dddb7..7df4028 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 c1c1b1e..953f85d 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 5cd840d..fa20fd5 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)) } -- 2.26.0