Skip to content

Commit 03721cb

Browse files
committed
Merge branch 'Chigala-refactor/lower-max-retries-for-specific-tasks'
2 parents a107824 + 773a6e2 commit 03721cb

12 files changed

+82
-14
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type EnqueueRunExecutionV2Options = {
2727
runAt?: Date;
2828
resumeTaskId?: string;
2929
isRetry?: boolean;
30+
skipRetrying?: boolean;
3031
};
3132

3233
export async function enqueueRunExecutionV2(
@@ -47,6 +48,7 @@ export async function enqueueRunExecutionV2(
4748
tx,
4849
runAt: options.runAt,
4950
jobKey: `job_run:${run.id}`,
51+
maxAttempts: options.skipRetrying ? 1 : undefined,
5052
}
5153
);
5254
}

apps/webapp/app/platform/zodWorker.server.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,13 @@ export class ZodWorker<TMessageCatalog extends MessageCatalogSchema> {
195195
): Promise<GraphileJob> {
196196
const task = this.#tasks[identifier];
197197

198-
const optionsWithoutTx = omit(options ?? {}, ["tx"]);
198+
const optionsWithoutTx = removeUndefinedKeys(omit(options ?? {}, ["tx"]));
199199
const taskWithoutJobKey = omit(task, ["jobKey"]);
200200

201+
// Make sure options passed in to enqueue take precedence over task options
201202
const spec = {
202-
...optionsWithoutTx,
203203
...taskWithoutJobKey,
204+
...optionsWithoutTx,
204205
};
205206

206207
if (typeof task.queueName === "function") {
@@ -437,3 +438,12 @@ export class ZodWorker<TMessageCatalog extends MessageCatalogSchema> {
437438
logger.debug(`[worker][${this.#name}] ${message}`, args);
438439
}
439440
}
441+
442+
function removeUndefinedKeys<T extends object>(obj: T): T {
443+
for (let key in obj) {
444+
if (Object.prototype.hasOwnProperty.call(obj, key) && obj[key] === undefined) {
445+
delete obj[key];
446+
}
447+
}
448+
return obj;
449+
}

apps/webapp/app/routes/api.v1.endpoints.$environmentId.$endpointSlug.index.$indexHookIdentifier.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ActionArgs, LoaderArgs, json } from "@remix-run/server-runtime";
2+
import { RuntimeEnvironmentType } from "@trigger.dev/database";
23
import { z } from "zod";
34
import { PrismaClient, prisma } from "~/db.server";
45
import { logger } from "~/services/logger.server";
@@ -99,6 +100,9 @@ export class TriggerEndpointIndexHookService {
99100
slug: endpointSlug,
100101
},
101102
},
103+
include: {
104+
environment: true,
105+
},
102106
});
103107

104108
if (!endpoint) {
@@ -122,6 +126,8 @@ export class TriggerEndpointIndexHookService {
122126
},
123127
{
124128
runAt: new Date(Date.now() + 5000),
129+
maxAttempts:
130+
endpoint.environment.type === RuntimeEnvironmentType.DEVELOPMENT ? 1 : undefined,
125131
}
126132
);
127133
}

apps/webapp/app/services/endpointApi.server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ export class EndpointApiError extends Error {
2828
}
2929

3030
export class EndpointApi {
31-
constructor(private apiKey: string, private url: string) {}
31+
constructor(
32+
private apiKey: string,
33+
private url: string
34+
) {}
3235

3336
async ping(endpointId: string): Promise<PongResponse> {
3437
const response = await safeFetch(this.url, {

apps/webapp/app/services/endpoints/createEndpoint.server.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AuthenticatedEnvironment } from "../apiAuth.server";
44
import { EndpointApi } from "../endpointApi.server";
55
import { workerQueue } from "../worker.server";
66
import { env } from "~/env.server";
7+
import { RuntimeEnvironmentType } from "@trigger.dev/database";
78

89
const indexingHookIdentifier = customAlphabet("0123456789abcdefghijklmnopqrstuvxyz", 10);
910

@@ -51,6 +52,9 @@ export class CreateEndpointService {
5152
slug: id,
5253
},
5354
},
55+
include: {
56+
environment: true,
57+
},
5458
create: {
5559
environment: {
5660
connect: {
@@ -83,7 +87,11 @@ export class CreateEndpointService {
8387
id: endpoint.id,
8488
source: "INTERNAL",
8589
},
86-
{ tx }
90+
{
91+
tx,
92+
maxAttempts:
93+
endpoint.environment.type === RuntimeEnvironmentType.DEVELOPMENT ? 1 : undefined,
94+
}
8795
);
8896

8997
return endpoint;

apps/webapp/app/services/endpoints/validateCreateEndpoint.server.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AuthenticatedEnvironment } from "../apiAuth.server";
55
import { workerQueue } from "../worker.server";
66
import { CreateEndpointError } from "./createEndpoint.server";
77
import { EndpointApi } from "../endpointApi.server";
8+
import { RuntimeEnvironmentType } from "@trigger.dev/database";
89

910
const indexingHookIdentifier = customAlphabet("0123456789abcdefghijklmnopqrstuvxyz", 10);
1011

@@ -35,6 +36,9 @@ export class ValidateCreateEndpointService {
3536
slug: validationResult.endpointId,
3637
},
3738
},
39+
include: {
40+
environment: true,
41+
},
3842
create: {
3943
environment: {
4044
connect: {
@@ -67,7 +71,11 @@ export class ValidateCreateEndpointService {
6771
id: endpoint.id,
6872
source: "INTERNAL",
6973
},
70-
{ tx }
74+
{
75+
tx,
76+
maxAttempts:
77+
endpoint.environment.type === RuntimeEnvironmentType.DEVELOPMENT ? 1 : undefined,
78+
}
7179
);
7280

7381
return endpoint;

apps/webapp/app/services/events/ingestSendEvent.server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import { logger } from "../logger.server";
77
export class IngestSendEvent {
88
#prismaClient: PrismaClientOrTransaction;
99

10-
constructor(prismaClient: PrismaClientOrTransaction = prisma, private deliverEvents = true) {
10+
constructor(
11+
prismaClient: PrismaClientOrTransaction = prisma,
12+
private deliverEvents = true
13+
) {
1114
this.#prismaClient = prismaClient;
1215
}
1316

apps/webapp/app/services/runs/continueRun.server.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RuntimeEnvironmentType } from "@trigger.dev/database";
12
import { $transaction, Prisma, PrismaClient, prisma } from "~/db.server";
23
import { enqueueRunExecutionV2 } from "~/models/jobRunExecution.server";
34

@@ -16,6 +17,9 @@ export class ContinueRunService {
1617
async (tx) => {
1718
const run = await tx.jobRun.findUniqueOrThrow({
1819
where: { id: runId },
20+
include: {
21+
environment: true,
22+
},
1923
});
2024

2125
if (!RESUMABLE_STATUSES.includes(run.status)) {
@@ -35,7 +39,9 @@ export class ContinueRunService {
3539
},
3640
});
3741

38-
await enqueueRunExecutionV2(run, tx);
42+
await enqueueRunExecutionV2(run, tx, {
43+
skipRetrying: run.environment.type === RuntimeEnvironmentType.DEVELOPMENT,
44+
});
3945
},
4046
{ timeout: 10000 }
4147
);

apps/webapp/app/services/runs/performRunExecutionV2.server.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
RunJobSuccess,
77
RunSourceContextSchema,
88
} from "@trigger.dev/core";
9-
import type { Task } from "@trigger.dev/database";
9+
import { RuntimeEnvironmentType, type Task } from "@trigger.dev/database";
1010
import { generateErrorMessage } from "zod-error";
1111
import { eventRecordToApiJson } from "~/api.server";
1212
import { $transaction, PrismaClient, PrismaClientOrTransaction, prisma } from "~/db.server";
@@ -135,7 +135,9 @@ export class PerformRunExecutionV2Service {
135135
},
136136
});
137137

138-
await enqueueRunExecutionV2(run, tx);
138+
await enqueueRunExecutionV2(run, tx, {
139+
skipRetrying: run.environment.type === RuntimeEnvironmentType.DEVELOPMENT,
140+
});
139141
});
140142
}
141143
}
@@ -337,6 +339,7 @@ export class PerformRunExecutionV2Service {
337339
runAt: data.task.delayUntil ?? undefined,
338340
resumeTaskId: data.task.id,
339341
isRetry,
342+
skipRetrying: run.environment.type === RuntimeEnvironmentType.DEVELOPMENT,
340343
});
341344
}
342345
});
@@ -409,6 +412,7 @@ export class PerformRunExecutionV2Service {
409412
runAt: data.retryAt,
410413
resumeTaskId: data.task.id,
411414
isRetry,
415+
skipRetrying: run.environment.type === RuntimeEnvironmentType.DEVELOPMENT,
412416
});
413417
});
414418
}
@@ -464,7 +468,9 @@ export class PerformRunExecutionV2Service {
464468
},
465469
});
466470

467-
await enqueueRunExecutionV2(run, tx);
471+
await enqueueRunExecutionV2(run, tx, {
472+
skipRetrying: run.environment.type === RuntimeEnvironmentType.DEVELOPMENT,
473+
});
468474

469475
break;
470476
}

apps/webapp/app/services/runs/startRun.server.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { ConnectionType, Integration, IntegrationConnection } from "@trigger.dev/database";
1+
import {
2+
RuntimeEnvironmentType,
3+
type ConnectionType,
4+
type Integration,
5+
type IntegrationConnection,
6+
} from "@trigger.dev/database";
27
import type { PrismaClient, PrismaClientOrTransaction } from "~/db.server";
38
import { prisma } from "~/db.server";
49
import { enqueueRunExecutionV2 } from "~/models/jobRunExecution.server";
@@ -83,7 +88,9 @@ export class StartRunService {
8388

8489
const updatedRun = await updateRun();
8590

86-
await enqueueRunExecutionV2(updatedRun, this.#prismaClient);
91+
await enqueueRunExecutionV2(updatedRun, this.#prismaClient, {
92+
skipRetrying: run.environment.type === RuntimeEnvironmentType.DEVELOPMENT,
93+
});
8794
}
8895

8996
async #handleMissingConnections(id: string, runConnectionsByKey: RunConnectionsByKey) {
@@ -140,6 +147,7 @@ async function findRun(tx: PrismaClientOrTransaction, id: string) {
140147
where: { id },
141148
include: {
142149
queue: true,
150+
environment: true,
143151
version: {
144152
include: {
145153
integrations: {

0 commit comments

Comments
 (0)