Skip to content

Commit 1025cd3

Browse files
authored
refactor: improve code logic and format in app remote sync task (#11691)
* feat: Implement AppStore synchronization task - Introduced a new `syncAppStoreTask` function to handle the synchronization of applications from the AppStore. - The function checks for updates, retrieves the application list, and manages the synchronization process, including downloading app icons and updating app details. - Refactored the existing app synchronization logic from `SyncAppListFromRemote` into the new task for improved maintainability and clarity. - Added logging for progress tracking during the synchronization process. * feat: Refactor HTTP request handling to support custom client - Introduced `HandleRequestWithClient` function to allow custom HTTP clients for requests. - Moved the transport loading logic into the new function for better separation of concerns. - Updated `HandleRequest` to utilize the new function, enhancing flexibility in request handling. * refactor: Simplify HTTP request handling in app synchronization - Replaced direct HTTP request handling with `HandleRequestWithClient` for downloading app icons and Docker Compose files. - Removed unused variables and logging related to icon download updates for cleaner code. - Improved error handling and logging for failed requests.
1 parent de402ea commit 1025cd3

4 files changed

Lines changed: 371 additions & 245 deletions

File tree

agent/app/service/app.go

Lines changed: 1 addition & 241 deletions
Original file line numberDiff line numberDiff line change
@@ -925,247 +925,7 @@ func (a AppService) SyncAppListFromRemote(taskID string) (err error) {
925925
if err != nil {
926926
return err
927927
}
928-
syncTask.AddSubTask(task.GetTaskName(i18n.GetMsgByKey("App"), task.TaskSync, task.TaskScopeAppStore), func(t *task.Task) (err error) {
929-
updateRes, err := a.GetAppUpdate()
930-
if err != nil {
931-
return err
932-
}
933-
if !updateRes.CanUpdate {
934-
if updateRes.IsSyncing {
935-
t.Log(i18n.GetMsgByKey("AppStoreIsSyncing"))
936-
return nil
937-
}
938-
t.Log(i18n.GetMsgByKey("AppStoreIsUpToDate"))
939-
return nil
940-
}
941-
list := &dto.AppList{}
942-
if updateRes.AppList == nil {
943-
list, err = getAppList()
944-
if err != nil {
945-
return err
946-
}
947-
} else {
948-
list = updateRes.AppList
949-
}
950-
settingService := NewISettingService()
951-
_ = settingService.Update("AppStoreSyncStatus", constant.StatusSyncing)
952-
953-
setting, err := settingService.GetSettingInfo()
954-
if err != nil {
955-
return err
956-
}
957-
var (
958-
appTags []*model.AppTag
959-
oldAppIds []uint
960-
)
961-
if err = SyncTags(list.Extra); err != nil {
962-
return err
963-
}
964-
deleteCustomApp()
965-
oldApps, err := appRepo.GetBy(appRepo.WithNotLocal())
966-
if err != nil {
967-
return err
968-
}
969-
for _, old := range oldApps {
970-
oldAppIds = append(oldAppIds, old.ID)
971-
}
972-
973-
baseRemoteUrl := fmt.Sprintf("%s/%s/1panel", global.CONF.RemoteURL.AppRepo, global.CONF.Base.Mode)
974-
975-
appsMap := getApps(oldApps, list.Apps, setting.SystemVersion, t)
976-
977-
t.LogStart(i18n.GetMsgByKey("SyncAppDetail"))
978-
for _, l := range list.Apps {
979-
app, ok := appsMap[l.AppProperty.Key]
980-
if !ok {
981-
continue
982-
}
983-
iconStr := ""
984-
_, iconRes, err := req_helper.HandleRequest(l.Icon, http.MethodGet, constant.TimeOut20s)
985-
if err == nil {
986-
if !strings.Contains(string(iconRes), "<xml>") {
987-
iconStr = base64.StdEncoding.EncodeToString(iconRes)
988-
}
989-
}
990-
app.Icon = iconStr
991-
app.TagsKey = l.AppProperty.Tags
992-
if l.AppProperty.Recommend > 0 {
993-
app.Recommend = l.AppProperty.Recommend
994-
} else {
995-
app.Recommend = 9999
996-
}
997-
app.ReadMe = l.ReadMe
998-
app.LastModified = l.LastModified
999-
versions := l.Versions
1000-
detailsMap := getAppDetails(app.Details, versions)
1001-
for _, v := range versions {
1002-
version := v.Name
1003-
detail := detailsMap[version]
1004-
versionUrl := fmt.Sprintf("%s/%s/%s", baseRemoteUrl, app.Key, version)
1005-
paramByte, _ := json.Marshal(v.AppForm)
1006-
var appForm dto.AppForm
1007-
_ = json.Unmarshal(paramByte, &appForm)
1008-
if appForm.SupportVersion > 0 && common.CompareVersion(strconv.FormatFloat(appForm.SupportVersion, 'f', -1, 64), setting.SystemVersion) {
1009-
delete(detailsMap, version)
1010-
continue
1011-
}
1012-
if _, ok := InitTypes[app.Type]; ok {
1013-
dockerComposeUrl := fmt.Sprintf("%s/%s", versionUrl, "docker-compose.yml")
1014-
_, composeRes, err := req_helper.HandleRequest(dockerComposeUrl, http.MethodGet, constant.TimeOut20s)
1015-
if err == nil {
1016-
detail.DockerCompose = string(composeRes)
1017-
}
1018-
} else {
1019-
detail.DockerCompose = ""
1020-
}
1021-
1022-
detail.Params = string(paramByte)
1023-
detail.DownloadUrl = fmt.Sprintf("%s/%s", versionUrl, app.Key+"-"+version+".tar.gz")
1024-
detail.DownloadCallBackUrl = v.DownloadCallBackUrl
1025-
detail.Update = true
1026-
detail.LastModified = v.LastModified
1027-
detailsMap[version] = detail
1028-
}
1029-
var newDetails []model.AppDetail
1030-
for _, detail := range detailsMap {
1031-
newDetails = append(newDetails, detail)
1032-
}
1033-
app.Details = newDetails
1034-
appsMap[l.AppProperty.Key] = app
1035-
}
1036-
t.LogSuccess(i18n.GetMsgByKey("SyncAppDetail"))
1037-
1038-
tags, _ := tagRepo.All()
1039-
var (
1040-
addAppArray []model.App
1041-
updateAppArray []model.App
1042-
deleteAppArray []model.App
1043-
deleteIds []uint
1044-
tagMap = make(map[string]uint, len(tags))
1045-
)
1046-
1047-
for _, v := range appsMap {
1048-
if v.ID == 0 {
1049-
addAppArray = append(addAppArray, v)
1050-
} else {
1051-
if v.Status == constant.AppTakeDown {
1052-
installs, _ := appInstallRepo.ListBy(context.Background(), appInstallRepo.WithAppId(v.ID))
1053-
if len(installs) > 0 {
1054-
updateAppArray = append(updateAppArray, v)
1055-
continue
1056-
}
1057-
deleteAppArray = append(deleteAppArray, v)
1058-
deleteIds = append(deleteIds, v.ID)
1059-
} else {
1060-
updateAppArray = append(updateAppArray, v)
1061-
}
1062-
}
1063-
}
1064-
1065-
tx, ctx := getTxAndContext()
1066-
defer func() {
1067-
if err != nil {
1068-
tx.Rollback()
1069-
return
1070-
}
1071-
}()
1072-
if len(addAppArray) > 0 {
1073-
if err = appRepo.BatchCreate(ctx, addAppArray); err != nil {
1074-
return
1075-
}
1076-
}
1077-
if len(deleteAppArray) > 0 {
1078-
if err = appRepo.BatchDelete(ctx, deleteAppArray); err != nil {
1079-
return
1080-
}
1081-
if err = appDetailRepo.DeleteByAppIds(ctx, deleteIds); err != nil {
1082-
return
1083-
}
1084-
}
1085-
for _, tag := range tags {
1086-
tagMap[tag.Key] = tag.ID
1087-
}
1088-
for _, update := range updateAppArray {
1089-
if err = appRepo.Save(ctx, &update); err != nil {
1090-
return
1091-
}
1092-
}
1093-
apps := append(addAppArray, updateAppArray...)
1094-
1095-
var (
1096-
addDetails []model.AppDetail
1097-
updateDetails []model.AppDetail
1098-
deleteDetails []model.AppDetail
1099-
)
1100-
for _, app := range apps {
1101-
for _, tag := range app.TagsKey {
1102-
tagId, ok := tagMap[tag]
1103-
if ok {
1104-
exist, _ := appTagRepo.GetFirst(ctx, appTagRepo.WithByTagID(tagId), appTagRepo.WithByAppID(app.ID))
1105-
if exist == nil {
1106-
appTags = append(appTags, &model.AppTag{
1107-
AppId: app.ID,
1108-
TagId: tagId,
1109-
})
1110-
}
1111-
}
1112-
}
1113-
for _, d := range app.Details {
1114-
d.AppId = app.ID
1115-
if d.ID == 0 {
1116-
addDetails = append(addDetails, d)
1117-
} else {
1118-
if d.Status == constant.AppTakeDown {
1119-
runtime, _ := runtimeRepo.GetFirst(ctx, runtimeRepo.WithDetailId(d.ID))
1120-
if runtime != nil {
1121-
updateDetails = append(updateDetails, d)
1122-
continue
1123-
}
1124-
installs, _ := appInstallRepo.ListBy(ctx, appInstallRepo.WithDetailIdsIn([]uint{d.ID}))
1125-
if len(installs) > 0 {
1126-
updateDetails = append(updateDetails, d)
1127-
continue
1128-
}
1129-
deleteDetails = append(deleteDetails, d)
1130-
} else {
1131-
updateDetails = append(updateDetails, d)
1132-
}
1133-
}
1134-
}
1135-
}
1136-
if len(addDetails) > 0 {
1137-
if err = appDetailRepo.BatchCreate(ctx, addDetails); err != nil {
1138-
return
1139-
}
1140-
}
1141-
if len(deleteDetails) > 0 {
1142-
if err = appDetailRepo.BatchDelete(ctx, deleteDetails); err != nil {
1143-
return
1144-
}
1145-
}
1146-
for _, u := range updateDetails {
1147-
if err = appDetailRepo.Update(ctx, u); err != nil {
1148-
return
1149-
}
1150-
}
1151-
1152-
if len(oldAppIds) > 0 {
1153-
if err = appTagRepo.DeleteByAppIds(ctx, deleteIds); err != nil {
1154-
return
1155-
}
1156-
}
1157-
1158-
if len(appTags) > 0 {
1159-
if err = appTagRepo.BatchCreate(ctx, appTags); err != nil {
1160-
return
1161-
}
1162-
}
1163-
tx.Commit()
1164-
1165-
_ = settingService.Update("AppStoreSyncStatus", constant.StatusSyncSuccess)
1166-
_ = settingService.Update("AppStoreLastModified", strconv.Itoa(list.LastModified))
1167-
return nil
1168-
}, nil)
928+
syncTask.AddSubTask(task.GetTaskName(i18n.GetMsgByKey("App"), task.TaskSync, task.TaskScopeAppStore), a.syncAppStoreTask, nil)
1169929

1170930
go func() {
1171931
if err := syncTask.Execute(); err != nil {

0 commit comments

Comments
 (0)