@@ -26,6 +26,17 @@ import (
2626 "github.com/zgsm-ai/codebase-indexer/internal/svc"
2727)
2828
29+ func extractFileOperations (metadata * types.SyncMetadata ) map [string ]string {
30+ operations := make (map [string ]string )
31+
32+ // 遍历FileList,该字段存储了文件路径到操作类型的映射
33+ for filePath , operation := range metadata .FileList {
34+ operations [filePath ] = operation
35+ }
36+
37+ return operations
38+ }
39+
2940type TaskLogic struct {
3041 logx.Logger
3142 ctx context.Context
@@ -130,6 +141,39 @@ func (l *TaskLogic) SubmitTask(req *types.IndexTaskRequest, r *http.Request) (re
130141 l .Logger .Infof ("任务分类统计 - 添加: %d, 删除: %d, 修改: %d" ,
131142 len (addTasks ), len (deleteTasks ), len (modifyTasks ))
132143
144+ /////////////////////////////////////////////////////////////
145+ //初始化任务状态
146+ /////////////////////////////////////////////
147+
148+ fileOperations := make (map [string ]string )
149+ if metadata != nil {
150+ fileOperations = extractFileOperations (metadata )
151+ }
152+
153+ // 状态修改为处理中
154+ l .svcCtx .StatusManager .UpdateFileStatus (ctx , req .RequestId ,
155+ func (status * types.FileStatusResponseData ) {
156+ status .Process = "processing"
157+ status .TotalProgress = 0
158+ var fileStatusItems []types.FileStatusItem
159+
160+ for path , _ := range files {
161+ fileStatusItem := types.FileStatusItem {
162+ Path : path , // 使用当前处理的文件路径,而不是codebasePath
163+ Status : "processing" ,
164+ Operate : fileOperations [path ],
165+ }
166+ fileStatusItems = append (fileStatusItems , fileStatusItem )
167+ }
168+
169+ status .FileList = fileStatusItems
170+ l .Logger .Infof ("初始化状态: - RequestId: %s , %v" , req .RequestId , status .FileList )
171+ })
172+
173+ /////////////////////////////////////////////////////////////
174+ //执行任务
175+ /////////////////////////////////////////////
176+
133177 // 如果有删除任务,从向量数据库中删除对应的文件
134178 if len (deleteTasks ) > 0 {
135179 l .Logger .Infof ("开始从向量数据库中删除 %d 个文件" , len (deleteTasks ))
@@ -152,25 +196,32 @@ func (l *TaskLogic) SubmitTask(req *types.IndexTaskRequest, r *http.Request) (re
152196 }
153197 l .Logger .Infof ("更新代码库信息成功 - RequestId: %s" , req .RequestId )
154198
155- // 生成请求ID
156- requestId := l .generateRequestId (req .RequestId , clientId , codebase .Name )
157- l .Logger .Infof ("生成请求ID - RequestId: %s, 新RequestId: %s" , req .RequestId , requestId )
158-
159199 // 提交索引任务
160- l .Logger .Infof ("开始提交索引任务 - RequestId: %s" , req .RequestId )
161- if err := l .submitIndexTask (ctx , codebase , clientId , requestId , mux , files , metadata ); err != nil {
162- l .Logger .Errorf ("提交索引任务失败 - RequestId: %s, 错误: %v" , req .RequestId , err )
163- return nil , err
164- }
165- l .Logger .Infof ("提交索引任务成功 - RequestId: %s" , req .RequestId )
200+ l .Logger .Infof ("开始提交索引任务 - RequestId: %s, 文件数量: %d" , req .RequestId , len (files ))
201+
202+ // 检查文件处理个数是否为0,如果为0则标识完成状态,不提交submitIndexTask任务
203+ if len (files ) == 0 {
204+ l .Logger .Infof ("文件处理个数为0,直接标识完成状态 - RequestId: %s" , req .RequestId )
205+
206+ l .svcCtx .StatusManager .UpdateFileStatus (ctx , req .RequestId , func (status * types.FileStatusResponseData ) {
207+ status .Process = "processing"
208+ status .TotalProgress = 100
209+
210+ for i , _ := range status .FileList {
211+ status .FileList [i ].Status = "completed"
212+ }
213+
214+ })
166215
167- // 初始化文件处理状态
168- l .Logger .Infof ("开始初始化文件处理状态 - RequestId: %s" , req .RequestId )
169- if err := l .initializeFileStatus (ctx , req .RequestId ); err != nil {
170- l .Logger .Errorf ("初始化文件处理状态失败 - RequestId: %s, 错误: %v" , req .RequestId , err )
171- // 不返回错误,继续处理
216+ l .Logger .Infof ("初始化文件处理状态为完成成功 - RequestId: %s" , req .RequestId )
217+ } else {
218+ // 文件数量大于0,正常提交索引任务
219+ if err := l .submitIndexTask (ctx , codebase , clientId , req .RequestId , mux , files , metadata ); err != nil {
220+ l .Logger .Errorf ("提交索引任务失败 - RequestId: %s, 错误: %v" , req .RequestId , err )
221+ return nil , err
222+ }
223+ l .Logger .Infof ("提交索引任务成功 - RequestId: %s" , req .RequestId )
172224 }
173- l .Logger .Infof ("初始化文件处理状态成功 - RequestId: %s" , req .RequestId )
174225
175226 return & types.IndexTaskResponseData {TaskId : req .RequestId }, nil
176227}
@@ -469,14 +520,6 @@ func (l *TaskLogic) updateCodebaseInfo(codebase *model.Codebase, fileCount int,
469520 return nil
470521}
471522
472- // generateRequestId 生成请求ID
473- func (l * TaskLogic ) generateRequestId (requestId , clientId , codebaseName string ) string {
474- if requestId == "" {
475- return fmt .Sprintf ("%s-%s-%d" , clientId , codebaseName , time .Now ().Unix ())
476- }
477- return requestId
478- }
479-
480523// submitIndexTask 提交索引任务
481524func (l * TaskLogic ) submitIndexTask (ctx context.Context , codebase * model.Codebase , clientId , requestId string , mux * redsync.Mutex , files map [string ][]byte , metadata * types.SyncMetadata ) error {
482525 startTime := time .Now ()
@@ -497,6 +540,12 @@ func (l *TaskLogic) submitIndexTask(ctx context.Context, codebase *model.Codebas
497540 },
498541 }
499542
543+ runningTasks := l .svcCtx .TaskPool .Running ()
544+ taskCapacity := l .svcCtx .TaskPool .Cap ()
545+ l .Logger .Infof ("任务池状态 - RequestId: %s, 正在运行任务: %d, 任务容量: %d" , requestId , runningTasks , taskCapacity )
546+
547+ // 使用任务池提交任务
548+
500549 l .Logger .Infof ("开始提交任务到任务池 - RequestId: %s, 超时时间: %v" , requestId , l .svcCtx .Config .IndexTask .GraphTask .Timeout )
501550 err := l .svcCtx .TaskPool .Submit (func () {
502551 taskStartTime := time .Now ()
@@ -523,18 +572,6 @@ func (l *TaskLogic) submitIndexTask(ctx context.Context, codebase *model.Codebas
523572 return nil
524573}
525574
526- // initializeFileStatus 初始化文件处理状态
527- func (l * TaskLogic ) initializeFileStatus (ctx context.Context , requestId string ) error {
528- initialStatus := & types.FileStatusResponseData {
529- Process : "pending" ,
530- TotalProgress : 0 ,
531- FileList : []types.FileStatusItem {},
532- }
533-
534- // 使用RequestId作为键存储状态
535- return l .svcCtx .StatusManager .SetFileStatusByRequestId (ctx , requestId , initialStatus )
536- }
537-
538575func (l * TaskLogic ) initCodebaseIfNotExists (clientId , clientPath , userUid , codebaseName string ) (* model.Codebase , error ) {
539576 var codebase * model.Codebase
540577 var err error
0 commit comments