Files
Matt Aitken 4b3b418abb
🚀 Publish Trigger.dev Docker / units (push) Failing after 4s
🚀 Publish Trigger.dev Docker / typecheck (push) Failing after 5s
🚀 Publish Trigger.dev Docker / e2e (push) Failing after 4s
🚀 Publish Trigger.dev Docker / publish (push) Has been skipped
Set endpoint URLs to null, instead of deleting them (#878)
* Added Endpoint deletedAt column

* Only show endpoints where they’re not deleted

* Don’t delete Endpoints, set the deletedAt and change their slug name

* Only perform indexing if the endpoint isn’t deleted

* Have a nullable URL for endpoints

* Deal with null URLs throughout the app

* Re-running and retrying behaves properly when there’s no endpoint URL

* Remove console.log

* Better error message when doing a run
2024-01-30 10:45:12 +00:00

101 lines
2.5 KiB
TypeScript

import type { Job, JobVersion } from "@trigger.dev/database";
import { $transaction, PrismaClientOrTransaction } from "~/db.server";
import { prisma } from "~/db.server";
import { workerQueue } from "~/services/worker.server";
import type { AuthenticatedEnvironment } from "../apiAuth.server";
import { logger } from "../logger.server";
export class CreateRunService {
#prismaClient: PrismaClientOrTransaction;
constructor(prismaClient: PrismaClientOrTransaction = prisma) {
this.#prismaClient = prismaClient;
}
public async call(
{
environment,
eventId,
job,
version,
}: {
environment: AuthenticatedEnvironment;
eventId: string;
job: Job;
version: JobVersion;
},
options: { callbackUrl?: string } = {}
) {
if (!environment.organization.runsEnabled) {
logger.debug("Runs are disabled for this organization", environment);
return;
}
const endpoint = await this.#prismaClient.endpoint.findUniqueOrThrow({
where: {
id: version.endpointId,
},
});
if (!endpoint.url) {
logger.debug("Endpoint has no url", endpoint);
return;
}
const eventRecord = await this.#prismaClient.eventRecord.findUniqueOrThrow({
where: {
id: eventId,
},
});
return await $transaction(this.#prismaClient, async (tx) => {
const run = await tx.jobRun.create({
data: {
preprocess: version.preprocessRuns,
jobId: job.id,
versionId: version.id,
eventId: eventId,
environmentId: environment.id,
organizationId: environment.organizationId,
projectId: environment.projectId,
endpointId: endpoint.id,
externalAccountId: eventRecord.externalAccountId
? eventRecord.externalAccountId
: undefined,
isTest: eventRecord.isTest,
internal: job.internal,
},
});
if (options.callbackUrl) {
await tx.jobRunSubscription.createMany({
data: [
{
runId: run.id,
recipientMethod: "WEBHOOK",
recipient: options.callbackUrl,
event: "SUCCESS",
},
{
runId: run.id,
recipientMethod: "WEBHOOK",
recipient: options.callbackUrl,
event: "FAILURE",
},
],
});
}
await workerQueue.enqueue(
"startRun",
{
id: run.id,
},
{ tx }
);
return run;
});
}
}