fix: fix issue with agent cpu usage error (#8792)

Refs https://github.com/1Panel-dev/1Panel/issues/8742
This commit is contained in:
CityFun 2025-05-23 14:50:12 +08:00 committed by GitHub
parent 28ab4e2527
commit 433a911ef6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 28 additions and 18 deletions

View file

@ -2,6 +2,8 @@ package cmd
import (
"bytes"
"context"
"errors"
"fmt"
"log"
"os"
@ -89,7 +91,17 @@ func (c *CommandHelper) RunWithStdoutBashCf(command string, arg ...interface{})
}
func (c *CommandHelper) run(name string, arg ...string) (string, error) {
cmd := exec.Command(name, arg...)
var cmd *exec.Cmd
var ctx context.Context
var cancel context.CancelFunc
if c.timeout != 0 {
ctx, cancel = context.WithTimeout(context.Background(), c.timeout)
defer cancel()
cmd = exec.CommandContext(ctx, name, arg...)
} else {
cmd = exec.Command(name, arg...)
}
customWriter := &CustomWriter{taskItem: c.taskItem}
var stdout, stderr bytes.Buffer
@ -122,25 +134,15 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
}
if c.timeout != 0 {
if err := cmd.Start(); err != nil {
return "", err
err := cmd.Run()
if c.taskItem != nil {
customWriter.Flush()
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
if c.taskItem != nil {
customWriter.Flush()
}
}()
after := time.After(c.timeout)
select {
case <-after:
_ = cmd.Process.Kill()
if ctx != nil && errors.Is(ctx.Err(), context.DeadlineExceeded) {
return "", buserr.New("ErrCmdTimeout")
case err := <-done:
if err != nil {
return handleErr(stdout, stderr, err)
}
}
if err != nil {
return handleErr(stdout, stderr, err)
}
return stdout.String(), nil
}

View file

@ -2821,6 +2821,7 @@ const message = {
statusCode: 'Status code',
manage: 'Management',
autoRestart: 'Auto Restart',
EXITED: 'Exited',
},
},
xpack: {

View file

@ -2687,6 +2687,7 @@ const message = {
statusCode: 'ステータスコード',
manage: '管理',
autoRestart: '自動再起動',
EXITED: '終了しました',
},
},
xpack: {

View file

@ -2643,6 +2643,7 @@ const message = {
statusCode: '상태 코드',
manage: '관리',
autoRestart: '자동 재시작',
EXITED: '종료됨',
},
},
xpack: {

View file

@ -2747,6 +2747,7 @@ const message = {
statusCode: 'Kod status',
manage: 'Pengurusan',
autoRestart: 'Auto Restart',
EXITED: 'Telah keluar',
},
},
xpack: {

View file

@ -2747,6 +2747,7 @@ const message = {
statusCode: 'Código de status',
manage: 'Gerenciamento',
autoRestart: 'Reinicialização Automática',
EXITED: 'Saiu',
},
},
xpack: {

View file

@ -2740,6 +2740,7 @@ const message = {
statusCode: 'Код статуса',
manage: 'Управление',
autoRestart: 'Автоматический перезапуск',
EXITED: 'Вышел',
},
},
xpack: {

View file

@ -2611,6 +2611,7 @@ const message = {
statusCode: '狀態碼',
manage: '管理',
autoRestart: '自動重啟',
EXITED: '已退出',
},
},
xpack: {

View file

@ -2602,6 +2602,7 @@ const message = {
statusCode: '状态码',
manage: '管理',
autoRestart: '自动重启',
EXITED: '已退出',
},
},
xpack: {