Skip to content

Commit 92a8138

Browse files
Random fixes and improvements (#244)
1 parent f4db3d2 commit 92a8138

File tree

5 files changed

+71
-16
lines changed

5 files changed

+71
-16
lines changed

.env.development

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ AUTH_URL="http://localhost:3000"
3434
# Sentry
3535
# SENTRY_BACKEND_DSN=""
3636
# NEXT_PUBLIC_SENTRY_WEBAPP_DSN=""
37-
# SENTRY_ENVIRONMENT="dev"
37+
SENTRY_ENVIRONMENT="dev"
3838
# NEXT_PUBLIC_SENTRY_ENVIRONMENT="dev"
3939
# SENTRY_AUTH_TOKEN=
4040

packages/backend/src/connectionManager.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Connection, ConnectionSyncStatus, PrismaClient, Prisma } from "@sourcebot/db";
1+
import { Connection, ConnectionSyncStatus, PrismaClient, Prisma, RepoIndexingStatus } from "@sourcebot/db";
22
import { Job, Queue, Worker } from 'bullmq';
33
import { Settings } from "./types.js";
44
import { ConnectionConfig } from "@sourcebot/schemas/v3/connection.type";
@@ -160,7 +160,7 @@ export class ConnectionManager implements IConnectionManager {
160160
}
161161
}
162162

163-
const { repoData, notFound } = result;
163+
let { repoData, notFound } = result;
164164

165165
// Push the information regarding not found users, orgs, and repos to the connection's syncStatusMetadata. Note that
166166
// this won't be overwritten even if the connection job fails
@@ -174,7 +174,7 @@ export class ConnectionManager implements IConnectionManager {
174174
});
175175

176176
// Filter out any duplicates by external_id and external_codeHostUrl.
177-
repoData.filter((repo, index, self) => {
177+
repoData = repoData.filter((repo, index, self) => {
178178
return index === self.findIndex(r =>
179179
r.external_id === repo.external_id &&
180180
r.external_codeHostUrl === repo.external_codeHostUrl
@@ -263,6 +263,14 @@ export class ConnectionManager implements IConnectionManager {
263263

264264
private async onSyncJobFailed(job: Job | undefined, err: unknown) {
265265
this.logger.info(`Connection sync job failed with error: ${err}`);
266+
Sentry.captureException(err, {
267+
tags: {
268+
repoId: job?.data.repo.id,
269+
jobId: job?.id,
270+
queue: QUEUE_NAME,
271+
}
272+
});
273+
266274
if (job) {
267275
const { connectionId } = job.data;
268276

packages/backend/src/repoManager.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,25 @@ export class RepoManager implements IRepoManager {
130130
const thresholdDate = new Date(Date.now() - this.settings.reindexIntervalMs);
131131
const repos = await this.db.repo.findMany({
132132
where: {
133-
repoIndexingStatus: {
134-
in: [
135-
RepoIndexingStatus.NEW,
136-
RepoIndexingStatus.INDEXED
137-
]
138-
},
139133
OR: [
140-
{ indexedAt: null },
141-
{ indexedAt: { lt: thresholdDate } },
134+
// "NEW" is really a misnomer here - it just means that the repo needs to be indexed
135+
// immediately. In most cases, this will be because the repo was just created and
136+
// is indeed "new". However, it could also be that a "retry" was requested on a failed
137+
// index. So, we don't want to block on the indexedAt timestamp here.
138+
{
139+
repoIndexingStatus: RepoIndexingStatus.NEW,
140+
},
141+
// When the repo has already been indexed, we only want to reindex if the reindexing
142+
// interval has elapsed (or if the date isn't set for some reason).
143+
{
144+
AND: [
145+
{ repoIndexingStatus: RepoIndexingStatus.INDEXED },
146+
{ OR: [
147+
{ indexedAt: null },
148+
{ indexedAt: { lt: thresholdDate } },
149+
]}
150+
]
151+
}
142152
]
143153
},
144154
include: {
@@ -335,7 +345,15 @@ export class RepoManager implements IRepoManager {
335345
}
336346

337347
private async onIndexJobFailed(job: Job<RepoIndexingPayload> | undefined, err: unknown) {
338-
this.logger.info(`Repo index job failed (id: ${job?.id ?? 'unknown'})`);
348+
this.logger.info(`Repo index job failed (id: ${job?.id ?? 'unknown'}) with error: ${err}`);
349+
Sentry.captureException(err, {
350+
tags: {
351+
repoId: job?.data.repo.id,
352+
jobId: job?.id,
353+
queue: REPO_INDEXING_QUEUE,
354+
}
355+
});
356+
339357
if (job) {
340358
this.promClient.activeRepoIndexingJobs.dec();
341359
this.promClient.repoIndexingFailTotal.inc();
@@ -474,6 +492,13 @@ export class RepoManager implements IRepoManager {
474492

475493
private async onGarbageCollectionJobFailed(job: Job<RepoGarbageCollectionPayload> | undefined, err: unknown) {
476494
this.logger.info(`Garbage collection job failed (id: ${job?.id ?? 'unknown'}) with error: ${err}`);
495+
Sentry.captureException(err, {
496+
tags: {
497+
repoId: job?.data.repo.id,
498+
jobId: job?.id,
499+
queue: REPO_GC_QUEUE,
500+
}
501+
});
477502

478503
if (job) {
479504
this.promClient.activeRepoGarbageCollectionJobs.dec();

packages/web/src/app/posthogProvider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export function PostHogProvider({ children, disabled }: PostHogProviderProps) {
3838
// @see next.config.mjs for path rewrites to the "/ingest" route.
3939
api_host: "/ingest",
4040
person_profiles: 'identified_only',
41-
capture_pageview: false, // @nocheckin Disable automatic pageview capture if we're not in public demo mode
42-
autocapture: false, // Disable automatic event capture
41+
capture_pageview: false,
42+
autocapture: false,
4343
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4444
sanitize_properties: (properties: Record<string, any>, _event: string) => {
4545
// https://posthog.com/docs/libraries/js#config

packages/web/src/initialize.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ConnectionSyncStatus, OrgRole, Prisma } from '@sourcebot/db';
1+
import { ConnectionSyncStatus, OrgRole, Prisma, RepoIndexingStatus } from '@sourcebot/db';
22
import { env } from './env.mjs';
33
import { prisma } from "@/prisma";
44
import { SINGLE_TENANT_USER_ID, SINGLE_TENANT_ORG_ID, SINGLE_TENANT_ORG_DOMAIN, SINGLE_TENANT_ORG_NAME, SINGLE_TENANT_USER_EMAIL } from './lib/constants';
@@ -105,6 +105,13 @@ const initSingleTenancy = async () => {
105105
name: key,
106106
orgId: SINGLE_TENANT_ORG_ID,
107107
}
108+
},
109+
include: {
110+
repos: {
111+
include: {
112+
repo: true,
113+
}
114+
}
108115
}
109116
});
110117

@@ -137,6 +144,21 @@ const initSingleTenancy = async () => {
137144
});
138145

139146
console.log(`Upserted connection with name '${key}'. Connection ID: ${connectionDb.id}`);
147+
148+
// Re-try any repos that failed to index.
149+
const failedRepos = currentConnection?.repos.filter(repo => repo.repo.repoIndexingStatus === RepoIndexingStatus.FAILED).map(repo => repo.repo.id) ?? [];
150+
if (failedRepos.length > 0) {
151+
await prisma.repo.updateMany({
152+
where: {
153+
id: {
154+
in: failedRepos,
155+
}
156+
},
157+
data: {
158+
repoIndexingStatus: RepoIndexingStatus.NEW,
159+
}
160+
})
161+
}
140162
}
141163
}
142164
}

0 commit comments

Comments
 (0)