@@ -182,13 +182,25 @@ export class RepoManager implements IRepoManager {
182
182
return token ;
183
183
}
184
184
185
- private async syncGitRepository ( repo : RepoWithConnections ) {
185
+ private async syncGitRepository ( repo : RepoWithConnections , repoAlreadyInIndexingState : boolean ) {
186
186
let fetchDuration_s : number | undefined = undefined ;
187
187
let cloneDuration_s : number | undefined = undefined ;
188
188
189
189
const repoPath = getRepoPath ( repo , this . ctx ) ;
190
190
const metadata = repo . metadata as Record < string , string > ;
191
191
192
+ // If the repo was already in the indexing state, this job was likely killed and picked up again. As a result,
193
+ // to ensure the repo state is valid, we delete the repo if it exists so we get a fresh clone
194
+ if ( repoAlreadyInIndexingState && existsSync ( repoPath ) ) {
195
+ this . logger . info ( `Deleting repo directory ${ repoPath } during sync because it was already in the indexing state` ) ;
196
+ await rm ( repoPath , { recursive : true , force : true } , ( err ) => {
197
+ if ( err ) {
198
+ this . logger . error ( `Failed to delete repo directory ${ repoPath } : ${ err } ` ) ;
199
+ throw err ;
200
+ }
201
+ } ) ;
202
+ }
203
+
192
204
if ( existsSync ( repoPath ) ) {
193
205
this . logger . info ( `Fetching ${ repo . id } ...` ) ;
194
206
@@ -235,6 +247,21 @@ export class RepoManager implements IRepoManager {
235
247
private async runIndexJob ( job : Job < RepoIndexingPayload > ) {
236
248
this . logger . info ( `Running index job (id: ${ job . id } ) for repo ${ job . data . repo . id } ` ) ;
237
249
const repo = job . data . repo as RepoWithConnections ;
250
+
251
+ // We have to use the existing repo object to get the repoIndexingStatus because the repo object
252
+ // inside the job is unchanged from when it was added to the queue.
253
+ const existingRepo = await this . db . repo . findUnique ( {
254
+ where : {
255
+ id : repo . id ,
256
+ } ,
257
+ } ) ;
258
+ if ( ! existingRepo ) {
259
+ this . logger . error ( `Repo ${ repo . id } not found` ) ;
260
+ throw new Error ( `Repo ${ repo . id } not found` ) ;
261
+ }
262
+ const repoAlreadyInIndexingState = existingRepo . repoIndexingStatus === RepoIndexingStatus . INDEXING ;
263
+
264
+
238
265
await this . db . repo . update ( {
239
266
where : {
240
267
id : repo . id ,
@@ -255,7 +282,7 @@ export class RepoManager implements IRepoManager {
255
282
256
283
while ( attempts < maxAttempts ) {
257
284
try {
258
- stats = await this . syncGitRepository ( repo ) ;
285
+ stats = await this . syncGitRepository ( repo , repoAlreadyInIndexingState ) ;
259
286
break ;
260
287
} catch ( error ) {
261
288
attempts ++ ;
0 commit comments