Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
so-operation-api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
smart-operation
so-operation-api
Commits
7a49adf2
Commit
7a49adf2
authored
Jul 19, 2023
by
陈子龙
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
执行ansible命令日志存储缓存
parent
996781d8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
22 deletions
+60
-22
src/common/conf/options.go
src/common/conf/options.go
+1
-0
src/service/task_history.go
src/service/task_history.go
+57
-20
src/service/task_manage.go
src/service/task_manage.go
+2
-2
No files found.
src/common/conf/options.go
View file @
7a49adf2
...
...
@@ -62,4 +62,5 @@ const (
LocalDateTimeFormat
string
=
"2006-01-02 15:04:05"
FinalHeartBeatUnixKey
=
"FinalHeartBeatUnix"
AutoExecHistory
=
"AutoExecHistory"
TaskExecLog
=
"TaskExecLog"
)
src/service/task_history.go
View file @
7a49adf2
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
,
err
2
:=
redis
.
HExists
(
conf
.
AutoExecHistory
,
cast
.
ToString
(
id
))
if
err
2
!=
nil
{
conf
.
Logger
.
Error
(
"redis HExists err"
,
zap
.
Error
(
err
2
))
err
=
resp
.
RedisExecError
.
ErrorDetail
(
err
2
)
b1
,
err
1
:=
redis
.
HExists
(
conf
.
AutoExecHistory
,
cast
.
ToString
(
id
))
if
err
1
!=
nil
{
conf
.
Logger
.
Error
(
"redis HExists err"
,
zap
.
Error
(
err
1
))
err
=
resp
.
RedisExecError
.
ErrorDetail
(
err
1
)
}
if
!
b1
{
return
}
//获取key
value
,
err
3
:=
redis
.
HGet
(
conf
.
AutoExecHistory
,
cast
.
ToString
(
id
))
if
err
3
!=
nil
{
conf
.
Logger
.
Error
(
"redis HGet err"
,
zap
.
Error
(
err
3
))
err
=
resp
.
RedisExecError
.
ErrorDetail
(
err
3
)
value
,
err
2
:=
redis
.
HGet
(
conf
.
AutoExecHistory
,
cast
.
ToString
(
id
))
if
err
2
!=
nil
{
conf
.
Logger
.
Error
(
"redis HGet err"
,
zap
.
Error
(
err
2
))
err
=
resp
.
RedisExecError
.
ErrorDetail
(
err
2
)
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
}
...
...
src/service/task_manage.go
View file @
7a49adf2
...
...
@@ -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
))
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment