Skip to content

Commit 4cfc343

Browse files
authored
fix: Modify the recommended application synchronization mode (#8050)
1 parent adbf44a commit 4cfc343

File tree

24 files changed

+88
-106
lines changed

24 files changed

+88
-106
lines changed

agent/app/api/v2/dashboard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (b *BaseApi) LoadAppLauncherOption(c *gin.Context) {
6262
}
6363

6464
func (b *BaseApi) SyncAppLauncher(c *gin.Context) {
65-
var req dto.SyncFromMaster
65+
var req []dto.AppLauncherSync
6666
if err := helper.CheckBindAndValidate(&req, c); err != nil {
6767
return
6868
}

agent/app/dto/dashboard.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ type DashboardCurrent struct {
100100
ShotTime time.Time `json:"shotTime"`
101101
}
102102

103+
type AppLauncherSync struct {
104+
Key string `json:"key"`
105+
}
106+
103107
type DiskInfo struct {
104108
Path string `json:"path"`
105109
Type string `json:"type"`

agent/app/repo/task.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package repo
22

33
import (
44
"context"
5+
56
"github.com/1Panel-dev/1Panel/agent/constant"
67
"github.com/1Panel-dev/1Panel/agent/global"
78

@@ -19,6 +20,7 @@ type ITaskRepo interface {
1920
Update(ctx context.Context, task *model.Task) error
2021
UpdateRunningTaskToFailed() error
2122
CountExecutingTask() (int64, error)
23+
Delete(opts ...DBOption) error
2224

2325
WithByID(id string) DBOption
2426
WithResourceID(id uint) DBOption
@@ -108,3 +110,11 @@ func (t TaskRepo) CountExecutingTask() (int64, error) {
108110
err := getTaskDb(t.WithByStatus(constant.StatusExecuting)).Model(&model.Task{}).Count(&count).Error
109111
return count, err
110112
}
113+
114+
func (u TaskRepo) Delete(opts ...DBOption) error {
115+
db := global.TaskDB
116+
for _, opt := range opts {
117+
db = opt(db)
118+
}
119+
return db.Delete(&model.Task{}).Error
120+
}

agent/app/service/backup_record.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,13 @@ func (u *BackupRecordService) LoadRecordSize(req dto.SearchForSize) ([]dto.Recor
251251
var datas []dto.RecordFileSize
252252
var wg sync.WaitGroup
253253
for i := 0; i < len(list); i++ {
254-
item := dto.RecordFileSize{ID: list[i].ID}
254+
datas = append(datas, dto.RecordFileSize{ID: list[i].ID})
255255
if val, ok := clientMap[fmt.Sprintf("%v", list[i].DownloadID)]; ok {
256256
wg.Add(1)
257257
go func(index int) {
258-
item.Size, _ = val.client.Size(path.Join(val.backupPath, list[i].FilePath))
259-
datas = append(datas, item)
258+
datas[index].Size, _ = val.client.Size(path.Join(val.backupPath, list[i].FilePath))
260259
wg.Done()
261260
}(i)
262-
} else {
263-
datas = append(datas, item)
264261
}
265262
}
266263
wg.Wait()

agent/app/service/dashboard.go

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@ package service
22

33
import (
44
"encoding/json"
5-
"fmt"
65
network "net"
76
"os"
87
"sort"
98
"strings"
109
"sync"
1110
"time"
1211

13-
"github.com/1Panel-dev/1Panel/agent/app/repo"
14-
"github.com/1Panel-dev/1Panel/agent/buserr"
15-
1612
"github.com/1Panel-dev/1Panel/agent/app/dto"
1713
"github.com/1Panel-dev/1Panel/agent/app/model"
1814
"github.com/1Panel-dev/1Panel/agent/constant"
@@ -33,7 +29,7 @@ import (
3329
type DashboardService struct{}
3430

3531
type IDashboardService interface {
36-
Sync(req dto.SyncFromMaster) error
32+
Sync(req []dto.AppLauncherSync) error
3733

3834
LoadOsInfo() (*dto.OsInfo, error)
3935
LoadBaseInfo(ioOption string, netOption string) (*dto.DashboardBase, error)
@@ -48,33 +44,12 @@ func NewIDashboardService() IDashboardService {
4844
return &DashboardService{}
4945
}
5046

51-
func (u *DashboardService) Sync(req dto.SyncFromMaster) error {
52-
var launcherItem model.AppLauncher
53-
if err := json.Unmarshal([]byte(req.Data), &launcherItem); err != nil {
54-
return err
55-
}
56-
launcher, _ := launcherRepo.Get(settingRepo.WithByKey(req.Name))
57-
switch req.Operation {
58-
case "create":
59-
if launcher.ID != 0 {
60-
launcherItem.ID = launcher.ID
61-
return launcherRepo.Save(&launcherItem)
62-
}
63-
return launcherRepo.Create(&launcherItem)
64-
case "delete":
65-
if launcher.ID == 0 {
66-
return buserr.New("ErrRecordNotFound")
67-
}
68-
return launcherRepo.Delete(repo.WithByID(launcher.ID))
69-
case "update":
70-
if launcher.ID == 0 {
71-
return buserr.New("ErrRecordNotFound")
72-
}
73-
launcherItem.ID = launcher.ID
74-
return launcherRepo.Save(&launcherItem)
75-
default:
76-
return fmt.Errorf("not support such operation %s", req.Operation)
47+
func (u *DashboardService) Sync(req []dto.AppLauncherSync) error {
48+
var launchers []model.AppLauncher
49+
for _, item := range req {
50+
launchers = append(launchers, model.AppLauncher{Key: item.Key})
7751
}
52+
return launcherRepo.SyncAll(launchers)
7853
}
7954

8055
func (u *DashboardService) LoadOsInfo() (*dto.OsInfo, error) {

agent/app/service/device_clean.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/docker/docker/api/types/filters"
1717

1818
"github.com/1Panel-dev/1Panel/agent/app/dto"
19+
"github.com/1Panel-dev/1Panel/agent/app/repo"
1920
"github.com/1Panel-dev/1Panel/agent/app/task"
2021
"github.com/1Panel-dev/1Panel/agent/global"
2122
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
@@ -37,7 +38,6 @@ const (
3738
uploadPath = "1panel/uploads"
3839
downloadPath = "1panel/download"
3940
logPath = "1panel/log"
40-
taskPath = "1panel/task"
4141
)
4242

4343
func (u *DeviceService) Scan() dto.CleanData {
@@ -254,18 +254,10 @@ func (u *DeviceService) Clean(req []dto.Clean) {
254254
dropFileOrDir(path.Join(global.Dir.BaseDir, logPath, item.Name))
255255
}
256256
case "task_log":
257-
pathItem := path.Join(global.Dir.BaseDir, taskPath, item.Name)
258-
dropFileOrDir(path.Join(global.Dir.BaseDir, taskPath, item.Name))
257+
pathItem := path.Join(global.Dir.BaseDir, logPath, item.Name)
258+
dropFileOrDir(pathItem)
259259
if len(item.Name) == 0 {
260-
files, _ := os.ReadDir(pathItem)
261-
if len(files) == 0 {
262-
continue
263-
}
264-
for _, file := range files {
265-
_ = cronjobRepo.DeleteRecord(cronjobRepo.WithByRecordFile(path.Join(pathItem, file.Name())))
266-
}
267-
} else {
268-
_ = cronjobRepo.DeleteRecord(cronjobRepo.WithByRecordFile(pathItem))
260+
_ = taskRepo.Delete(repo.WithByType(item.Name))
269261
}
270262
case "images":
271263
dropImages()
@@ -506,8 +498,8 @@ func loadLogTree(fileOp fileUtils.FileOp) []dto.CleanTree {
506498
}
507499
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "system_log", Size: uint64(size), Children: list1, Type: "system_log", IsRecommend: true})
508500

509-
path2 := path.Join(global.Dir.BaseDir, taskPath)
510-
list2 := loadTreeWithAllFile(false, path2, "task_log", path2, fileOp)
501+
path2 := path.Join(global.Dir.BaseDir, logPath)
502+
list2 := loadTreeWithDir(false, "task_log", path2, fileOp)
511503
size2, _ := fileOp.GetDirSize(path2)
512504
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "task_log", Size: uint64(size2), Children: list2, Type: "task_log"})
513505
return treeData
@@ -570,6 +562,9 @@ func loadTreeWithDir(isCheck bool, treeType, pathItem string, fileOp fileUtils.F
570562
if (treeType == "old_upgrade" || treeType == "upgrade") && !strings.HasPrefix(file.Name(), "upgrade_2023") {
571563
continue
572564
}
565+
if treeType == "task_log" && file.Name() == "ssl" {
566+
continue
567+
}
573568
if file.IsDir() {
574569
size, err := fileOp.GetDirSize(path.Join(pathItem, file.Name()))
575570
if err != nil {

core/app/service/app_launcher.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/1Panel-dev/1Panel/core/app/model"
1010
"github.com/1Panel-dev/1Panel/core/app/repo"
1111
"github.com/1Panel-dev/1Panel/core/constant"
12+
"github.com/1Panel-dev/1Panel/core/utils/req_helper"
1213
"github.com/1Panel-dev/1Panel/core/utils/xpack"
1314
)
1415

@@ -37,32 +38,23 @@ func (u *LauncherService) Search() ([]string, error) {
3738

3839
func (u *LauncherService) ChangeShow(req dto.SettingUpdate) error {
3940
launcher, _ := launcherRepo.Get(repo.WithByKey(req.Key))
40-
if req.Value == constant.StatusEnable {
41-
if launcher.ID != 0 {
42-
go syncLauncherToAgent(launcher, "create")
43-
return nil
44-
}
45-
launcher.Key = req.Key
46-
if err := launcherRepo.Create(&launcher); err != nil {
41+
if req.Value == constant.StatusEnable && launcher.ID == 0 {
42+
if err := launcherRepo.Create(&model.AppLauncher{Key: req.Key}); err != nil {
4743
return err
4844
}
49-
go syncLauncherToAgent(launcher, "create")
50-
return nil
51-
}
52-
if launcher.ID == 0 {
53-
go syncLauncherToAgent(launcher, "delete")
54-
return nil
5545
}
56-
if err := launcherRepo.Delete(repo.WithByKey(req.Key)); err != nil {
57-
return err
46+
if req.Value == constant.StatusDisable && launcher.ID != 0 {
47+
if err := launcherRepo.Delete(repo.WithByKey(req.Key)); err != nil {
48+
return err
49+
}
5850
}
59-
go syncLauncherToAgent(launcher, "delete")
51+
go syncLauncherToAgent()
6052
return nil
6153
}
6254

63-
func syncLauncherToAgent(launcher model.AppLauncher, operation string) {
64-
itemData, _ := json.Marshal(launcher)
65-
itemJson := dto.SyncToAgent{Name: launcher.Key, Operation: operation, Data: string(itemData)}
66-
bodyItem, _ := json.Marshal(itemJson)
67-
_ = xpack.RequestToAllAgent("/api/v2/backups/sync", http.MethodPost, bytes.NewReader((bodyItem)))
55+
func syncLauncherToAgent() {
56+
launchers, _ := launcherRepo.List()
57+
itemData, _ := json.Marshal(launchers)
58+
_, _ = req_helper.NewLocalClient("/api/v2/dashboard/app/launcher/sync", http.MethodPost, bytes.NewReader((itemData)))
59+
_ = xpack.RequestToAllAgent("/api/v2/dashboard/app/launcher/sync", http.MethodPost, bytes.NewReader((itemData)))
6860
}

frontend/src/components/status/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const getType = (status: string) => {
6363
case 'disable':
6464
case 'unhealthy':
6565
case 'failed':
66+
case 'lost':
6667
return 'danger';
6768
case 'paused':
6869
case 'exited':

frontend/src/lang/modules/en.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3185,9 +3185,8 @@ const message = {
31853185
'Detected that there is already 1panel service on this node. Adding this node will use the original service port and installation directory of 1panel. Do you want to continue?',
31863186
coreExist:
31873187
'Detected that there is already 1panel-core service on this node. Unable to add this node, please check and try again!',
3188-
agentExist: 'Detected that there is already 1panel-agent service on this node',
3189-
forceAdd: 'Force Add',
3190-
forceAddHelper: 'Force add will forcibly replace the 1panel-agent service on this node',
3188+
agentExist:
3189+
'Detected that the 1panel-agent service already exists on this node. Continuing to add will retain the node data and only replace the 1panel-agent service. Do you want to continue?',
31913190
reinstallHelper: 'Reinstall node {0}, do you want to continue?',
31923191
unhealthyCheck: 'Abnormal Check',
31933192
fixOperation: 'Fix Operation',

frontend/src/lang/modules/ja.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2997,9 +2997,8 @@ const message = {
29972997
'このノードに既に1panelサービスが存在します。このノードを追加すると、1panelの元のサービスポートとインストールディレクトリが使用されます。続行しますか?',
29982998
coreExist:
29992999
'このノードに既に1panel-coreサービスが存在します。このノードを追加できません。確認して再試行してください!',
3000-
agentExist: 'このノードに既に1panel-agentサービスが存在します',
3001-
forceAdd: '強制追加',
3002-
forceAddHelper: '強制追加は、このノードの1panel-agentサービスを強制的に置き換えます',
3000+
agentExist:
3001+
'このノードに1panel-agentサービスが既に存在することが検出されました。追加を続行すると、ノードデータは保持され、1panel-agentサービスのみが置き換えられます。続行しますか?',
30033002
reinstallHelper: 'ノード{0}を再インストールします。続行しますか?',
30043003
unhealthyCheck: '異常チェック',
30053004
fixOperation: '修正操作',

0 commit comments

Comments
 (0)