Skip to content

Commit be0ccd1

Browse files
authored
fix: add plugin id conflict checker (coze-dev#78)
1 parent 945b0af commit be0ccd1

File tree

5 files changed

+127
-12
lines changed

5 files changed

+127
-12
lines changed

backend/application/app/app.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import (
6060
"github.com/coze-dev/coze-studio/backend/pkg/lang/conv"
6161
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
6262
"github.com/coze-dev/coze-studio/backend/pkg/logs"
63+
"github.com/coze-dev/coze-studio/backend/pkg/safego"
6364
"github.com/coze-dev/coze-studio/backend/pkg/taskgroup"
6465
"github.com/coze-dev/coze-studio/backend/types/consts"
6566
"github.com/coze-dev/coze-studio/backend/types/errno"
@@ -183,18 +184,19 @@ func (a *APPApplicationService) DraftProjectDelete(ctx context.Context, req *pro
183184
logs.CtxErrorf(ctx, "publish project '%d' failed, err=%v", req.ProjectID, err)
184185
}
185186

186-
err = a.deleteAPPResources(ctx, req.ProjectID)
187-
if err != nil {
188-
logs.CtxErrorf(ctx, "delete app '%d' resources failed, err=%v", req.ProjectID, err)
189-
}
187+
safego.Go(ctx, func() {
188+
// When an app is deleted, resource deletion is currently handled as a weak dependency, meaning some resources might not be deleted, but they will be inaccessible to the user.
189+
// TODO:: Application resources need to check the deletion status of the application
190+
a.deleteAPPResources(ctx, req.ProjectID)
191+
})
190192

191193
resp = &projectAPI.DraftProjectDeleteResponse{}
192194

193195
return resp, nil
194196
}
195197

196-
func (a *APPApplicationService) deleteAPPResources(ctx context.Context, appID int64) (err error) {
197-
err = plugin.PluginApplicationSVC.DeleteAPPAllPlugins(ctx, appID)
198+
func (a *APPApplicationService) deleteAPPResources(ctx context.Context, appID int64) {
199+
err := plugin.PluginApplicationSVC.DeleteAPPAllPlugins(ctx, appID)
198200
if err != nil {
199201
logs.CtxErrorf(ctx, "delete app '%d' plugins failed, err=%v", appID, err)
200202
}
@@ -218,8 +220,6 @@ func (a *APPApplicationService) deleteAPPResources(ctx context.Context, appID in
218220
if err != nil {
219221
logs.CtxErrorf(ctx, "delete app '%d' workflow failed, err=%v", appID, err)
220222
}
221-
222-
return nil
223223
}
224224

225225
func (a *APPApplicationService) DraftProjectUpdate(ctx context.Context, req *projectAPI.DraftProjectUpdateRequest) (resp *projectAPI.DraftProjectUpdateResponse, err error) {

backend/application/plugin/init.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@ package plugin
1818

1919
import (
2020
"context"
21+
"strconv"
22+
"strings"
2123

2224
"gorm.io/gorm"
2325

26+
"github.com/coze-dev/coze-studio/backend/domain/plugin/conf"
2427
pluginConf "github.com/coze-dev/coze-studio/backend/domain/plugin/conf"
2528
"github.com/coze-dev/coze-studio/backend/domain/plugin/repository"
2629
"github.com/coze-dev/coze-studio/backend/domain/plugin/service"
2730
search "github.com/coze-dev/coze-studio/backend/domain/search/service"
2831
user "github.com/coze-dev/coze-studio/backend/domain/user/service"
2932
"github.com/coze-dev/coze-studio/backend/infra/contract/idgen"
3033
"github.com/coze-dev/coze-studio/backend/infra/contract/storage"
34+
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
35+
"github.com/coze-dev/coze-studio/backend/pkg/lang/slices"
36+
"github.com/coze-dev/coze-studio/backend/types/errno"
3137
)
3238

3339
type ServiceComponents struct {
@@ -68,6 +74,11 @@ func InitService(ctx context.Context, components *ServiceComponents) (*PluginApp
6874
OAuthRepo: oauthRepo,
6975
})
7076

77+
err = checkIDExist(ctx, pluginSVC)
78+
if err != nil {
79+
return nil, err
80+
}
81+
7182
PluginApplicationSVC.DomainSVC = pluginSVC
7283
PluginApplicationSVC.eventbus = components.EventBus
7384
PluginApplicationSVC.oss = components.OSS
@@ -77,3 +88,51 @@ func InitService(ctx context.Context, components *ServiceComponents) (*PluginApp
7788

7889
return PluginApplicationSVC, nil
7990
}
91+
92+
func checkIDExist(ctx context.Context, pluginService service.PluginService) error {
93+
pluginProducts := conf.GetAllPluginProducts()
94+
95+
pluginIDs := make([]int64, 0, len(pluginProducts))
96+
var toolIDs []int64
97+
for _, p := range pluginProducts {
98+
pluginIDs = append(pluginIDs, p.Info.ID)
99+
toolIDs = append(toolIDs, p.ToolIDs...)
100+
}
101+
102+
pluginInfos, err := pluginService.MGetDraftPlugins(ctx, pluginIDs)
103+
if err != nil {
104+
return err
105+
}
106+
if len(pluginInfos) > 0 {
107+
conflictsIDs := make([]int64, 0, len(pluginInfos))
108+
for _, p := range pluginInfos {
109+
conflictsIDs = append(conflictsIDs, p.ID)
110+
}
111+
112+
return errorx.New(errno.ErrPluginIDExist,
113+
errorx.KV("plugin_id", strings.Join(slices.Transform(conflictsIDs, func(id int64) string {
114+
return strconv.FormatInt(id, 10)
115+
}), ",")),
116+
)
117+
}
118+
119+
tools, err := pluginService.MGetDraftTools(ctx, toolIDs)
120+
if err != nil {
121+
return err
122+
}
123+
124+
if len(tools) > 0 {
125+
conflictsIDs := make([]int64, 0, len(tools))
126+
for _, t := range tools {
127+
conflictsIDs = append(conflictsIDs, t.ID)
128+
}
129+
130+
return errorx.New(errno.ErrToolIDExist,
131+
errorx.KV("tool_id", strings.Join(slices.Transform(conflictsIDs, func(id int64) string {
132+
return strconv.FormatInt(id, 10)
133+
}), ",")),
134+
)
135+
}
136+
return nil
137+
138+
}

backend/domain/plugin/internal/dal/plugin_draft.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/coze-dev/coze-studio/backend/api/model/crossdomain/plugin"
2929
"github.com/coze-dev/coze-studio/backend/api/model/plugin_develop_common"
30+
"github.com/coze-dev/coze-studio/backend/domain/plugin/conf"
3031
"github.com/coze-dev/coze-studio/backend/domain/plugin/entity"
3132
"github.com/coze-dev/coze-studio/backend/domain/plugin/internal/dal/model"
3233
"github.com/coze-dev/coze-studio/backend/domain/plugin/internal/dal/query"
@@ -88,7 +89,7 @@ func (p *PluginDraftDAO) getSelected(opt *PluginSelectedOption) (selected []fiel
8889
}
8990

9091
func (p *PluginDraftDAO) Create(ctx context.Context, plugin *entity.PluginInfo) (pluginID int64, err error) {
91-
id, err := p.idGen.GenID(ctx)
92+
id, err := p.genPluginID(ctx)
9293
if err != nil {
9394
return 0, err
9495
}
@@ -117,6 +118,25 @@ func (p *PluginDraftDAO) Create(ctx context.Context, plugin *entity.PluginInfo)
117118
return id, nil
118119
}
119120

121+
func (p *PluginDraftDAO) genPluginID(ctx context.Context) (id int64, err error) {
122+
123+
retryTimes := 5
124+
for i := 0; i < retryTimes; i++ {
125+
id, err = p.idGen.GenID(ctx)
126+
if err != nil {
127+
return 0, err
128+
}
129+
if _, ok := conf.GetPluginProduct(id); !ok {
130+
break
131+
}
132+
if i == retryTimes-1 {
133+
return 0, fmt.Errorf("id %d is confilict with product plugin id.", id)
134+
}
135+
}
136+
137+
return id, nil
138+
}
139+
120140
func (p *PluginDraftDAO) Get(ctx context.Context, pluginID int64, opt *PluginSelectedOption) (plugin *entity.PluginInfo, exist bool, err error) {
121141
table := p.query.PluginDraft
122142
pl, err := table.WithContext(ctx).
@@ -262,7 +282,7 @@ func (p *PluginDraftDAO) Update(ctx context.Context, plugin *entity.PluginInfo)
262282
}
263283

264284
func (p *PluginDraftDAO) CreateWithTX(ctx context.Context, tx *query.QueryTx, plugin *entity.PluginInfo) (pluginID int64, err error) {
265-
id, err := p.idGen.GenID(ctx)
285+
id, err := p.genPluginID(ctx)
266286
if err != nil {
267287
return 0, err
268288
}

backend/domain/plugin/internal/dal/tool_draft.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/coze-dev/coze-studio/backend/api/model/crossdomain/plugin"
2929
common "github.com/coze-dev/coze-studio/backend/api/model/plugin_develop_common"
30+
"github.com/coze-dev/coze-studio/backend/domain/plugin/conf"
3031
"github.com/coze-dev/coze-studio/backend/domain/plugin/entity"
3132
"github.com/coze-dev/coze-studio/backend/domain/plugin/internal/dal/model"
3233
"github.com/coze-dev/coze-studio/backend/domain/plugin/internal/dal/query"
@@ -90,7 +91,8 @@ func (t *ToolDraftDAO) getSelected(opt *ToolSelectedOption) (selected []field.Ex
9091
}
9192

9293
func (t *ToolDraftDAO) Create(ctx context.Context, tool *entity.ToolInfo) (toolID int64, err error) {
93-
id, err := t.idGen.GenID(ctx)
94+
95+
id, err := t.genToolID(ctx)
9496
if err != nil {
9597
return 0, err
9698
}
@@ -111,6 +113,27 @@ func (t *ToolDraftDAO) Create(ctx context.Context, tool *entity.ToolInfo) (toolI
111113
return id, nil
112114
}
113115

116+
func (t *ToolDraftDAO) genToolID(ctx context.Context) (id int64, err error) {
117+
118+
retryTimes := 5
119+
120+
for i := 0; i < retryTimes; i++ {
121+
id, err = t.idGen.GenID(ctx)
122+
if err != nil {
123+
return 0, err
124+
}
125+
126+
if _, ok := conf.GetToolProduct(id); !ok {
127+
break
128+
}
129+
if i == retryTimes-1 {
130+
return 0, fmt.Errorf("id %d is confilict with product tool id.", id)
131+
}
132+
}
133+
134+
return id, nil
135+
}
136+
114137
func (t *ToolDraftDAO) Get(ctx context.Context, toolID int64) (tool *entity.ToolInfo, exist bool, err error) {
115138
table := t.query.ToolDraft
116139
tl, err := table.WithContext(ctx).
@@ -335,7 +358,7 @@ func (t *ToolDraftDAO) BatchCreateWithTX(ctx context.Context, tx *query.QueryTx,
335358
tls := make([]*model.ToolDraft, 0, len(tools))
336359

337360
for _, tool := range tools {
338-
id, err := t.idGen.GenID(ctx)
361+
id, err := t.genToolID(ctx)
339362
if err != nil {
340363
return nil, err
341364
}

backend/types/errno/plugin.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,26 @@ const (
3838
ErrPluginToolsCheckFailed = 109000011
3939
ErrPluginParseToolRespFailed = 109000012
4040
ErrPluginOAuthFailed = 109000013
41+
ErrPluginIDExist = 109000014
42+
ErrToolIDExist = 109000015
4143
)
4244

4345
const (
4446
PluginMsgKey = "msg"
4547
)
4648

4749
func init() {
50+
51+
code.Register(
52+
ErrPluginIDExist,
53+
"Plugin ID already exists : {plugin_id}",
54+
code.WithAffectStability(false),
55+
)
56+
code.Register(
57+
ErrToolIDExist,
58+
"Tool ID already exists : {tool_id}",
59+
code.WithAffectStability(false),
60+
)
4861
code.Register(
4962
ErrPluginPermissionCode,
5063
fmt.Sprintf("unauthorized access : {%s}", PluginMsgKey),

0 commit comments

Comments
 (0)