Skip to content

Commit 2769951

Browse files
committed
Ensure creating tags does not throw constraint validation error, fix batch v3 job retries
1 parent c519a5a commit 2769951

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

apps/webapp/app/models/taskRunTag.server.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
1-
import { prisma } from "~/db.server";
1+
import { Prisma, type PrismaClientOrTransaction } from "@trigger.dev/database";
22
import { generateFriendlyId } from "~/v3/friendlyIdentifiers";
3+
import cuid from "cuid";
4+
import { prisma } from "~/db.server";
35

46
export const MAX_TAGS_PER_RUN = 10;
57

68
export async function createTag({ tag, projectId }: { tag: string; projectId: string }) {
79
if (tag.trim().length === 0) return;
8-
return prisma.taskRunTag.upsert({
9-
where: {
10-
projectId_name: {
11-
projectId: projectId,
12-
name: tag,
13-
},
14-
},
15-
create: {
16-
name: tag,
17-
friendlyId: generateFriendlyId("runtag"),
18-
projectId: projectId,
19-
},
20-
update: {},
21-
});
10+
11+
const friendlyId = generateFriendlyId("runtag");
12+
const now = new Date();
13+
const id = cuid();
14+
15+
return await prisma
16+
.$queryRaw<Array<{ id: string; friendlyId: string; name: string; projectId: string }>>(
17+
Prisma.sql`
18+
INSERT INTO "TaskRunTag" ("id", "friendlyId", "name", "projectId", "createdAt")
19+
VALUES (${id}, ${friendlyId}, ${tag}, ${projectId}, ${now})
20+
ON CONFLICT ("projectId", "name")
21+
DO UPDATE SET "friendlyId" = "TaskRunTag"."friendlyId"
22+
RETURNING "id", "friendlyId", "name", "projectId"
23+
`
24+
)
25+
.then((rows) => rows[0]);
2226
}
2327

24-
export async function getTagsForRunId({
25-
friendlyId,
26-
environmentId,
27-
}: {
28-
friendlyId: string;
29-
environmentId: string;
30-
}) {
31-
const run = await prisma.taskRun.findFirst({
28+
export async function getTagsForRunId(
29+
{
30+
friendlyId,
31+
environmentId,
32+
}: {
33+
friendlyId: string;
34+
environmentId: string;
35+
},
36+
db: PrismaClientOrTransaction
37+
) {
38+
const run = await db.taskRun.findFirst({
3239
where: {
3340
friendlyId,
3441
runtimeEnvironmentId: environmentId,

apps/webapp/app/v3/services/batchTriggerV3.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ export class BatchTriggerV3Service extends BaseService {
682682
count:
683683
options.strategy === "sequential"
684684
? options.range.count
685-
: options.range.count - result.workingIndex - options.range.start,
685+
: options.range.count - (result.workingIndex - options.range.start),
686686
},
687687
attemptCount: $attemptCount,
688688
strategy: options.strategy,

references/v3-catalog/src/trigger/batch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const batchParentTask = task({
4141
},
4242
options: {
4343
idempotencyKey: `item${i}`,
44+
tags: ["foo:bar", "bar:baz"],
4445
},
4546
}));
4647

0 commit comments

Comments
 (0)