Commit 10789817 authored by 陈子龙's avatar 陈子龙

Merge branch 'dev-czl' into dev

parents 21a02a49 7a49adf2
...@@ -62,4 +62,5 @@ const ( ...@@ -62,4 +62,5 @@ const (
LocalDateTimeFormat string = "2006-01-02 15:04:05" LocalDateTimeFormat string = "2006-01-02 15:04:05"
FinalHeartBeatUnixKey = "FinalHeartBeatUnix" FinalHeartBeatUnixKey = "FinalHeartBeatUnix"
AutoExecHistory = "AutoExecHistory" AutoExecHistory = "AutoExecHistory"
TaskExecLog = "TaskExecLog"
) )
package service package service
import ( import (
"encoding/json"
"fmt" "fmt"
json "github.com/json-iterator/go"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cast" "github.com/spf13/cast"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity"
...@@ -12,6 +12,7 @@ import ( ...@@ -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/common/conf"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"go.uber.org/zap" "go.uber.org/zap"
"strconv"
"time" "time"
) )
...@@ -125,6 +126,35 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo ...@@ -125,6 +126,35 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo
err = resp.DbConnectError.WithError(err) err = resp.DbConnectError.WithError(err)
return return
} }
redis, err := client.GetRedisClient()
if err != nil {
conf.Logger.Error("redis err", zap.Error(err))
err = resp.RedisConnectError.ErrorDetail(err)
return
}
//判断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 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). _, err = db.Table("task_history").Alias("th").Where("id = ?", id).
...@@ -134,28 +164,36 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo ...@@ -134,28 +164,36 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo
err = resp.DbSelectError.WithError(err) err = resp.DbSelectError.WithError(err)
return return
} }
taskExecLog, errMarshal := json.Marshal(taskExecLogRes)
if taskExecLogRes.State == 0 { if errMarshal != nil {
redis, err1 := client.GetRedisClient() conf.Logger.Error("Marshal taskExecLog err", zap.Error(errMarshal))
if err1 != nil { err = resp.FAIL.WithError(errMarshal)
conf.Logger.Error("redis err", zap.Error(err1)) return
err = resp.RedisConnectError.ErrorDetail(err1) }
// 写缓存
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 return
} }
}
if taskExecLogRes.State == 0 {
//判断key值是否存在 //判断key值是否存在
b1, err2 := redis.HExists(conf.AutoExecHistory, cast.ToString(id)) b1, err1 := redis.HExists(conf.AutoExecHistory, cast.ToString(id))
if err2 != nil { if err1 != nil {
conf.Logger.Error("redis HExists err", zap.Error(err2)) conf.Logger.Error("redis HExists err", zap.Error(err1))
err = resp.RedisExecError.ErrorDetail(err2) err = resp.RedisExecError.ErrorDetail(err1)
} }
if !b1 { if !b1 {
return return
} }
//获取key //获取key
value, err3 := redis.HGet(conf.AutoExecHistory, cast.ToString(id)) value, err2 := redis.HGet(conf.AutoExecHistory, cast.ToString(id))
if err3 != nil { if err2 != nil {
conf.Logger.Error("redis HGet err", zap.Error(err3)) conf.Logger.Error("redis HGet err", zap.Error(err2))
err = resp.RedisExecError.ErrorDetail(err3) err = resp.RedisExecError.ErrorDetail(err2)
return return
} }
//解析key //解析key
...@@ -166,7 +204,6 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo ...@@ -166,7 +204,6 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo
err = resp.FAIL.WithError(err) err = resp.FAIL.WithError(err)
return return
} }
conf.Logger.Info("redis HGet Log", zap.String("log", execCache.ExecLog))
taskExecLogRes.ExecLog = execCache.ExecLog taskExecLogRes.ExecLog = execCache.ExecLog
taskExecLogRes.ExecEndTime = execCache.ExecEndTime taskExecLogRes.ExecEndTime = execCache.ExecEndTime
} }
......
...@@ -456,7 +456,6 @@ func ExecAnsible(id, taskId int, value string) { ...@@ -456,7 +456,6 @@ func ExecAnsible(id, taskId int, value string) {
conf.Logger.Error("Modify Execution Status", zap.Error(err)) conf.Logger.Error("Modify Execution Status", zap.Error(err))
//return //return
} }
//redis.HDel(conf.AutoExecHistory, strconv.Itoa(id))
} else { } else {
//任务执行失败 //任务执行失败
err = UpdateExecHistory(request.UpdateExecHistory{ err = UpdateExecHistory(request.UpdateExecHistory{
...@@ -468,6 +467,7 @@ func ExecAnsible(id, taskId int, value string) { ...@@ -468,6 +467,7 @@ func ExecAnsible(id, taskId int, value string) {
conf.Logger.Error("Modify Execution Status err", zap.Error(err)) conf.Logger.Error("Modify Execution Status err", zap.Error(err))
//return //return
} }
//redis.HDel(conf.AutoExecHistory, strconv.Itoa(id))
} }
redis.HDel(conf.AutoExecHistory, strconv.Itoa(id))
redis.HDel(conf.TaskExecLog, strconv.Itoa(id))
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment