-
-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow cancelling jobs from trigger-client sdk (#562)
* feat: allow cancelling jobs from trigger-client sdk * use presenter instead of service for non-mutating logic
- Loading branch information
Showing
6 changed files
with
176 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@trigger.dev/sdk": patch | ||
--- | ||
|
||
allow cancelling jobs from trigger-client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Job } from "@trigger.dev/database"; | ||
import { PrismaClient, prisma } from "~/db.server"; | ||
|
||
type ApiRunOptions = { | ||
runId: Job["id"]; | ||
maxTasks?: number; | ||
taskDetails?: boolean; | ||
subTasks?: boolean; | ||
cursor?: string; | ||
}; | ||
|
||
export class ApiRunPresenter { | ||
#prismaClient: PrismaClient; | ||
|
||
constructor(prismaClient: PrismaClient = prisma) { | ||
this.#prismaClient = prismaClient; | ||
} | ||
|
||
public async call({ | ||
runId, | ||
maxTasks = 20, | ||
taskDetails = false, | ||
subTasks = false, | ||
cursor, | ||
}: ApiRunOptions) { | ||
const take = Math.min(maxTasks, 50); | ||
|
||
return await prisma.jobRun.findUnique({ | ||
where: { | ||
id: runId, | ||
}, | ||
select: { | ||
id: true, | ||
status: true, | ||
startedAt: true, | ||
updatedAt: true, | ||
completedAt: true, | ||
environmentId: true, | ||
output: true, | ||
tasks: { | ||
select: { | ||
id: true, | ||
parentId: true, | ||
displayKey: true, | ||
status: true, | ||
name: true, | ||
icon: true, | ||
startedAt: true, | ||
completedAt: true, | ||
params: taskDetails, | ||
output: taskDetails, | ||
}, | ||
where: { | ||
parentId: subTasks ? undefined : null, | ||
}, | ||
orderBy: { | ||
id: "asc", | ||
}, | ||
take: take + 1, | ||
cursor: cursor | ||
? { | ||
id: cursor, | ||
} | ||
: undefined, | ||
}, | ||
statuses: { | ||
select: { key: true, label: true, state: true, data: true, history: true }, | ||
}, | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import type { ActionArgs } from "@remix-run/server-runtime"; | ||
import { json } from "@remix-run/server-runtime"; | ||
import { PrismaErrorSchema } from "~/db.server"; | ||
import { z } from "zod"; | ||
import { authenticateApiRequest } from "~/services/apiAuth.server"; | ||
import { CancelRunService } from "~/services/runs/cancelRun.server"; | ||
import { ApiRunPresenter } from "~/presenters/ApiRunPresenter.server"; | ||
|
||
const ParamsSchema = z.object({ | ||
runId: z.string(), | ||
}); | ||
|
||
export async function action({ request, params }: ActionArgs) { | ||
// Ensure this is a POST request | ||
if (request.method.toUpperCase() !== "POST") { | ||
return { status: 405, body: "Method Not Allowed" }; | ||
} | ||
|
||
// Authenticate the request | ||
const authenticationResult = await authenticateApiRequest(request); | ||
|
||
if (!authenticationResult) { | ||
return json({ error: "Invalid or Missing API Key" }, { status: 401 }); | ||
} | ||
|
||
const parsed = ParamsSchema.safeParse(params); | ||
|
||
if (!parsed.success) { | ||
return json({ error: "Invalid or Missing runId" }, { status: 400 }); | ||
} | ||
|
||
const { runId } = parsed.data; | ||
|
||
const service = new CancelRunService(); | ||
try { | ||
await service.call({ runId }); | ||
} catch (error) { | ||
const prismaError = PrismaErrorSchema.safeParse(error); | ||
// Record not found in the database | ||
if (prismaError.success && prismaError.data.code === "P2005") { | ||
return json({ error: "Run not found" }, { status: 404 }); | ||
} else { | ||
return json({ error: "Internal Server Error" }, { status: 500 }); | ||
} | ||
} | ||
|
||
const presenter = new ApiRunPresenter(); | ||
const jobRun = await presenter.call({ | ||
runId: runId, | ||
}); | ||
|
||
if (!jobRun) { | ||
return json({ message: "Run not found" }, { status: 404 }); | ||
} | ||
|
||
return json({ | ||
id: jobRun.id, | ||
status: jobRun.status, | ||
startedAt: jobRun.startedAt, | ||
updatedAt: jobRun.updatedAt, | ||
completedAt: jobRun.completedAt, | ||
output: jobRun.output, | ||
tasks: jobRun.tasks, | ||
statuses: jobRun.statuses.map((s) => ({ | ||
...s, | ||
state: s.state ?? undefined, | ||
data: s.data ?? undefined, | ||
history: s.history ?? undefined, | ||
})), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters