Commit 7a49adf2 authored by 陈子龙's avatar 陈子龙

执行ansible命令日志存储缓存

parent 996781d8
......@@ -62,4 +62,5 @@ const (
LocalDateTimeFormat string = "2006-01-02 15:04:05"
FinalHeartBeatUnixKey = "FinalHeartBeatUnix"
AutoExecHistory = "AutoExecHistory"
TaskExecLog = "TaskExecLog"
)
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
}
......
......@@ -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))
}
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