Skip to content

Commit af104ed

Browse files
feedback
1 parent 4fd1645 commit af104ed

File tree

6 files changed

+69
-5
lines changed

6 files changed

+69
-5
lines changed

packages/backend/src/connectionManager.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Settings } from "./types.js";
1111
import { groupmqLifecycleExceptionWrapper } from "./utils.js";
1212
import { syncSearchContexts } from "./ee/syncSearchContexts.js";
1313
import { captureEvent } from "./posthog.js";
14+
import { PromClient } from "./promClient.js";
1415

1516
const LOG_TAG = 'connection-manager';
1617
const logger = createLogger(LOG_TAG);
@@ -38,6 +39,7 @@ export class ConnectionManager {
3839
private db: PrismaClient,
3940
private settings: Settings,
4041
redis: Redis,
42+
private promClient: PromClient,
4143
) {
4244
this.queue = new Queue<JobPayload>({
4345
redis,
@@ -137,6 +139,8 @@ export class ConnectionManager {
137139
},
138140
jobId: job.id,
139141
});
142+
143+
this.promClient.pendingConnectionSyncJobs.inc({ connection: job.connection.name });
140144
}
141145

142146
return jobs.map(job => job.id);
@@ -147,6 +151,9 @@ export class ConnectionManager {
147151
const logger = createJobLogger(jobId);
148152
logger.info(`Running connection sync job ${jobId} for connection ${connectionName} (id: ${job.data.connectionId}) (attempt ${job.attempts + 1} / ${job.maxAttempts})`);
149153

154+
this.promClient.pendingConnectionSyncJobs.dec({ connection: connectionName });
155+
this.promClient.activeConnectionSyncJobs.inc({ connection: connectionName });
156+
150157
// @note: We aren't actually doing anything with this atm.
151158
const abortController = new AbortController();
152159

@@ -265,7 +272,7 @@ export class ConnectionManager {
265272
private onJobCompleted = async (job: Job<JobPayload>) =>
266273
groupmqLifecycleExceptionWrapper('onJobCompleted', logger, async () => {
267274
const logger = createJobLogger(job.id);
268-
const { connectionId, orgId } = job.data;
275+
const { connectionId, connectionName, orgId } = job.data;
269276

270277
await this.db.connectionSyncJob.update({
271278
where: {
@@ -301,6 +308,9 @@ export class ConnectionManager {
301308

302309
logger.info(`Connection sync job ${job.id} for connection ${job.data.connectionName} (id: ${job.data.connectionId}) completed`);
303310

311+
this.promClient.activeConnectionSyncJobs.dec({ connection: connectionName });
312+
this.promClient.connectionSyncJobSuccessTotal.inc({ connection: connectionName });
313+
304314
const result = job.returnvalue as JobResult;
305315
captureEvent('backend_connection_sync_job_completed', {
306316
connectionId: connectionId,
@@ -328,12 +338,17 @@ export class ConnectionManager {
328338
}
329339
});
330340

341+
this.promClient.activeConnectionSyncJobs.dec({ connection: connection.name });
342+
this.promClient.connectionSyncJobFailTotal.inc({ connection: connection.name });
343+
331344
logger.error(`Failed job ${job.id} for connection ${connection.name} (id: ${connection.id}). Attempt ${attempt} / ${job.opts.attempts}. Failing job.`);
332345
} else {
333346
const connection = await this.db.connection.findUniqueOrThrow({
334347
where: { id: job.data.connectionId },
335348
});
336349

350+
this.promClient.connectionSyncJobReattemptsTotal.inc({ connection: connection.name });
351+
337352
logger.warn(`Failed job ${job.id} for connection ${connection.name} (id: ${connection.id}). Attempt ${attempt} / ${job.opts.attempts}. Retrying.`);
338353
}
339354

@@ -358,6 +373,9 @@ export class ConnectionManager {
358373
}
359374
});
360375

376+
this.promClient.activeConnectionSyncJobs.dec({ connection: connection.name });
377+
this.promClient.connectionSyncJobFailTotal.inc({ connection: connection.name });
378+
361379
logger.error(`Job ${jobId} stalled for connection ${connection.name} (id: ${connection.id})`);
362380

363381
captureEvent('backend_connection_sync_job_failed', {

packages/backend/src/env.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ export const env = createEnv({
5656

5757
EXPERIMENT_EE_PERMISSION_SYNC_ENABLED: booleanSchema.default('false'),
5858
AUTH_EE_GITHUB_BASE_URL: z.string().optional(),
59-
60-
FORCE_ENABLE_ANONYMOUS_ACCESS: booleanSchema.default('false'),
6159
},
6260
runtimeEnv: process.env,
6361
emptyStringAsUndefined: true,

packages/backend/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ if (hasEntitlement('github-app')) {
5050
await GithubAppManager.getInstance().init(prisma);
5151
}
5252

53-
const connectionManager = new ConnectionManager(prisma, settings, redis);
53+
const connectionManager = new ConnectionManager(prisma, settings, redis, promClient);
5454
const repoPermissionSyncer = new RepoPermissionSyncer(prisma, settings, redis);
5555
const userPermissionSyncer = new UserPermissionSyncer(prisma, settings, redis);
5656
const repoIndexManager = new RepoIndexManager(prisma, settings, redis, promClient);

packages/backend/src/promClient.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export class PromClient {
1616
public repoIndexJobFailTotal: Counter<string>;
1717
public repoIndexJobSuccessTotal: Counter<string>;
1818

19+
public activeConnectionSyncJobs: Gauge<string>;
20+
public pendingConnectionSyncJobs: Gauge<string>;
21+
public connectionSyncJobReattemptsTotal: Counter<string>;
22+
public connectionSyncJobFailTotal: Counter<string>;
23+
public connectionSyncJobSuccessTotal: Counter<string>;
24+
1925
public readonly PORT = 3060;
2026

2127
constructor() {
@@ -56,6 +62,41 @@ export class PromClient {
5662
});
5763
this.registry.registerMetric(this.repoIndexJobSuccessTotal);
5864

65+
this.activeConnectionSyncJobs = new Gauge({
66+
name: 'active_connection_sync_jobs',
67+
help: 'The number of connection sync jobs in progress',
68+
labelNames: ['connection'],
69+
});
70+
this.registry.registerMetric(this.activeConnectionSyncJobs);
71+
72+
this.pendingConnectionSyncJobs = new Gauge({
73+
name: 'pending_connection_sync_jobs',
74+
help: 'The number of connection sync jobs waiting in queue',
75+
labelNames: ['connection'],
76+
});
77+
this.registry.registerMetric(this.pendingConnectionSyncJobs);
78+
79+
this.connectionSyncJobReattemptsTotal = new Counter({
80+
name: 'connection_sync_job_reattempts',
81+
help: 'The number of connection sync job reattempts',
82+
labelNames: ['connection'],
83+
});
84+
this.registry.registerMetric(this.connectionSyncJobReattemptsTotal);
85+
86+
this.connectionSyncJobFailTotal = new Counter({
87+
name: 'connection_sync_job_fails',
88+
help: 'The number of connection sync job fails',
89+
labelNames: ['connection'],
90+
});
91+
this.registry.registerMetric(this.connectionSyncJobFailTotal);
92+
93+
this.connectionSyncJobSuccessTotal = new Counter({
94+
name: 'connection_sync_job_successes',
95+
help: 'The number of connection sync job successes',
96+
labelNames: ['connection'],
97+
});
98+
this.registry.registerMetric(this.connectionSyncJobSuccessTotal);
99+
59100
client.collectDefaultMetrics({
60101
register: this.registry,
61102
});

packages/db/prisma/migrations/20251026194628_ensure_single_tenant_org/migration.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
-- Installs the pgcrypto extension. Required for the gen_random_uuid() function.
2+
-- @see: https://www.prisma.io/docs/orm/prisma-migrate/workflows/native-database-functions#how-to-install-a-postgresql-extension-as-part-of-a-migration
3+
CREATE EXTENSION IF NOT EXISTS pgcrypto;
4+
15
-- Ensure single tenant organization exists
26
INSERT INTO "Org" (id, name, domain, "inviteLinkId", "createdAt", "updatedAt")
37
VALUES (1, 'default', '~', gen_random_uuid(), NOW(), NOW())

packages/web/src/app/[domain]/settings/connections/page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ export default async function ConnectionsPage() {
5050
}
5151

5252
const getConnectionsWithLatestJob = async () => sew(() =>
53-
withAuthV2(async ({ prisma }) => {
53+
withAuthV2(async ({ prisma, org }) => {
5454
const connections = await prisma.connection.findMany({
55+
where: {
56+
orgId: org.id,
57+
},
5558
include: {
5659
_count: {
5760
select: {

0 commit comments

Comments
 (0)