From 25560e75dbff3d1e2542774f56f71eac08c8e84d 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 10:41:38 +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/service/host_manage.go | 3 +-- src/service/task_history.go | 29 +++++++++++++++++++++++ src/service/task_manage.go | 46 ++++++++++++++++++++++++++----------- src/service/work_order.go | 6 ++--- 4 files changed, 65 insertions(+), 19 deletions(-) diff --git a/src/service/host_manage.go b/src/service/host_manage.go index 2ee1498..4a03daf 100644 --- a/src/service/host_manage.go +++ b/src/service/host_manage.go @@ -378,13 +378,12 @@ func (h *HostManageSvc) PageListHostManage(req request.ListHostManageReq) (total if req.CreateDateTo != "" { finder.Where("hm.create_time <= ?", req.CreateDateTo) } - finder.OrderBy("hm.id") //查询任务 total, err = finder.Select("hm.id,hm.host_name,hm.create_user,hm.create_time,(SELECT COUNT(*) FROM "+ "task_manage tm WHERE tm.is_delete = 0 AND tm.host_group_id = hm.ID) AS task_cnt,(SELECT COUNT(*) FROM "+ "host_manage_list hml WHERE hml.is_delete = 0 AND hml.conn_status = 1 AND hml.host_group_id = hm.ID) AS "+ "ip_cnt_err,(SELECT COUNT(*) FROM host_manage_list hml WHERE hml.is_delete = 0 AND hml.host_group_id = hm.ID) AS ip_cnt"). - OrderBy("hm.create_time").Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&hostManageListRes) + OrderBy("hm.create_time desc").Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&hostManageListRes) if err != nil { err = resp.DbSelectError.WithError(err) return diff --git a/src/service/task_history.go b/src/service/task_history.go index 82ac5e5..c3a3fa5 100644 --- a/src/service/task_history.go +++ b/src/service/task_history.go @@ -2,12 +2,16 @@ package service import ( "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" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" + "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" "time" ) @@ -117,6 +121,7 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo ) db, err := client.GetDbClient() if err != nil { + conf.Logger.Error("db err", zap.Error(err)) err = resp.DbConnectError.WithError(err) return } @@ -130,6 +135,30 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo return } + if taskExecLogRes.State == 0 { + redis, err := client.GetRedisClient() + if err != nil { + conf.Logger.Error("redis err", zap.Error(err)) + err = resp.RedisConnectError.ErrorDetail(err) + return + } + value, err := redis.HGet(conf.AutoExecHistory, cast.ToString(id)) + if err != nil { + conf.Logger.Error("redis HGet err", zap.Error(err)) + err = resp.RedisExecError.ErrorDetail(err) + return + } + var execCache ExecCache + err = json.Unmarshal([]byte(value), &execCache) + if err != nil { + conf.Logger.Error("redis value Unmarshal err", zap.Error(err)) + err = resp.FAIL.WithError(err) + return + } + taskExecLogRes.ExecLog = execCache.ExecLog + taskExecLogRes.ExecEndTime = execCache.ExecEndTime + } + //获取执行耗时 start, err = time.Parse("2006-01-02 15:04:05", taskExecLogRes.ExecStartTime.Format("2006-01-02 15:04:05")) if err != nil { diff --git a/src/service/task_manage.go b/src/service/task_manage.go index f60de1f..a9e1787 100644 --- a/src/service/task_manage.go +++ b/src/service/task_manage.go @@ -4,6 +4,7 @@ import ( "bufio" "errors" "fmt" + json "github.com/json-iterator/go" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" @@ -169,10 +170,9 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int if req.HostGroupId != 0 { finder.Where("tm.host_group_id = ?", req.HostGroupId) } - finder.OrderBy("tm.id") //查询任务 total, err = finder.Select("tm.id,tm.task_name,tm.task_desc,(select count(*) from task_history th "+ - "where th.task_id = tm.id) as exec_cnt,tm.create_user,tm.create_time").OrderBy("tm.create_time"). + "where th.task_id = tm.id) as exec_cnt,tm.create_user,tm.create_time").OrderBy("tm.create_time desc"). Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&taskManageListRes) if err != nil { err = resp.DbSelectError.WithError(err) @@ -354,6 +354,11 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id return } +type ExecCache struct { + ExecLog string `json:"exec_log"` + ExecEndTime time.Time `json:"exec_end_time"` // 执行结束时间 +} + // ExecAnsible 执行ansible命令 func ExecAnsible(id, taskId int, value string) { var cmd *exec.Cmd @@ -388,11 +393,12 @@ func ExecAnsible(id, taskId int, value string) { readerr := bufio.NewReader(stderr) var out, outErr int - var execLog string - + //var execLog string + var execObj []byte + var execCache ExecCache redis, err := client.GetRedisClient() if err != nil { - zap.L().Error(err.Error()) + conf.Logger.Error("redis err", zap.Error(err)) } for { //逐行输出日志 @@ -401,11 +407,17 @@ func ExecAnsible(id, taskId int, value string) { out = 1 } else if out == 0 { //存储执行日志 - execLog = execLog + lineOut + " \n " + //execLog = execLog + lineOut + " \n " + execCache.ExecLog = execCache.ExecLog + lineOut + " \n " + execCache.ExecEndTime = time.Now() + execObj, err = json.Marshal(execCache) + if err != nil { + conf.Logger.Error("lineOut Marshal execCache err", zap.Error(err)) + } // 写缓存 - err = redis.HSet(conf.AutoExecHistory, strconv.Itoa(id), execLog) + err = redis.HSet(conf.AutoExecHistory, strconv.Itoa(id), fmt.Sprintf("%s", execObj)) if err != nil { - conf.Logger.Error("Store Execution Log", zap.Error(err)) + conf.Logger.Error("Store Execution Log err", zap.Error(err)) } } @@ -414,11 +426,17 @@ func ExecAnsible(id, taskId int, value string) { outErr = 1 } else if outErr == 0 { //存储异常执行日志 - execLog = execLog + lineErr + " \n " + //execLog = execLog + lineErr + " \n " + execCache.ExecLog = execCache.ExecLog + lineErr + " \n " + execCache.ExecEndTime = time.Now() + execObj, err = json.Marshal(execCache) + if err != nil { + conf.Logger.Error("lineErr Marshal execCache err", zap.Error(err)) + } // 写缓存 - err = redis.HSet(conf.AutoExecHistory, strconv.Itoa(id), execLog) + err = redis.HSet(conf.AutoExecHistory, strconv.Itoa(id), fmt.Sprintf("%s", execObj)) if err != nil { - conf.Logger.Error("Store Execution Log", zap.Error(err)) + conf.Logger.Error("Store Execution Log err", zap.Error(err)) } } if out == 1 && outErr == 1 { @@ -431,7 +449,7 @@ func ExecAnsible(id, taskId int, value string) { //任务执行成功 err = UpdateExecHistory(request.UpdateExecHistory{ TaskHistoryId: id, - ExecLog: execLog, + ExecLog: execCache.ExecLog, State: 1, }) if err != nil { @@ -443,11 +461,11 @@ func ExecAnsible(id, taskId int, value string) { //任务执行失败 err = UpdateExecHistory(request.UpdateExecHistory{ TaskHistoryId: id, - ExecLog: execLog, + ExecLog: execCache.ExecLog, State: 2, }) if err != nil { - conf.Logger.Error("Modify Execution Status", zap.Error(err)) + conf.Logger.Error("Modify Execution Status err", zap.Error(err)) //return } redis.HDel(conf.AutoExecHistory, strconv.Itoa(id)) diff --git a/src/service/work_order.go b/src/service/work_order.go index dbac0a8..84e9cb3 100644 --- a/src/service/work_order.go +++ b/src/service/work_order.go @@ -263,7 +263,7 @@ func (w *WorkOrderManageSvc) ListWorkOrderManage(req request.ListWorkOrderManage if req.CreateDateTo != "" { finder.Where("create_time <= ?", req.CreateDateTo) } - finder.OrderBy("create_time") + finder.OrderBy("create_time desc") //查询任务 total, err = finder.Select("id,order_name,order_level,order_cnt,push_obj,timing_type,timing_state,create_user,create_time"). Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&workOrderListRes) @@ -456,7 +456,7 @@ func (w *WorkOrderManageSvc) ListWorkOrderIssuance(req request.ListWorkOrderReq) if req.Id != 0 { finder.Where("wom.id = ?", req.Id) } - finder.OrderBy("woi.create_time") + finder.OrderBy("woi.create_time desc") //查询任务 total, err = finder.Select("woi.\"id\",woi.order_id,wom.order_name,wom.order_level,woi.order_state,woi.push_obj,woi.create_user,woi.create_time,woi.complete_time,"+ "(select count(1) from work_order_me wome where wome.order_issuance_id = woi.id and wome.order_state = woi.order_state) as state_cnt,"+ @@ -615,7 +615,7 @@ func (w *WorkOrderManageSvc) ListWorkOrderMe(req request.ListWorkOrderReq) (tota if req.CompleteTimeTo != "" { finder.Where("wome.complete_time <= ?", req.CompleteTimeTo) } - finder.OrderBy("woi.create_time") + finder.OrderBy("woi.create_time desc") //查询任务 total, err = finder.Select("wome.\"id\",wome.order_id,wome.order_issuance_id,wom.order_name,wom.order_level,"+ "wome.order_state,woi.push_obj,woi.create_user,woi.create_time,wome.complete_time"). -- 2.26.0