Skip to content

Commit b77f55f

Browse files
committed
wipe existing repo if we've picked up a killed job to ensure good state
1 parent 8531b14 commit b77f55f

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

packages/backend/src/repoManager.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,25 @@ export class RepoManager implements IRepoManager {
182182
return token;
183183
}
184184

185-
private async syncGitRepository(repo: RepoWithConnections) {
185+
private async syncGitRepository(repo: RepoWithConnections, repoAlreadyInIndexingState: boolean) {
186186
let fetchDuration_s: number | undefined = undefined;
187187
let cloneDuration_s: number | undefined = undefined;
188188

189189
const repoPath = getRepoPath(repo, this.ctx);
190190
const metadata = repo.metadata as Record<string, string>;
191191

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+
192204
if (existsSync(repoPath)) {
193205
this.logger.info(`Fetching ${repo.id}...`);
194206

@@ -235,6 +247,21 @@ export class RepoManager implements IRepoManager {
235247
private async runIndexJob(job: Job<RepoIndexingPayload>) {
236248
this.logger.info(`Running index job (id: ${job.id}) for repo ${job.data.repo.id}`);
237249
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+
238265
await this.db.repo.update({
239266
where: {
240267
id: repo.id,
@@ -255,7 +282,7 @@ export class RepoManager implements IRepoManager {
255282

256283
while (attempts < maxAttempts) {
257284
try {
258-
stats = await this.syncGitRepository(repo);
285+
stats = await this.syncGitRepository(repo, repoAlreadyInIndexingState);
259286
break;
260287
} catch (error) {
261288
attempts++;

0 commit comments

Comments
 (0)