@@ -138,10 +138,20 @@ func (j *IndexCleanJob) cleanupExpiredWorkspaceIndexes(ctx context.Context) {
138138 j .logger .Error ("recovered from panic in index clean job: %v" , r )
139139 }
140140 }()
141- j .logger .Info ("start to clean up expired workspace indexes with expiry period %.0f hours" , j .expiryPeriod .Hours ())
141+
142+ // 获取 codebase 开关状态
143+ codebaseEnv := j .storageRepo .GetCodebaseEnv ()
144+ if codebaseEnv == nil {
145+ j .logger .Warn ("failed to get codebase env" )
146+ return
147+ }
148+
149+ j .logger .Info ("start to clean up expired workspace indexes with expiry period %.0f hours, codebase switch: %s" ,
150+ j .expiryPeriod .Hours (), codebaseEnv .Switch )
151+
142152 workspaces , err := j .workspaceRepo .ListWorkspaces ()
143153 if err != nil {
144- j .logger .Error ("list workspaces failed with %v" , err )
154+ j .logger .Warn ("list workspaces failed with %v" , err )
145155 return
146156 }
147157
@@ -151,13 +161,23 @@ func (j *IndexCleanJob) cleanupExpiredWorkspaceIndexes(ctx context.Context) {
151161 }
152162
153163 for _ , workspace := range workspaces {
154- // 活跃中 更新时间小于过期间隔 索引数量为0 跳过
155- if workspace .Active == dto .True || time .Since (workspace .UpdatedAt ) < j .expiryPeriod ||
156- workspace .CodegraphFileNum == 0 {
157- continue
164+ // 如果 codebase 开关为 on,则使用原来的逻辑
165+ if codebaseEnv .Switch == dto .SwitchOn {
166+ // 活跃中 更新时间小于过期间隔 索引数量为0 跳过
167+ if workspace .Active == dto .True || time .Since (workspace .UpdatedAt ) < j .expiryPeriod ||
168+ workspace .CodegraphFileNum == 0 {
169+ continue
170+ }
171+ } else {
172+ // 如果 codebase 开关为 off,则检查是否过期一天
173+ if time .Since (workspace .UpdatedAt ) < 24 * time .Hour ||
174+ workspace .CodegraphFileNum == 0 {
175+ continue
176+ }
158177 }
159- j .logger .Info ("workspace %s updated_at %s exceeds expiry period %.0f hours, start to cleanup." ,
160- workspace .WorkspacePath , workspace .UpdatedAt .Format ("2006-01-02 15:04:05" ), j .expiryPeriod .Hours ())
178+
179+ j .logger .Info ("workspace %s updated_at %s exceeds expiry period, start to cleanup." ,
180+ workspace .WorkspacePath , workspace .UpdatedAt .Format ("2006-01-02 15:04:05" ))
161181 // 清理索引 (有更新数据库为0的逻辑)
162182 if err = j .indexer .RemoveAllIndexes (ctx , workspace .WorkspacePath ); err != nil {
163183 j .logger .Error ("remove indexes failed with %v" , err )
@@ -177,9 +197,16 @@ func (j *IndexCleanJob) cleanupInactiveWorkspaceEmbeddings(ctx context.Context)
177197 }
178198 }()
179199
200+ // 获取 codebase 开关状态
201+ codebaseEnv := j .storageRepo .GetCodebaseEnv ()
202+ if codebaseEnv == nil {
203+ j .logger .Warn ("failed to get codebase env" )
204+ return
205+ }
206+
180207 workspaces , err := j .workspaceRepo .ListWorkspaces ()
181208 if err != nil {
182- j .logger .Error ("list workspaces failed with %v" , err )
209+ j .logger .Warn ("list workspaces failed with %v" , err )
183210 return
184211 }
185212
@@ -192,12 +219,77 @@ func (j *IndexCleanJob) cleanupInactiveWorkspaceEmbeddings(ctx context.Context)
192219 authInfo := config .GetAuthInfo ()
193220 clientId := authInfo .ClientId
194221 for _ , workspace := range workspaces {
195- // 只处理 active 为 false, embedding_file_num 不为 0, 且更新时间超过过期间隔的工作区
196- if workspace .Active == dto .True || time .Since (workspace .UpdatedAt ) < j .embeddingExpiryPeriod ||
197- workspace .EmbeddingFileNum == 0 {
222+ if workspace .EmbeddingFileNum == 0 {
223+ continue
224+ }
225+ // 调用 FetchCombinedSummary 方法获取结果
226+ summaryReq := dto.CombinedSummaryReq {
227+ ClientId : clientId ,
228+ CodebasePath : workspace .WorkspacePath ,
229+ }
230+
231+ summaryResp , err := j .syncRepo .FetchCombinedSummary (summaryReq )
232+ if err != nil {
233+ j .logger .Warn ("failed to fetch combined summary for workspace %s: %v" , workspace .WorkspacePath , err )
198234 continue
199235 }
200236
237+ // 判断状态,若为 failed 则复用下面的更新 workspace、删除 event、删除 codebaseConfig 和 embeddingConfig 逻辑
238+ if summaryResp .Data .Embedding .TotalChunks == 0 {
239+ j .logger .Info ("workspace %s embedding total chunks is 0, start to cleanup." ,
240+ workspace .WorkspacePath )
241+
242+ // 更新 workspace
243+ updateWorkspace := map [string ]interface {}{
244+ "file_num" : 0 ,
245+ "embedding_file_num" : 0 ,
246+ "embedding_ts" : 0 ,
247+ "embedding_message" : "" ,
248+ "embedding_failed_file_paths" : "" ,
249+ }
250+ if err := j .workspaceRepo .UpdateWorkspaceByMap (workspace .WorkspacePath , updateWorkspace ); err != nil {
251+ j .logger .Error ("update workspace failed with %v" , err )
252+ continue
253+ }
254+
255+ // 1. 删除这个 workspace 的所有 event 表记录
256+ if err := j .cleanupWorkspaceEvents (workspace .WorkspacePath ); err != nil {
257+ j .logger .Error ("failed to cleanup events for workspace %s: %v" , workspace .WorkspacePath , err )
258+ continue
259+ }
260+
261+ // 2. 删除 codebaseConfig
262+ codebaseId := utils .GenerateCodebaseID (workspace .WorkspacePath )
263+ if err := j .storageRepo .DeleteCodebaseConfig (codebaseId ); err != nil {
264+ j .logger .Error ("failed to delete codebase config for workspace %s: %v" , workspace .WorkspacePath , err )
265+ continue
266+ }
267+
268+ // 3. 删除 embeddingConfig
269+ embeddingId := utils .GenerateEmbeddingID (workspace .WorkspacePath )
270+ if err := j .embeddingRepo .DeleteEmbeddingConfig (embeddingId ); err != nil {
271+ j .logger .Error ("failed to delete embedding config for workspace %s: %v" , workspace .WorkspacePath , err )
272+ continue
273+ }
274+
275+ j .logger .Info ("workspace %s embeddings cleaned up successfully." , workspace .WorkspacePath )
276+ cleanedCount ++
277+ continue
278+ }
279+
280+ // 如果 codebase 开关为 on,则使用原来的逻辑
281+ if codebaseEnv .Switch == dto .SwitchOn {
282+ // 只处理 active 为 false, 且更新时间超过过期间隔的工作区
283+ if workspace .Active == dto .True || time .Since (workspace .UpdatedAt ) < j .embeddingExpiryPeriod {
284+ continue
285+ }
286+ } else {
287+ // 如果 codebase 开关为 off,则检查是否过期一天
288+ if time .Since (workspace .UpdatedAt ) < 24 * time .Hour {
289+ continue
290+ }
291+ }
292+
201293 j .logger .Info ("workspace %s is inactive and exceeds expiry period, start to cleanup embeddings." ,
202294 workspace .WorkspacePath )
203295
0 commit comments