Skip to content

Commit bad7757

Browse files
remove transaction for job creation
1 parent 26a7555 commit bad7757

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

packages/backend/src/ee/repoPermissionSyncer.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,25 @@ export class RepoPermissionSyncer {
110110
}
111111

112112
private async schedulePermissionSync(repos: Repo[]) {
113-
await this.db.$transaction(async (tx) => {
114-
const jobs = await tx.repoPermissionSyncJob.createManyAndReturn({
115-
data: repos.map(repo => ({
116-
repoId: repo.id,
117-
})),
118-
});
119-
120-
await this.queue.addBulk(jobs.map((job) => ({
121-
name: 'repoPermissionSyncJob',
122-
data: {
123-
jobId: job.id,
124-
},
125-
opts: {
126-
removeOnComplete: env.REDIS_REMOVE_ON_COMPLETE,
127-
removeOnFail: env.REDIS_REMOVE_ON_FAIL,
128-
}
129-
})))
113+
// @note: we don't perform this in a transaction because
114+
// we want to avoid the situation where a job is created and run
115+
// prior to the transaction being committed.
116+
const jobs = await this.db.repoPermissionSyncJob.createManyAndReturn({
117+
data: repos.map(repo => ({
118+
repoId: repo.id,
119+
})),
130120
});
121+
122+
await this.queue.addBulk(jobs.map((job) => ({
123+
name: 'repoPermissionSyncJob',
124+
data: {
125+
jobId: job.id,
126+
},
127+
opts: {
128+
removeOnComplete: env.REDIS_REMOVE_ON_COMPLETE,
129+
removeOnFail: env.REDIS_REMOVE_ON_FAIL,
130+
}
131+
})))
131132
}
132133

133134
private async runJob(job: Job<RepoPermissionSyncJob>) {

packages/backend/src/ee/userPermissionSyncer.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Redis } from "ioredis";
66
import { PERMISSION_SYNC_SUPPORTED_CODE_HOST_TYPES } from "../constants.js";
77
import { env } from "../env.js";
88
import { createOctokitFromToken, getReposForAuthenticatedUser } from "../github.js";
9-
import { createGitLabFromOAuthToken, createGitLabFromPersonalAccessToken, getProjectsForAuthenticatedUser } from "../gitlab.js";
9+
import { createGitLabFromOAuthToken, getProjectsForAuthenticatedUser } from "../gitlab.js";
1010
import { hasEntitlement } from "@sourcebot/shared";
1111
import { Settings } from "../types.js";
1212

@@ -113,24 +113,25 @@ export class UserPermissionSyncer {
113113
}
114114

115115
private async schedulePermissionSync(users: User[]) {
116-
await this.db.$transaction(async (tx) => {
117-
const jobs = await tx.userPermissionSyncJob.createManyAndReturn({
118-
data: users.map(user => ({
119-
userId: user.id,
120-
})),
121-
});
122-
123-
await this.queue.addBulk(jobs.map((job) => ({
124-
name: 'userPermissionSyncJob',
125-
data: {
126-
jobId: job.id,
127-
},
128-
opts: {
129-
removeOnComplete: env.REDIS_REMOVE_ON_COMPLETE,
130-
removeOnFail: env.REDIS_REMOVE_ON_FAIL,
131-
}
132-
})))
116+
// @note: we don't perform this in a transaction because
117+
// we want to avoid the situation where a job is created and run
118+
// prior to the transaction being committed.
119+
const jobs = await this.db.userPermissionSyncJob.createManyAndReturn({
120+
data: users.map(user => ({
121+
userId: user.id,
122+
})),
133123
});
124+
125+
await this.queue.addBulk(jobs.map((job) => ({
126+
name: 'userPermissionSyncJob',
127+
data: {
128+
jobId: job.id,
129+
},
130+
opts: {
131+
removeOnComplete: env.REDIS_REMOVE_ON_COMPLETE,
132+
removeOnFail: env.REDIS_REMOVE_ON_FAIL,
133+
}
134+
})))
134135
}
135136

136137
private async runJob(job: Job<UserPermissionSyncJob>) {

0 commit comments

Comments
 (0)