Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Lei Li
public
Commits
544ad0ec
Commit
544ad0ec
authored
Oct 15, 2024
by
Lei Li
Browse files
feat: 增加升级失败回滚操作
parent
a3280358
Changes
5
Show whitespace changes
Inline
Side-by-side
.idea/.gitignore
0 → 100644
View file @
544ad0ec
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
.idea/modules.xml
0 → 100644
View file @
544ad0ec
<?xml version="1.0" encoding="UTF-8"?>
<project
version=
"4"
>
<component
name=
"ProjectModuleManager"
>
<modules>
<module
fileurl=
"file://$PROJECT_DIR$/.idea/public.iml"
filepath=
"$PROJECT_DIR$/.idea/public.iml"
/>
</modules>
</component>
</project>
\ No newline at end of file
.idea/public.iml
0 → 100644
View file @
544ad0ec
<?xml version="1.0" encoding="UTF-8"?>
<module
type=
"WEB_MODULE"
version=
"4"
>
<component
name=
"Go"
enabled=
"true"
/>
<component
name=
"NewModuleRootManager"
>
<content
url=
"file://$MODULE_DIR$"
/>
<orderEntry
type=
"inheritedJdk"
/>
<orderEntry
type=
"sourceFolder"
forTests=
"false"
/>
</component>
</module>
\ No newline at end of file
.idea/vcs.xml
0 → 100644
View file @
544ad0ec
<?xml version="1.0" encoding="UTF-8"?>
<project
version=
"4"
>
<component
name=
"VcsDirectoryMappings"
>
<mapping
directory=
""
vcs=
"Git"
/>
</component>
</project>
\ No newline at end of file
lib/monitor_daemon/monitor_daemon.go
View file @
544ad0ec
...
...
@@ -14,6 +14,7 @@ import (
"time"
"linkfog.com/public/lib/common"
"linkfog.com/public/lib/file"
"linkfog.com/public/lib/l"
)
...
...
@@ -43,6 +44,8 @@ type MonitorDaemon struct {
restartInterval
*
restartInterval
cmdline
string
childPid
int
agentMd5
string
cmdline0
string
isProcAbnormalCallback
IsProcAbnormalCallback
}
...
...
@@ -146,8 +149,16 @@ func New(cmdline string, opts ...MonitorDaemonOpt) (*MonitorDaemon, error) {
opt
(
&
m
)
}
m
.
cmdline0
=
strings
.
Split
(
m
.
cmdline
,
" "
)[
0
]
md5
,
err
:=
file
.
GetFileMD5
(
m
.
cmdline0
)
if
err
!=
nil
{
l
.
Errorf
(
"calculation of agent MD5(%s) failed err:%s"
,
m
.
cmdline0
,
err
)
}
else
{
m
.
agentMd5
=
md5
}
//第一次启动进程
err
:
=
m
.
startProcess
()
err
=
m
.
startProcess
()
if
err
!=
nil
{
l
.
Errorf
(
"start child process failed %s err:%v"
,
cmdline
,
err
)
return
nil
,
err
...
...
@@ -243,29 +254,32 @@ func (m *MonitorDaemon) runWithSignal(sigs []os.Signal) {
}
if
!
isAlive
&&
m
.
restartInterval
.
isNeedStart
()
{
//忽略错误,只打印日志
err
:=
m
.
startProcess
()
md5
,
err
:=
file
.
GetFileMD5
(
m
.
cmdline0
)
if
err
!=
nil
{
l
.
Errorf
(
"calculation of agent MD5(%s) failed err:%s"
,
m
.
cmdline0
,
err
)
}
err
=
m
.
startProcess
()
if
err
!=
nil
{
// 异常情况,直接无法启动,进行回滚操作
errRetryTimes
++
l
.
Errorf
(
"monitorProcess start child process failed %s, errRetryTimes:%d, err:%v"
,
m
.
cmdline
,
errRetryTimes
,
err
)
if
errRetryTimes
>
m
.
maxRetryTimes
{
if
_
,
err
:=
os
.
Stat
(
m
.
agentBackupPath
);
err
==
nil
||
!
os
.
IsNotExist
(
err
)
{
agentName
:=
strings
.
Split
(
m
.
cmdline
,
" "
)[
0
]
if
agentName
!=
""
{
err
:=
os
.
Rename
(
m
.
agentBackupPath
,
agentName
)
if
err
!=
nil
{
l
.
Errorf
(
"monitorProcess rollback agent process failed err:%v"
,
err
)
}
else
{
if
md5
!=
m
.
agentMd5
&&
errRetryTimes
>
m
.
maxRetryTimes
{
m
.
rollbackForAbnormalCase
()
errRetryTimes
=
0
}
}
}
else
{
if
md5
==
m
.
agentMd5
{
errRetryTimes
++
// 异常情况,启动几秒后停止,进行回滚操作
if
errRetryTimes
>
m
.
maxRetryTimes
{
m
.
rollbackForAbnormalCase
()
errRetryTimes
=
0
}
}
}
else
{
errRetryTimes
=
0
l
.
Infof
(
"monitorProcess start child process success pid: %d"
,
m
.
childPid
)
m
.
agentMd5
=
md5
}
l
.
Infof
(
"monitorProcess start child process success pid: %d, errRetryTimes:%d"
,
m
.
childPid
,
errRetryTimes
)
}
m
.
restartInterval
.
setLastStart
()
}
...
...
@@ -435,3 +449,21 @@ func readStderr(stderrReader io.Reader) {
}
}
func
(
m
*
MonitorDaemon
)
rollbackForAbnormalCase
()
{
if
_
,
err
:=
os
.
Stat
(
m
.
agentBackupPath
);
err
==
nil
||
!
os
.
IsNotExist
(
err
)
{
if
m
.
cmdline0
!=
""
{
err
:=
os
.
Rename
(
m
.
agentBackupPath
,
m
.
cmdline0
)
if
err
!=
nil
{
l
.
Errorf
(
"monitorProcess rollback agent process failed err:%v"
,
err
)
}
else
{
md5
,
err
:=
file
.
GetFileMD5
(
m
.
cmdline0
)
if
err
!=
nil
{
l
.
Errorf
(
"calculation of agent MD5(%s) failed err:%s"
,
m
.
cmdline0
,
err
)
}
else
{
m
.
agentMd5
=
md5
}
l
.
Infof
(
"monitorProcess rollback agent process successful md5:%s"
,
md5
)
}
}
}
}
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