1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 08:19:15 +08:00

fix: 解决清空容器日志导致的 CPU 占用异常问题 (#2019)

Refs #1999
This commit is contained in:
ssongliu 2023-08-21 17:00:12 +08:00 committed by GitHub
parent fe7fed02ee
commit 24c87905b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 5 deletions

View File

@ -531,11 +531,15 @@ func (u *ContainerService) ContainerLogClean(req dto.OperationWithName) error {
if err != nil {
return err
}
container, err := client.ContainerInspect(context.Background(), req.Name)
ctx := context.Background()
containerItem, err := client.ContainerInspect(ctx, req.Name)
if err != nil {
return err
}
file, err := os.OpenFile(container.LogPath, os.O_RDWR|os.O_CREATE, 0666)
if err := client.ContainerStop(ctx, containerItem.ID, container.StopOptions{}); err != nil {
return err
}
file, err := os.OpenFile(containerItem.LogPath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}
@ -545,10 +549,14 @@ func (u *ContainerService) ContainerLogClean(req dto.OperationWithName) error {
}
_, _ = file.Seek(0, 0)
files, _ := filepath.Glob(fmt.Sprintf("%s.*", container.LogPath))
files, _ := filepath.Glob(fmt.Sprintf("%s.*", containerItem.LogPath))
for _, file := range files {
_ = os.Remove(file)
}
if err := client.ContainerStart(ctx, containerItem.ID, types.ContainerStartOptions{}); err != nil {
return err
}
return nil
}

View File

@ -130,7 +130,7 @@ const acceptParams = (props: DialogProps): void => {
};
const onClean = async () => {
ElMessageBox.confirm(i18n.global.t('commons.msg.clean'), i18n.global.t('container.cleanLog'), {
ElMessageBox.confirm(i18n.global.t('container.cleanLogHelper'), i18n.global.t('container.cleanLog'), {
confirmButtonText: i18n.global.t('commons.button.confirm'),
cancelButtonText: i18n.global.t('commons.button.cancel'),
type: 'info',

View File

@ -507,6 +507,8 @@ const message = {
lastHour: 'Last Hour',
last10Min: 'Last 10 Minutes',
cleanLog: 'Clean log',
cleanLogHelper:
'Clearing logs requires restarting the container, and this operation cannot be rolled back. Do you want to continue?',
newName: 'New name',
source: 'Resource rate',
cpuShare: 'CPU Share',

View File

@ -496,6 +496,7 @@ const message = {
lastHour: '最近 1 小時',
last10Min: '最近 10 分鐘',
cleanLog: '清空日誌',
cleanLogHelper: '清空日誌需要重啟容器該操作無法回滾是否繼續',
newName: '新名稱',
source: '資源使用率',
cpuShare: 'CPU 權重',

View File

@ -496,6 +496,7 @@ const message = {
lastHour: '最近 1 小时',
last10Min: '最近 10 分钟',
cleanLog: '清空日志',
cleanLogHelper: '清空日志需要重启容器该操作无法回滚是否继续',
newName: '新名称',
source: '资源使用率',
cpuShare: 'CPU 权重',

View File

@ -161,7 +161,7 @@ const onDownload = async () => {
};
const onClean = async () => {
ElMessageBox.confirm(i18n.global.t('commons.msg.clean'), i18n.global.t('container.cleanLog'), {
ElMessageBox.confirm(i18n.global.t('container.cleanLogHelper'), i18n.global.t('container.cleanLog'), {
confirmButtonText: i18n.global.t('commons.button.confirm'),
cancelButtonText: i18n.global.t('commons.button.cancel'),
type: 'info',